KEMBAR78
Inheritance Notes | PDF | Inheritance (Object Oriented Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
6 views13 pages

Inheritance Notes

C++ inheritance allows one class (derived class) to inherit properties and behaviors from another class (base class), enabling code reuse and extension. There are five types of inheritance in C++: single, multiple, hierarchical, multilevel, and hybrid. The document provides syntax examples and explanations for each type of inheritance, including access specifiers (private, public, protected) and their implications on member accessibility.

Uploaded by

magarratna471
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Inheritance Notes

C++ inheritance allows one class (derived class) to inherit properties and behaviors from another class (base class), enabling code reuse and extension. There are five types of inheritance in C++: single, multiple, hierarchical, multilevel, and hybrid. The document provides syntax examples and explanations for each type of inheritance, including access specifiers (private, public, protected) and their implications on member accessibility.

Uploaded by

magarratna471
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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.

Types of Inheritance

C++ supports five types of inheritance:

o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance

Derived Classes

A Derived class is defined as the class derived from the base class.

The Syntax of Derived class:


1. class derived_class_name :: visibility-mode base_class_name
2. {
3. // body of the derived class.
4. }

Where,

derived_class_name: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the features of the base class are publicly
inherited or privately inherited. It can be public or private.

base_class_name: It is the name of the base class.

o When the base class is privately inherited by the derived class, public members of the
base class becomes the private members of the derived class. Therefore, the public
members of the base class are not accessible by the objects of the derived class only by
the member functions of the derived class.
o When the base class is publicly inherited by the derived class, public members of the base
class also become the public members of the derived class. Therefore, the public
members of the base class are accessible by the objects of the derived class as well as by
the member functions of the base class.
Private Public Protected

Only the functions inside the The participants of the The base class and its derived classes
base class can access class class that have been allow access to class members that have
members that have been made public are been designated as protected.
designated as private. accessible from
anywhere.

The keyword private is used to The keyword public is Members that are protected are
declare private members, used to declare members, identified by using the word protected
followed by the colon (:) followed by the colon (:) and the colon (:) character.
symbol. symbol.

Private members are not In a class, public In a class, protected members can be
passed down across classes. members may be inherited.
inherited.

It offers its data members the It offers no security at all Compared to private access specifier, it
highest level of protection. for the data it holds. offers less security.

A friend function can be used Access to public members Protected members are inaccessible
to access private members. is possible both with and outside the class and cannot be accessed
without the aid of the by the friend function, but they may be
friend feature. accessed by any subclass (derived class)
of that class.

class Base
{
public:
int x;
protected:
int y;

private:
int z;

};

class PublicDerived: public Base {

// x is accessible in the PublicDerived class and is public.

// y is accessible and protected in the PublicDerived class.

// Since z belongs to the base class as a private member, it is not accessible through
PublicDerived.

};

class ProtectedDerived: protected Base {

// In the ProtectedDerived class, x is accessible and protected.

// y is accessible and protected in the ProtectedDerived class.

// Due to the fact that z is a private member of the base class, it is not available
via ProtectedDerived.

};

class PrivateDerived: private Base {

// Only the member functions of the PrivateDerived class have access to the privat
e variable x.
// Only the member functions of the PrivateDerived class have access to the private
variable y.

// Given that z is a private member of the base class, it is not available via Privat
eDerived.
};

C++ Single Inheritance

Single inheritance is defined as the inheritance in which a derived class is inherited from the
only one base class.

#include <iostream>
using namespace std;
class Account
{
public:
float salary = 60000;
};
class Programmer: public Account
{
public:
float bonus = 5000;
};
int main()
{
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
#include <iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};

class B : private A
{
public:
void display()
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();

return 0;
}

MULTILEVEL INHERITANCE
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};

// Derived class (child)


class MyChild: public MyClass {
};

// Derived class (grandchild)


class MyGrandChild: public MyChild {
};
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}

#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}

MULTIPLE INHERITANCE
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};

// Another base class


class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};

// Derived class
class MyChildClass: public MyClass, public MyOtherClass
{
};

int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
}
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};

class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A, public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();

return 0;
}
Hybrid Inheritance
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};

class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};

class D : public B, public C


{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Hierarchical Inheritance
#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <<m<< std::endl;
std::cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}

You might also like