Vous êtes sur la page 1sur 9

1

UNIT-I
CS6301 Programming and Data Structures II
III-Sem-CSE III -Sem-IT 2013-Regulations
Anna University
Question Bank
[OBJECT ORIENTED PROGRAMMING FUNDAMENTALS]

PART-A

1) Why do we need object oriented programming?


Object-oriented programming was developed because limitations were discovered in
earlier approaches to programming known as procedure oriented programming.

2) What are the characteristics and drawbacks of procedure oriented programming?

3) What are the features of Object Oriented Programming Paradigm?


2

The fundamental idea behind object-oriented languages is to combine into a single


unit both data and the functions that operate on that data. Such a unit is called an
object.

4) What are the basic concepts of Object Oriented Programming?

5) What is an object ?
Objects are the basic run time entities in an object oriented system. They may
represent a person , a place or any data item. Objects contain data and code to manipulate
data.

6) What is a class?

The entire set of data and code of an object that can be made a user defined data
type with the help of a class. A class is a collection of objects of type class.

7) How C++ evolved? Give an overview.


C++ was developed by Bjarne Stroustrup at Bell Laboratories
3

o Originally called C with classes


o The name C++ is based on Cs increment operator (++) Indicating that C++ is an
enhanced version of C
Widely used in many applications and fields
Well-suited to Programming in the Large

8) What are the features of a C++ Program?


A C++ program typically consists of a number of objects, which communicate with each other
by calling one anothers member functions. The organization of a C++ program is shown in
Figure below :

9) What is data abstraction?

Data abstraction refers to, providing only essential information to the outside
world and hiding their background details ie. to represent the needed
information in program without presenting the details.Data abstraction is a
programming (and design) technique that relies on the separation of interface
and implementation.

C++ classes provides great level of data abstraction. They provide sufficient
public methods to the outside world to play with the functionality of the object
and to manipulate object data ie. state without actually knowing how class has
been implemented internally.

10) What is data encapsulation?

Encapsulation is an Object Oriented Programming concept that binds together the


data and functions that manipulate the data, and that keeps both safe from outside
interference and misuse. Data encapsulation led to the important OOP concept
of data hiding.
4

Data encapsulation is a mechanism of bundling the data, and the functions that
use them and data abstraction is a mechanism of exposing only the interfaces and
hiding the implementation details from the user.

11) What is data hiding?


The insulation of the data from direct access by the program is called data hiding
or information hiding

12) What are inheritance / reusability / derivation?


Inheritance is the process by which objects of one class acquire the properties of
objects of another class.
The concept of inheritance provides the idea of reusability. This means that we can add
additional features to an existing class without modifying it .
Derivation involves the creation of new classes ( derived class ) from the existing ones
(base class).

13) What are member functions?


Member Functions
Member functions are functions that are included within a class. Member functions are called
method as well.

14) Define function.


The process of splitting a large program into small manageable tasks and designing them
independently is popularly called modular programming or divide and conquer technique. A
repeated group of instructions in a program can be organized as a function. A function is a set of
program statements that can be processed independently.

15) What is a pointer? What are the uses of a pointer?


Pointer is defined as a variable used to store memory addresses.
A pointer is used for :
Accessing array elements .
Passing arguments to a function when the function needs to modify the original.
Passing arrays and strings to functions.
Creating data structures such as linked lists, binary tree etc .
Obtaining memory from the system dynamically.

16) What is a friend function?
Friend function is a special type of function which is used to access all the private
and protected members of a class. The functions that are declared with the keyword
friend are called friend functions. A function can be a friend to multiple classes.

17) What are the properties of a friend function?


A friend function is not in the scope of the class to which it has been declared as
friend.
It can be invoked like a normal function without the help of any object.
5

Unlike member functions it cannot access the member names directly and has to use
an object name and dot membership with each member name.

18) What is the difference between friend function and member function?
The only difference between a friend function and member function is that, the
friend function requires the argument to be explicitly passed to the function and processes
them explicitly, whereas, the member function considers the first argument implicitly.

19) What is an inline function?


Inline functions are those whose function body is inserted in place of the function
call statement during the compilation process. With the inline code the program will not
incur any context switching overhead.

20) What is a recursive function?


A function that contains a function call to itself or a function call to a second
function which eventually calls the first function is known as recursive functions.

21) Give the structure of a C++ program.


Include files
Class definition
Member function definitions
Main function
22) What is function prototype?
Function prototype is otherwise known as function declaration. The prototype
describes the function interface to the compiler by giving details such as the number and
type of arguments and the type of return values.

23) What is a class ? How will you define a class?


A class is a way to bind the data and its associated functions together. A class
specification has two parts :
Class declaration
Class function definition
24) What are the characteristics of a static data member?
Static data member is initialized to zero when the first object of its class is created. No
other initialization is permitted.
Only one copy of that member is created for the entire class and is shared by all the
objects of that class, no matter how many objects are created.
It is visible only within the class, but its life time is the entire program.

25) What are the properties of a static member function?


A static function can have access to only other static members, declared in the same class.
A static member function can be called using the class name as follows
Classname :: function-name;

26) In what way is a private member function different from public member
function.
6

A private member function can only be called by another function that is a


member of its class. Even an object cannot invoke a private function using the dot
operator.

27) What is a constructor ?


A constructor is a special member function whose task is to initialize the objects of
its class. It is special because its name is the same as the class name.
The constructor is invoked whenever an object of its associated class is created. It
is called constructor because it constructs the values f data members of the class.

28) What are storage classes? Give examples.

Storage Classes in C++


Storage classes are used to specify the lifetime and scope of variables. How storage is
allocated for variables and How variable is treated by complier depends on these storage
classes.
These are basically divided into 5 different types :
Global variables
Local variables

Register variables

Static variables

Extern variables

Global Variables
These are defined at the starting , before all function bodies and are available throughout
the program.
using namespace std;
int globe; // Global variable
void func();
int main()
{
.....
}

Local variables
They are defined and are available within a particular scope. They are also
called Automatic variablebecause they come into being when scope is entered and
automatically go away when the scope ends.
7

The keyword auto is used, but by default all local variables are auto, so we don't have to
explicitly add keyword auto before variable dedaration. Default value of such variable
is garbage.

Register variables
This is also a type of local variable. This keyword is used to tell the compiler to make
access to this variable as fast as possible. Variables are stored in registers to increase the
access speed.
But you can never use or compute address of register variable and also , a register
variable can be declared only within a block, that means, you cannot
have global or static register variables.

Static Variables
Static variables are the variables which are initialized & allocated storage only once at the
beginning of program execution, no matter how many times they are used and called in
the program. A static variable retains its value until the end of program.
void fun()
{
static int i = 10;
i++;
cout << i;
}
int main()
{
fun(); // Output = 11
fun(); // Output = 12
fun(); // Output = 13
}
As, i is static, hence it will retain its value through function calls, and is initialized only
once at the beginning.
Static specifiers are also used in classes, but that we will learn later.

Extern Variables
This keyword is used to access variable in a file which is declared & defined in some
other file, that is the existence of a global variable in one file is declared using extern
keyword in another file.
8

29) Explain with an example where functions are used as arguments in a function call in C+
+.

Passing Functions as Arguments in C++

C++ allow functions to be passed as parameters. This allows a degree of generic


programming in which entire tasks (not just values!) are provided as parameters to a
function.

Declaration

A prototype for a function which takes a function parameter looks like the following:

void func ( void (*f)(int) );

This states that the parameter f will be a pointer to a function which has a void return type
and which takes a single int parameter. The following function (print) is an example of a
function which could be passed to func as a parameter because it is the proper type:

void print ( int x ) {


cout << x << endl;
}

Function Call
9

When calling a function with a function parameter, the value passed must be a pointer to
a function. Use the function's name (without parens) for this:

func(print);

would call func, passing the print function to it.

Function Body

As with any parameter, func can now use the parameter's name in the function body to
access the value of the parameter. Let's say that func will apply the function it is passed to
the numbers 0-4. Consider, first, what the loop would look like to call print directly:

for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {


print(x);
}

Since func's parameter declaration says that f is the name for a pointer to the desired
function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the
function print in this case). As a result, just replace every occurrence of print in the loop
above with *f:

void func ( void (*f)(int) ) {


for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
(*f)(x);
}
}

Vous aimerez peut-être aussi