Professional Documents
Culture Documents
Object Slicing (1) Merged
Object Slicing (1) Merged
Object Slicing (1) Merged
•Function overriding
•Virtual function
•pure virtual function
•Abstract class
•Virtual destructors
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
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;
}
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;
}
};
void function(int y)
{
cout << "Value of y is : " <<y;
}
int main()
{
function(3.4);
return 0;
}
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;
}
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;
}