You are on page 1of 30

• Object slicing

•Function overriding
•Virtual function
•pure virtual function
•Abstract class
•Virtual destructors

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Object slicing
 When a derived class object is assigned to a base class
object, the derived class object’s extra attributes are
sliced off (not considered) to generate the base class
object, is called object slicing.
 In C++, a derived class object can be assigned to a base
class object, but the other way is not possible.
 To tackle this slicing problem we can use pointers with
virtual functions.

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Object slicing
 Example-
class Base
{
protected:
int i;
public:
Base(int a) { i = a; }
void display()
{
cout << "Base class object, i = " << i << endl;
}
};

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Object slicing
class Derived : public Base
{
int j;

public:
Derived(int a, int b): Base(a)
{
j = b;
}
void display()
{
cout << "Derived class object, i = " << i<< ", j = " << j << endl;
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Object slicing
int main()
{
Base b(33);
Derived d(45, 54);
b.display();
b=d;
b.display();// Object Slicing, the member j of d is
sliced off
return 0;
}
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Object slicing
 Output
Base class object, i = 33
Base class object, i = 45

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Function overriding
 If base class and derived class have functions with
same prototype then derived class object invoke only
derived class function, is called function overriding.
 The derived class function overrides the base class
function.

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Function overriding

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Accessing the Overridden Function
in Base Class From Derived Class

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual function
 A virtual function is a member function which is
declared within a base class and is re-defined
(Overriden) by a derived class.
 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.

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual functions
 Rules for Virtual Functions-
They must be a member function of a class.
 They cannot be static.
 They can be a friend function of another
class.
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.
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Virtual functions
 Example-1
class Base
{
protected:
int i;
public:
Base(int a) { i = a; }
virtual void display()
{
cout << "Base class object, i = " << i << endl;
}
};

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual functions
class Derived : public Base
{
int j;

public:
Derived(int a, int b): Base(a)
{
j = b;
}
void display()
{
cout << "Derived class object, i = " << i<< ", j = " << j << endl;
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Virtual functions
int main()
{
Base b(33);
Derived d(45, 54);
Base *bptr;
bptr = &b;
bptr->display(); // calls base class version
bptr = &d;
bptr->display(); // calls derived class version
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual functions
 Output
Base class object, i = 33
Derived class object, i = 45 j = 54

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual function
• Example-2
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}

void show()
{
cout << "show base class" << endl;
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Virtual function
class derived : public base {
public:
void print()
{
cout << "print derived class" << endl;
}

void show()
{
cout << "show derived class" << endl;
}
};

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual function
int main()
{
base* bptr;
derived d;
bptr = &d;

// virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();
}
 Output
print derived class
show base class

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Pure virtual function

 A virtual function with empty body is called pure


virtual function.
 Syntax-
virtual int area() { }
OR
virtual int area() = 0;
 The pure virtual function must be defined by the
derived classes.

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Pure virtual function
class Shape
{
protected:
int width;
int height;
public:
virtual int area() = 0; // pure virtual function
void getdata(int w, int h)
{
width=w;
height=h;
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Pure virtual function
class Rectangle: public Shape
{
public:
int area()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int area() {
return (width * height)/2;
}
};

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Pure virtual function
int main(void)
{
Shape * sp[2];
Rectangle R;
Triangle T;
sp[0] = &R;
sp[1] = &T;
R.getdata(5,7);
T.getdata(6,7);
cout << “Area of Rectangle :” << sp[0]->area() <<“\n”;
cout << “Area of Triangle :”<< sp[1]->area() <<“\n”;
return 0;
}
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Pure virtual function
 Output
Area of Rectangle: 35
Area of Triangle: 21

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Abstract class
 A base class having at least one pure virtual function is
known as abstract class.
 It is not used to create objects.
 It is used only to act as a base class to be inherited by
other classes.
 The pure virtual function must be defined by the class
which is derived from the abstract base class.

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual destructors
 Constructors can not be virtual.
 Some valid reasons are-
1. To create an object the constructor of the object class
must be of the same type as the class.
2. When the constructors are called, virtual table is not
formed in memory to resolve any virtual function
calls.
 Virtual destructors are feasible in C++ and use to avoid
the memory leak problem.

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Virtual destructors
 The order of calling of destructors in an inheritance
hierarchy is opposite to that of constructors.
 Example-
class A
{
public:
~A()
{
cout<<"Base class destructor";
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Virtual destructors
class B : public A
{
public:
~B()
{
cout<<"Derived class destructor\n";
}
};
int main()
{
A *ptr = new B();
delete ptr; //trigger the base class destructor
return 0;
}
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Virtual destructors
 Output-
Base class destructor
 To make sure derived class destructor must be called,
declare the base class destructor as virtual as below-
class A
{
public:
virtual ~A()
{
cout<<"Base class destructor";
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer
Virtual destructors
 Output-
Derived class destructor
Base class destructor

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer
Thank You

Prepared by: Anil Kumar Tailor, Assistant Prof.


Engineering College Ajmer

You might also like