You are on page 1of 5

//CONSTRUCTORS

- special member method - class name - no return type - parameters - invoked object
creation
- object initialization - overloaded

types of constructors
1. default 2.parameterized 3.copy constructors

//default
- no args constructors - compiler generates if no const specified - garbage values
assigned to objects
- invoked for object created with no args
player ranjith;
player *ptr = new player ;

// Overloading constructors
-------------------------------------
* classes can have as many constructors as necessary
* each must have an unique signature
* default constructor is no longer compiler generator once another constructor is
declared.

// What is a copy constructor?


A copy constructor is a member function which initializes an object using another
object of the same class.
A copy constructor has the following general function prototype:

In C++, a Copy Constructor may be called in following cases:


1. When an object of the class is returned by value.
2. When an object of the class is passed (to a function) by value as an argument.
3. When an object is constructed based on another object of the same class.
4. When the compiler generates a temporary object.

// Best practices
--------------

* provide a copy constructor when your class has raw pointer members
* provide the copy constructor with const reference parameter.
* Use STL classes as they already provide copy constructor
* avoid using raw pointer data members if possible.

// Declaring the copy constructor


------------------------------
* passing by reference to avoid the copy
* const prevents any modification of source values.

Type::Type(const Type &source);


player::player(const player &source);
Account::Account(const Account &source);

/*-------------COPY CONSTRUCTOR----------------------------------------------*/
class1 obj3= obj2 ; //Copy constructor

class1(const class1 &obj)


{
cout<<"\n copy constructor is called";
data1=obj.data1;
data2=obj.data2;
}

/*--------------ASSIGNMENT OPERATOR------------------------*/
class1 obj4; //object initialized first
obj4 = obj3; // copied from existing object

class1& operator = (const class1 &obj)


{
cout<<"\n assignment operator called";
data1=obj.data2;
data2=obj.data1;

// Constructor Delegation in C++

* constructor to be able to call another constructor of the same class.


* calling a constructor by placing it in the initializer list of other constructors

A()
{
x = 0;
y = 0;
z = 0;
}

// Constructor delegation
A(int z) : A()
{
this->z = z; // Only update z
}

A obj(3);
obj.show(); // 0 0 3

//DESTRUCTORS
- special member method - ~class_name - no return type and parameters - invoked
object destroyed
- release memory and other resourses - one per class - cannot be overloaded

//Virtual Function in C++

* A virtual function is a member function which is declared within a base class and
is re-defined(Overriden) by a derived class.
* When you refer to a derived class object using a pointer or a reference to the
base class, you can call a virtual function for that object and execute the derived
class’s version of the function.

- Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for function call.
- They are mainly used to achieve Runtime polymorphism
- Functions are declared with a virtual keyword in base class.
- The resolving of function call is done at Run-time.

base* bptr;
derived d;
bptr = &d;
// virtual function, binded at runtime
bptr->print();

// Non-virtual function, binded at compile time


bptr->show();

// Rules for Virtual Functions

* cannot be static and also cannot be a friend function of another class.


* should be accessed using pointer or reference of base class type to achieve
run time polymorphism.
* The prototype of virtual functions should be same in base as well as derived
class.
* They are always defined in base class and overridden in derived class. It is
not mandatory for derived class to override (or re-define the virtual function), in
that case base class version of function is used.
* A class may have virtual destructor but it cannot have a virtual constructor.

Working of virtual functions(concept of VTABLE and VPTR)

As discussed here, If a class contains a virtual function then compiler itself does
two things:

* If object of that class is created then a virtual pointer(VPTR) is inserted


as a data member of the class to point to VTABLE of that class.
For each new object created, a new virtual pointer is inserted as a data
member of that class.
* Irrespective of object is created or not, a static array of function pointer
called VTABLE where each cell contains the address of each virtual function
contained in that class.

//1. What is a pure virtual function?

* A pure virtual function (or abstract function) in C++ is a virtual function for
which we don’t have implementation, we only declare it.
* A pure virtual function is declared by assigning 0 in declaration.

// An abstract class
class Test {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;

/* Other members */
};

// 2. What is abstract class?


A class which contains atleast one pure virtual function, is known as abstract
class.

// Some interesting facts about abstract class


1. We can’t create an object of abstract class.
Compiler Error: cannot declare variable 't' to be of abstract
2. We can have pointers and references of abstract class type.
Base* bp = new Derived();
bp->show();
3. If we do not override the pure virtual function in derived class, then derived
class also becomes abstract class.

// Can virtual functions be private in C++? Yes

In C++, virtual functions can be private and can be overridden by the derived
class.
For example, the following program compiles and runs fine.

// Can static functions be virtual in C++? No

In C++, a static member function of a class cannot be virtual. For example, below
program gives compilation error.

// Polymorphism in C++

1. Compile time Polymorphism


2. Runtime Polymorphism

// Compile time polymorphism:


This type of polymorphism is achieved by function overloading or operator
overloading.

1. Function Overloading:
* When there are multiple functions with same name but different parameters then
these functions are said to be overloaded.
* Functions can be overloaded by change in number of arguments or/and change in
type of arguments.

2. Operator Overloading:
* C++ also provide option to overload operators. For example, we can make the
operator (‘+’) for string class to concatenate two strings.
* We know that this is the addition operator whose task is to add two operands.
* So a single operator ‘+’ when placed between integer operands , adds them and
when placed between string operands, concatenates them.

// Runtime polymorphism:
* This type of polymorphism is achieved by Function Overriding.

1. Function overriding
* on the other hand occurs when a derived class has a definition for one of the
member functions of the base class.
* That base function is said to be overridden.

// Virtual Destructor

* Deleting a derived class object using a pointer to a base class that has a non-
virtual destructor results in undefined behavior.
* To correct this situation, the base class should be defined with a virtual
destructor.
For example, following program results in undefined behavior.

derived *d = new derived();


base *b = d;
delete b;
Constructing base
Constructing derived
Destructing base

You might also like