void fun1(int x) { { void fun1(float x) { } void main() { fun1(10); fun1(1.5); } The feature of runtime polymorphism is the ability to refer to objects without regard to their classes using a single pointer. We can use a pointer to a base class to refer all the derived objects. When we use the same function name in both base and derived classes a base class pointer executes the base class function only even when is assigned with derived class object address.
Syntax: Virtual return-type function_name([parameters]) { statements; }
E.g:
# include<iostream.h>
class animal { public: virtual void walk() cout<<”\n Inside animal walk”; }};
class dog:public animal { public: void walk() { cout<<”\n Inside dog walk”; }};
class cat: public animal { public: void walk() { cou<<”\n Inside cat walk”; }};
void play(animal *a) { (*a).walk(); }
void main() { cat c; dog d; play(&c); play (&d); }
O/p: Inside cat walk Inside dog walk
Eg:
# include<iostream.h>
class SIM { public:
void connect( )
{ cout<<”Inside connect”; }};
class airelsim:public sim { Public: Void connect( ) { Cout<<”In connected to airtel network”; }};
class bsnlsim:public sim { Public: Void connect( ) { Cout<<”In connected to bsnl network”; }};
class mobile { Public: Static void insert(Sim *s) { (*s).connect( ); }};
void main( ) { Airtle sim airtel; bsnl sim bsnl; Mobile::insert(&airtel); Mobile::insert(&bsnl); }
o/p: Inside connect Inside connect
Syntax: Virtual return type function – name (parameters)
# include<iostream.h> # include<conio.h> class shape { protected; float dim 1,dim 2; public: void read_dim( ) { cout<<”Input two dim”; cin>>dim1>>dim2; } Virtual void final_area( )=o; }; Class triangle:public shape { Public: void final_area( ) { float area=0.5*dim1*dim2; cout<<”In area of triangle is”<<area; }}; class rectangle:public shape { public: void final_area( ) float area=dim1*dim2; cout<<”In area of rectangle is”<<area; }}; Void main( ) { Triangle t1; t1.read_dim( ); Rectangle r1; r1.read_dim( ); t1.final_area( ); r1.final_area( ); }
O/P: Input two dim 2 3 Input two dim5 4 Area of triangle is 3 Area of triangle is 20
If there are two on more types of inheritances to design a program it is called as hybrid inheritance. Ex: class student{ }; class test:public student { };class sports:public student { }; Class result:public test,public sports; This is also called as Multi path inheristance as the class student inherited vice test and also via sports
Class alpha { }; Class beta:public alpha { }
You might want to make a class if it is a base class it has been passed to more than one derived class,as might happen with multiple inheritance.
Eg: class B {….} Class D:B,B{….};//ILLEGAL However a base class can be indirectly passed to the derived class more than once:
Eg: Class X:public B {….} Class Y:public B {….} Class Z:public X,public Y{….} In this case, each object of class Z will have sub objects of class B. If this causes problems, you can add the key word” virtual “to a base class specifier.
For example:
# include<iostream.h>
Class A { Public: Void fun1( ) { Cout<<”In Inside function1 of A”; }};
Class B:public virtual A { public: Void fun2( ) { Cout<<”In Inside function2 of A”; }};
Class C:public virtual A { public: Void fun 3( ) { Cout<<”In Inside function3 of C”; }};
Class D:public B,public C { Public: Void fun4( ) { cout<<”In Inside function4 of D”; }};
Void main( ) { D obj1; Obj1.fun1( ); Obj1.fun2( ); Obj1.fun3( ); Obj1.fun4( ); }
o/p: Inside function 1 of A Inside function 2 of A Inside function 3 of C Inside function 4 of D
If class is derived from more than one base class it is called as multiple inheritance. Ex: class base1 { }; class base2 { }; class derived:public base1,public base2 { };
Example:
# include<iostream.h>
Class person { Protected; Char name[10]; Public; Void read_name( ) { Cout<<”\n Input name”; Cin>>name; }};
Class employee { Protected; Int empno; public; void read_empno( ) { Cout<<”\n Input empno”; cin>>empno; }};
Classworker:public person,public employee { Float wage; Public; Void read_wage( ) { Cout<<” \n Input wage”; Cin>>wage; }
Void print_details( ) { Cout<<”In Name”<<name; Cout<<”In Empno”<<empno; Cout<<”In Wage”<<wage; }};
Void main( ) { Worker worker1; Worker1.read_name( ); Worker1.read_empno( ); Worker1.read_wage( ); Worker1.print_details( );
}
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.