Table of Contents
hide
Example : A C++ program to show basic Inheritance concepts.
#include <iostream>
using namespace std;
class A
{
public:
int x=50;
};
class B : public A
{
public:
int y=100;
};
int main()
{
A a;
B b;
cout<<a.x<<endl;
//cout<<a.y<<endl; Error class A has no member named Y
cout<<b.x<<endl;
cout<<b.y<<endl;
return 0;
}
Example : A C++ program to show Single Inheritance (public-public type).
#include <iostream>
using namespace std;
class A
{
public:
int x;
};
class B : public A // Public derivation of Public members of base class
{
public:
int y;
};
int main()
{
B b;
b.x=20;
b.y=30;
cout<<b.x<<endl;
cout<<b.y;
return 0;
}
Output :
20
30
Example : A C++ program to show Single Inheritance (public-private type).
#include <iostream>
using namespace std;
class A
{
private:
int x;
public:
void display1()
{
x=20;
cout<<x;
}
};
class B : public A // Public derivation of Private members of base class
{
public:
int y;
void display2()
{
y=30;
cout<<y;
}
};
int main()
{
B b;
b.display1();
b.display2();
return 0;
}
Example : A C++ program to show Single Inheritance (private-public type).
#include <iostream>
using namespace std;
class A
{
public:
int x;
};
class B : private A // Private derivation of Public members of base class
{
public:
int y;
public:
void display2()
{
x=50;
y=30;
cout<<x<<endl;
cout<<y;
}
};
int main()
{
B b;
b.display2();
return 0;
}
Output :
50
30
Example : A C++ program to show single Inheritance (private-protected).
#include <iostream>
using namespace std;
class A
{
protected:
int x;
};
class B : private A // Private derivation of Protected members of base class
{
public:
int y;
public:
void display2()
{
x=50;
y=30;
cout<<x<<endl;
cout<<y;
}
};
int main()
{
B b;
b.display2();
return 0;
}
Output :
50
30
NB : Similar as 'Private derivation of Public members' derivation but for other class (non-inherited class) object it behaves like as private.
Example : A C++ program to show single Inheritance (private-private).
#include <iostream>
using namespace std;
class A
{
private:
int x;
public:
void display1()
{
x=20;
cout<<x<<endl;
}
};
class B : private A // Private derivation of Private members of base class
{
public:
int y;
public:
void display2()
{
display1();
y=30;
cout<<y;
}
};
int main()
{
B b;
b.display2();
return 0;
}
Output :
20
30
Example : A C++ program to show Multilevel Inheritance.
#include <iostream>
using namespace std;
class A
{
public:
void display1()
{
int a=10;
cout<<a<<endl;
}
};
class B : public A
{
public:
void display2()
{
int b=20;
cout<<b<<endl;
}
};
class C : public B
{
public:
void display3()
{
int c=30;
cout<<c<<endl;
}
};
int main()
{
C c;
c.display1();
c.display2();
c.display3();
return 0;
}
Output :
10
20
30
--------- OR ----------
#include <iostream>
using namespace std;
class A
{
public:
void display1()
{
int a=10;
cout<<a<<endl;
}
};
class B : public A
{
public:
void display2()
{
int b=20;
cout<<b<<endl;
}
};
class C : public B
{
public:
void display3()
{
int c=30;
cout<<c<<endl;
}
};
int main()
{
B x ;
C y;
x.display1();
x.display2();
cout<<"\n";
y.display1();
y.display2();
y.display3();
return 0;
}
Output :
10
20
10
20
30
Example : A C++ program to show Multiple Inheritance.
#include <iostream>
using namespace std;
class A
{
public:
int x1=100;
void display1()
{
int a=10;
cout<<a<<" "<<x1<<endl;
}
};
class B
{
public:
int x2=200;
void display2()
{
int b=20;
cout<<b<<" "<<x2<<endl;
}
};
class C
{
public:
int x3=300;
void display3()
{
int c=30;
cout<<c<<" "<<x3<<endl;
}
};
class D : public A,B,C //public A, public B, public C, public D
{
public:
int x4=400;
void display4()
{
int d=40;
display1();
display2();
display3();
//child class object can call only through function, not directly to the variable
//cout<<a<<" "<<x1<<endl; - X Error generated
//cout<<b<<" "<<x2<<endl; - X Error generated
//cout<<c<<" "<<x3<<endl; - X Error generated
cout<<d<<" "<<x4<<endl;
}
};
int main()
{
D d;
d.display4();
return 0;
}
Output :
10 100
20 200
30 300
40 400
Example : A C++ program to demonstrate the Ambiguity problem in Multiple Inheritance.
#include <iostream>
using namespace std;
class A
{
public:
int x1=100;
void display()
{
int a=10;
cout<<a<<" "<<x1<<endl;
}
};
class B
{
public:
int x2=200;
void display()
{
int b=20;
cout<<b<<" "<<x2<<endl;
}
};
class C
{
public:
int x3=300;
void display()
{
int c=30;
cout<<c<<" "<<x3<<endl;
}
};
class D : public A, public B, public C
{
public:
int x4=400;
void display()
{
int d=40;
cout<<d<<" "<<x4<<endl;
}
};
int main()
{
D d;
d.A::display();
d.B::display();
d.C::display();
cout<<endl;
d.display();
return 0;
}
Output:
10 100
20 200
30 300
40 400
Example : A C++ program to show Hierarchical Inheritance.
#include <iostream>
using namespace std;
class A
{
public:
int x1=100;
void display1()
{
int a=10;
cout<<a<<" "<<x1<<endl;
}
};
class B : public A
{
public:
int x2=200;
void display2()
{
int b=20;
cout<<b<<" "<<x2<<endl;
}
};
class C : public A
{
public:
int x3=300;
void display3()
{
int c=30;
cout<<c<<" "<<x3<<endl;
}
};
int main()
{
B b;
C c;
b.display1();
b.display2();
cout<<endl;
c.display1();
c.display3();
return 0;
}
Output :
10 100
20 200
10 100
30 300
Example : A C++ program to show Hybrid Inheritance.
#include <iostream>
using namespace std;
class A
{
public:
int x1=100;
void display1()
{
int a=10;
cout<<a<<" "<<x1<<endl;
}
};
class B : public A // Single Inheritance
{
public:
int x2=200;
void display2()
{
int b=20;
cout<<b<<" "<<x2<<endl;
}
};
class C
{
public:
int x3=300;
void display3()
{
int c=30;
cout<<c<<" "<<x3<<endl;
}
};
class D: public B,C //Multiple Inheritance
{
public:
int x4=400;
void display4()
{
display2();
display3();
int c=40;
cout<<c<<" "<<x4<<endl;
}
};
int main()
{
B b;
D d;
b.display1();
d.display4();
return 0;
}
Output :
10 100
20 200
10 100
30 300
0 Comments