Object Slicing (1) Merged

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

• 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
•Binding
•Polymorphism
•Function Overloading
•Ambiguity in function overloading

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Binding
 Binding: The process of matching the function call with
the correct function definition by the compiler. There
are two types of bindings-
1. Static binding: By default, matching of function call
with the correct function definition happens at
compile time, is called static/early/compile-time
binding.
2. Dynamic binding: The compiler matches function calls
with the correct definition at the run time, is called
dynamic/late/run-time binding.

Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College


Ajmer
Polymorphism
 The ability to take more than one form is called
polymorphism.
 An operation may show different behaviors in different
instances.
 Behaviors depends on the type of data used in the
operation.
 For example, In addition operation, for two numbers it gives
sum and for two strings it gives concatenation.
 Two types of polymorphism-
1. Compile-time/static polymorphism
2. Run-time/dynamic polymorphism

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Types of Polymorphism

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
 The mechanism of using same function name to
create functions that perform different tasks, called
function overloading.
 Perform different tasks, depends on arguments list
in function call.
 Correct function to be invoked is determined by
checking the number and type of the arguments
but not on function return type.

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
 Example - an overloaded add() function
//Declaration
int add(int a, int b); //prototype 1
int add (int a, int b, int c); //prototype 2
double add(double x, double y); //prototype 3
double add(int p , double q); //prototype 4
double add(double p , int q); //prototype 5
//function call
cout<<add(5, 10); //uses prototype 1
cout<<add(15, 10.0); //uses prototype 4
cout<<add(12.5, 7.5); //uses prototype 3
cout<<add(5, 10, 15); //uses prototype 2
cout<<add(0.75, 5); //uses prototype 5
Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering
College Ajmer
Function overloading
 The function selection involves the following steps:-
1. Find an exact match in which the types of actual arguments are the
same.
2. Uses integral promotions to the actual arguments such as : char to int
,float to double.
3. Uses the following implicit type conversions to the actual arguments,
if multiple matches, then compiler will give error message.

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
//Program to overload sum() function
int sum(int, int);
int sum(int, int, int);
int main()
{
cout<<“Sum of two numbers is ”<<sum(5, 10);
cout<<“\n”;
cout<<“Sum of three numbers is ”<<sum(10, 20, 30);
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
int sum(int x, int y)
{
return(x+y);
}
int sum(int a, int b, int c)
{
return(a+b+c);
}
Output-
Sum of two numbers is 15
Sum of three numbers is 60

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
 When the compiler is unable to decide which function it
should invoke first among the overloaded functions, this
situation is known as function overloading ambiguity.
 The compiler does not run the program if it shows
ambiguity error.
 Causes of Function Overloading ambiguity:
1. Type Conversion.
2. Function with default arguments.
3. Function with a pass by reference

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
 Example-Type Conversion.
void function(float);
void function(int);
void function(float x)
{
cout << "Value of x is : " <<x;
}

void function(int y)
{
cout << "Value of y is : " <<y;
}

int main()
{
function(3.4);
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
 Example-Function with default arguments.

void function(int);
void function(int, int);
void function(int x)
{
cout << "Value of x is : " <<x;
}
void function(int y, int z=12)
{
cout << "Value of y is : " <<y;
cout << "Value of z is : " <<z;
}
int main()
{
function(10);
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
Example- Function with a pass by reference

void function(int);
void function(int &);
void function(int a)
{
cout << "Value of a is : " <<a;
}
void function(int &b)
{
cout << "Value of b is : " <<b;
}

int main()
{
int x=10;
function(x);
return 0;
}

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