Friday, August 6, 2010

Cpp Theory Questions 1

What is Inheritance?

Inheritance is the process by which objects of one class acquire the properties of objects of another class.

 

Inheritance allows one class to reuse the state and behavior of another class.

The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.

 

In C++, inheritance defines an "is a" relationship. The derived class is a type of its base class. An object of a class SortedList (which inherits from class List) can be treated as an object of class List in most cases

 

Terminology: "the child (or derived) class inherits (or is derived from) the parent (or base) class."

 

 

An example:

 

 

class Name

{

public:

Name(void) { myName = 0; }

~Name(void) { delete[] myName; }

 

void SetName(char* n)

{

myName = new char[strlen(n)+1];

strcpy(myName,n);

}

 

void Print(void) { cout << myName << endl; }

 

private:

char* myName;

};

 

class Contact: public Name

{

public:

Contact(void) { myAddress = 0; }

 

~Contact(void) { delete[] myAddress; }

 

void SetAddress(char* c)

{

myAddress = new char[strlen(c)+1];

strcpy(myAddress,c);

}

 

void Print(void)

{

Name::Print();

cout << myAddress << endl;

}

 

private:

char* myAddress;

};

 

int main(void)

{

Contact c;

 

c.SetName("John McClane");

c.SetAddress("137th floor, Nakatome Towers");

 

c.Print();

}

---------------------------------------------------------------------------------------------------------------------

What is encapsulation?

 

The wrapping of data and functions into a single unit(called class) is known as encapsulation.

 

The data is not accessible to the outside world and only those functions which are wrapped in the class can access it. These functions provide the interface between the objects data and the program. This insulation of the data from direct access by the program is called data hiding.

 

Containing and hiding information about an object, such as internal data structures and code.

Encapsulation isolates the internal complexity of an object's operation from the rest of the application.

For example, a client component asking for net revenue from a business object need not know the data's origin.

---------------------------------------------------------------------------------------------------------------------

 

What is Polymorphism?

 

Polymorphism allows a client to treat different objects in the same way even

if they were created from different classes and exhibit different behaviors.

 

You can use implementation inheritance to achieve polymorphism in languages such as C++ and Java.

 

Base class object's pointer can invoke methods in derived class objects.

 

You can also achieve polymorphism in C++ by function overloading and operator overloading.

---------------------------------------------------------------------------------------------------------------------

 

What is constructor or ctor?

 

Constructor creates an object and initializes it.

It also creates vtable for virtual functions. It is different from other methods in a class.

---------------------------------------------------------------------------------------------------------------------

 

What is destructor?

 

Destructor usually deletes any extra resources allocated by the object.

What is default constructor?

 

Constructor with no arguments or all the arguments has default values.

---------------------------------------------------------------------------------------------------------------------

 

What is copy constructor?

 

Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class.

If you don't implement one in your class then compiler implements one for you.

 

for example:

Boo Obj1(10); // calling Boo constructor

 

Boo Obj2(Obj1); // calling boo copy constructor

Boo Obj2 = Obj1;// calling boo copy constructor

---------------------------------------------------------------------------------------------------------------------

 

When are copy constructors called?

 

Copy constructors are called in following cases:

a) when a function returns an object of that class by value

b) when the object of that class is passed by value as an argument to a function

c) when you construct an object based on another object of the same class

d) When compiler generates a temporary object

---------------------------------------------------------------------------------------------------------------------

 

What is assignment operator?

 

Default assignment operator handles assigning one object to another of the same class. Member to member copy (shallow copy)

What are all the implicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one.??

 

default ctor

copy ctor

assignment operator

default destructor

address operator

---------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment