Professional Documents
Culture Documents
•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;
}
};