0% found this document useful (0 votes)
4 views

Inheritance

C++ inheritance allows one class to acquire properties and behaviors of another class. There are different types of inheritance in C++ including single, multilevel, multiple and hybrid inheritance. Inheritance provides code reusability and is useful for managing relationships between classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Inheritance

C++ inheritance allows one class to acquire properties and behaviors of another class. There are different types of inheritance in C++ including single, multilevel, multiple and hybrid inheritance. Inheritance provides code reusability and is useful for managing relationships between classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ Inheritance

In C++, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such way, you can reuse, extend or
modify the attributes and behaviors which are defined in other class.
In C++, the class which inherits the members of another class is called derived class
and the class whose members are inherited is called base class. The derived class is
the specialized class for the base class.

Advantage of C++ Inheritance


Code reusability: Now you can reuse the members of your parent class. So, there is
no need to define the member again. So less code is required in the class.
Types Of Inheritance
C++ Single Inheritance #include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Multiplication of a and b is : 20
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0; 20
}
#include <iostream> class B : private A
using namespace std; {
class A };
int main()
{
{
int a = 4; B b;
int b = 5; b.mul();
public:
void mul() return 0;
{ }
int c = a*b;
cout <<"Multiplication of a and b is : "<<c<< endl;
//return c;
}
};
ERROR
C++ Multilevel Inheritance
#include <iostream> class BabyDog: public Dog
using namespace std; {
class Animal { public:
public: void weep() {
void eat() { cout<<"Weeping...";
cout<<"Eating..."<<endl; }
} };
}; int main(void) {
class Dog: public Animal BabyDog d1;
{ d1.eat();
public: d1.bark();
void bark(){ d1.weep();
cout<<"Barking..."<<endl; return 0;
} }
};
C++ Multiple Inheritance
C++ Multiple Inheritance
class C : public A,public B
{
#include <iostream> public:
using namespace std; void display()
class A {
class B std::cout << "The value of a is : " <<a<< std::endl;
{
{ std::cout << "The value of b is : " <<b<< std::endl;
protected: cout<<"Addition of a and b is : "<<a+b;
protected:
int a; }
int b; };
public:
public: int main()
void get_a(int n) {
void get_b(int n)
{ C c;
{ c.get_a(10);
a = n;
b = n; c.get_b(20);
}
} c.display();
};
};
return 0;
}
Ambiquity Resolution in Inheritance
#include <iostream>
using namespace std; class C : public A, public B
class A {
{ void view()
public: {
void display()
{
display();
std::cout << "Class A" << std::endl; }
} };
}; int main()
class B {
{ C c;
public: c.display();
void display() return 0;
{ }
std::cout << "Class B" << std::endl;
}
};
C++ Hybrid Inheritance 1.#include <iostream>
2.using namespace std;
3.class A
4.{
5. protected:
6. int a;
7. public:
8. void get_a()
9. {
10. std::cout << "Enter the value of 'a' : " << std::endl;
11. cin>>a;
12. }
13.};
1.class B : public A
2.{
3. protected:
4. int b;
5. public:
6. void get_b()
7. {
8. std::cout << "Enter the value of 'b' : " << std::endl
;
9. cin>>b;
10. } 1.class C
11.}; 2.{
3. protected:
4. int c;
5. public:
6. void get_c()
7. {
8. std::cout << "Enter the value of c is : " << std::endl;
9. cin>>c;
10. }
11.};
1.class D : public B, public C
2.{
3. protected:
4. int d;
5. public:
6. void mul()
7. {
8. get_a();
9. get_b();
10. get_c();
11. std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
12. }
13.};
1.int main()
2.{
3. D d;
4. d.mul();
5. return 0;
6.}
C++ Hierarchical Inheritance 1.#include <iostream>
2.using namespace std;
3.class Shape // Declaration of base class.
4.{
5. public:
6. int a;
7. int b;
8. void get_data(int n,int m)
9. {
10. a= n;
11. b = m;
12. }
13.};
1.class Rectangle : public Shape // inheriting Shape class
2.{
3. public:
4. int rect_area()
5. {
6. int result = a*b;
7. return result;
8. }
9.};

1.class Triangle : public Shape // inheriting Shape class


2.{
3. public:
4. int triangle_area()
5. {
6. float result = 0.5*a*b;
7. return result;
8. }
9.};
1.int main()
2.{
3. Rectangle r;
4. Triangle t;
5. int length,breadth,base,height;
6. std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
7. cin>>length>>breadth;
8. r.get_data(length,breadth);
9. int m = r.rect_area();
10. std::cout << "Area of the rectangle is : " <<m<< std::endl;
11. std::cout << "Enter the base and height of the triangle: " << std::endl;
12. cin>>base>>height;
13. t.get_data(base,height);
14. float n = t.triangle_area();
15. std::cout <<"Area of the triangle is : " << n<<std::endl;
16. return 0;
17.}

You might also like