OOP (CO1201) Unit-4 - Inheritance in C++
OOP (CO1201) Unit-4 - Inheritance in C++
Prepared By,
Ms.Anuja Gunale
Introduction
In C++, it is possible to inherit attributes and methods
from one class to another. We group the "inheritance
concept" into two categories:
For example, if the base class is MyClass and the derived class
is sample it is specified as:
2 Public members and variables are accessible from outside the class.
class MyClass
{ public:
MyClass(void) { x=0; }
void f(int n1)
{ x= n1*5;}
void output(void) { cout<<x; }
private:
int x;
};
Inheritance Example:
int main(void)
{ sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}
Class A
Class A
Class B Class C
Class D
Hybrid Inheritance class C
{
protected:
#include <iostream>
int c;
using namespace std;
public:
class A
void get_c()
{
{
protected:
cout << "Enter the value of c is : " << endl;
int a;
cin>>c;
public:
}
void get_a()
};
{
class D : public B, public C
cout << "Enter the value of 'a' : " <<
{
endl;
protected:
cin>>a;
int d;
}
public:
};
void mul()
{
class B : public A
get_a();
{
get_b();
protected:
get_c(); Output:
int b;
cout << "Multiplication of a,b,c is : " <<a*b*c<< endl; Enter the length and breadth
public:
} of a rectangle: 23 20
void get_b() Area of the rectangle is : 460
};
{ Enter the base and height of
int main()
cout << "Enter the value of 'b' : " < the triangle: 2 5
{
< endl; Area of the triangle is : 5
D d;
cin>>b;
d.mul();
}
return 0;
}
}
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C; // class object declaration.
cout<<C.add(10, 20)<<endl;
Output:
cout<<C.add(12, 20, 23);
30 55
return 0;
}
#include <iostream>
using namespace std;
Function Overriding class Animal
{
public:
If derived class defines same void eat()
function as defined in its base {
class, it is known as function cout<<"Eating...";
overriding in C++. }
It is used to achieve runtime };
polymorphism. class Dog: public Animal
{
It enables you to provide public:
specific implementation of the void eat()
function which is already {
provided by its base class. cout<<"Eating bread...";
}
};
int main(void)
{
Dog d = Dog(); Output:
d.eat(); Eating bread...
return 0;
}
Virtual Base Class
Example Without Virtual Base Class
In C++, a virtual base class is a type of cpp
base class that is specified using the class A
virtual keyword when it is inherited. This is {
particularly useful in the context of public: void show()
multiple inheritance,
where it helps solve the diamond
{
problem. std::cout << "Class A" << std::endl;
}
The Diamond Problem };
The diamond problem occurs when a class class B1 : public A {};
D inherits from two classes B1 and B2, class B2 : public A {};
which both inherit from the same base class D : public B1, public B2 {};
class A. int main()
This can lead to ambiguity, as D could end
up with two copies of A—one through B1
{
and one through B2. D d;
d.show(); // Error: Ambiguity, which "show()" method to call?
}
class A
{
public: void show()
{
std::cout << "Class A" << std::endl;
}
};
class B1 : public virtual A { };
class B2 : public virtual A { };
class D : public B1, public B2 { };
int main()
{
D d;
d.show(); // No ambiguity, single copy of A
}