Unit II
Unit II
Inheritance
Inheritance is a mechanism of acquiring the features and behaviors of a class by another class. Sub
Class: The class that inherits properties from another class is called Sub class or Derived Class. Super
Class:The class whose properties are inherited by sub class is called Base Class or Super class.
1. Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e.
one sub class is inherited by one base class only.
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from
more than one classes. i.e one sub class is inherited from more than one base classes.
3. Multilevel Inheritance: In this type of inheritance, a derived class is created from another
derived class.
4. Hierarchical Inheritance: In this type of inheritance, more than one sub class is inherited from
a single base class. i.e. more than one derived class is created from a single base class.
5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one
type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
Public Inheritance
General syntax
When public access specifier is specified, the public members of the base class are inherited as public
while protected members are protected. Private members remain private. This is the most popular mode
of inheritance.
Private Inheritance
General Syntax
Protected Inheritance
General Syntax
When protected access specifier is used, public and protected members of the base class become
protected members in the derived class.
Note that when we use private access specifier for the base class, none of the base class members are
inherited. They all become private in the derived class.
3. Public mode:
When we inherit a derived class from a base class with public visibility mode, the public members and
protected members of the base class will be inherited as public members and protected members
respectively of the derived class.
#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : public X{
//As the visibility mode is public the protected members of class X becomes protected member
for class Y and public members of class X becomes public member for class Y
//protected: int b; inherited from class X
//public: int c; inherited from class X
};
int main()
{
//Only int c can be accessed in main function using Class Y object as it is public;
Y obj;
std::cout<<obj.c;
return 0;
}
Single inheritance
#include<iostream>
using namespace std;
class person
{
protected:
char name[10];
int age;
public:
void getinfo()
{
cout<<"\n enter data: name and age";
cin>>name>>age;
}
void showinfo()
{
cout<<"\n name"<< name<< "\n name"<<age;
}
};
public:
void totalsalary()
{
getinfo();
showinfo();
cout<< "enter salary";
cin>>salary;
cout<<"\n Salary"<< salary;
}
};
int main()
{
employee e;
e.totalsalary();
return 0;
}
----------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
class student
{
int rno;
char name[10];
public:
void getdata()
{
cout<<"\n enter data: rno and name\n";
cin>>rno>>name;
}
void showdata()
{
cout<<"\ndata of student is\n";
cout<<rno<<"\t"<<name;
}
};
public:
void inputmark()
{
getdata();
showdata();
cout<<"\nenter marks of stud\n";
cin>>mark1>>mark2;
}
void total()
{
sum=mark1+mark2;
}
void display()
{
cout<<"\ntotal marks of student :"<<sum;
}
};
int main()
{
studmark s;
s.inputmark();
s.total();
s.display();
return 0;
}
Multiple Inheritance
#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
char name[10];
int marks;
public:
void getdata()
{
cout<<"enter rollno, name and marks";
cin >>rollno>>name>>marks;
}
void showmarks()
{
cout<<"\rollno"<<rollno<<"name"<<name<<"marks"<<marks;
}
};
class sports
{
protected:
int score;
public:
void getscore()
{
cout<<"enter score";
cin >>score;
}
void showscore()
{
cout<<"\nscore is\t"<<score;
}
};
class result: public student,public sports
{
int total;
public:
void totaldisplay()
{
total=marks+score;
cout<<"\nResult is\t"<<total;
}
};
int main()
{
result s;
s.getdata();
s.getscore();
s.showmarks();
s.showscore();
s.totaldisplay();
return 0;
}
Multilevel
#include<iostream>
using namespace std;
class student
{
int rno;
char name[10];
public:
void getdata()
{
cout<<"\n enter data: rno and name\n";
cin>>rno>>name;
}
void showdata()
{
cout<<"\ndata of student is\n";
cout<<rno<<"\t"<<name;
}
};
class studmark : public student
{
int mark[5];
int sum,i;
float avg;
public:
void inputmark()
{
cout<<"\nenter marks of stud\n";
int i;
for(i=0;i<5;i++)
{
cin>>mark[i];
}
}
void total()
{
sum=0;
avg=0.0;
for(i=0;i<5;i++)
{
sum=sum+mark[i];
}
avg=sum/5;
int main()
{
studrecord s;
s.display();
return 0;
}
Hierarchical
#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
char name[10];
int marks;
public:
void getdata()
{
cout<<"enter rollno, name";
cin >>rollno>>name;
}
void showdata()
{
cout<<"\rollno"<<rollno<<"name"<<name;
}
};
public:
void getscore()
{
cout<<"enter score";
cin >>score;
}
void showscore()
{
cout<<"\nscore is\t"<<score;
}
};
public:
void getmarks()
{
cout<<"enter marks";
cin >>m1;
}
void showmarks()
{
cout<<"marks \t"<<m1;
}
};
int main()
{
sports s;
studmarks m;
s.getdata();
s.showdata();
s.getscore();
s.showscore();
m.getdata();
m.showdata();
m.getmarks();
m.showmarks();
return 0;
}
Hybrid Inheritance
#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
char name[10];
public:
void getdata()
{
cout<<"enter roll no and name";
cin >>rollno>>name;
}
void showdata()
{
cout<<" rollno "<<rollno<< "name"<<name;
}
};
public:
void getmarks()
{
cout<<"enter marks";
cin >>m1>>m2;
}
void showmarks()
{
cout<<"\nmarks are \t"<<m1<<m2;
}
};
public:
void getscore()
{
cout<<"enter score";
cin >>score;
}
void showscore()
{
cout<<"\nscore is\t"<<score;
}
};
public:
void totaldisplay()
{
//getdata();
//showdata();
getmarks();
showmarks();
getscore();
showscore();
total=m1+m2+score;
cout<<"\nTotal score is\t"<<total;
}
};
int main()
{
result s;
s.totaldisplay();
return 0;
}
Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of
a given class appearing in an inheritance hierarchy when using multiple inheritances.
s we can see from the figure that data members/function of class A are inherited twice to class D.
One through class B and second through class C. When any data / function member of class A is
accessed by an object of class D, ambiguity arises as to which data/function member would be
called? One inherited through B or the other inherited through C. This confuses compiler and it
displays error.
#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
char name[10];
public:
void getdata()
{
cout<<"enter roll no and name";
cin >>rollno>>name;
}
void showdata()
{
cout<<" rollno "<<rollno<< "name"<<name;
}
};
public:
void getmarks()
{
cout<<"enter marks";
cin >>m1>>m2;
}
void showmarks()
{
cout<<"\nmarks are \t"<<m1<<m2;
}
};
public:
void getscore()
{
cout<<"enter score";
cin >>score;
}
void showscore()
{
cout<<"\nscore is\t"<<score;
}
};
public:
void totaldisplay()
{
getdata();
showdata();
getmarks();
showmarks();
getscore();
showscore();
total=m1+m2+score;
cout<<"\nTotal score is\t"<<total;
}
};
int main()
{
result s;
s.totaldisplay();
return 0;
}
Multiple Inheritance: Multiple inheritance occurs when a class inherits attributes and methods
from more than one base class. This can lead to ambiguity when the derived class inherits
methods or attributes that have the same name from multiple base classes.
For example, if you have two base classes A and B, and a derived class C inherits from both A
and B, and both A and B have a method called foo(), calling C.foo() could be ambiguous
because it's not clear which foo() method should be called.
● C++: C++ allows multiple inheritance but does not provide automatic resolution for naming
conflicts. Developers need to explicitly specify which base class's method they want to call in
case of ambiguity.
For example, consider classes A, B, C, and D, where A is the base class and both B and C inherit
from A. Now, if D inherits from both B and C, it indirectly inherits from A through two different
paths. This can cause issues related to memory usage, method calls, and attribute access if not
managed properly.
Handling multipath inheritance requires careful design and may involve techniques like virtual
inheritance (available in C++) or similar mechanisms in other languages to ensure that there's
only one instance of the common base class shared among the intermediate classes.
In conclusion, both multiple inheritance and multipath inheritance can introduce ambiguity and
complexity in object-oriented programming. Language-specific mechanisms and design practices
are used to manage these situations effectively.
1. Constructors in Inheritance:
● Base Class Constructor: When an object of a derived class is created, the constructor of
the base class is called first. This is essential to properly initialize any attributes or
behaviors inherited from the base class. The derived class constructor can explicitly or
implicitly call the base class constructor using the constructor initialization list or using
the derived class constructor body.
● Derived Class Constructor: After the base class constructor is called, the constructor of
the derived class is executed.
class Base {
public:
Base() {
};
public:
Derived() {
};
int main() {
Derived d;
return 0;
O/P
Base Constructor
Derived Constructor
2. Destructors in Inheritance:
● Derived Class Destructor: When an object goes out of scope or is explicitly deleted, the
destructor of the derived class is called first. This allows you to perform cleanup
operations specific to the derived class.
● Base Class Destructor: After the derived class destructor is called, the destructor of the
base class is executed. This is crucial for releasing any resources allocated by the base
class.
class Base {
public:
Base() {
~Base() {
};
class Derived : public Base {
public:
Derived() {
~Derived() {
};
int main() {
Derived d;
return 0;
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
Case Study: Know about Firefox and Thunderbird as one of the popular softwares
developed using C++
Both Mozilla Firefox and Mozilla Thunderbird are popular open-source software applications
developed by the Mozilla Foundation. They are known for their functionality, performance, and
extensive use of the C++ programming language.
1. Mozilla Firefox:
● Description: Mozilla Firefox is a widely used web browser known for its speed, security
features, and customizable user interface. It aims to provide a fast and private browsing
experience for users while adhering to web standards.
● Programming Language: Firefox is primarily developed using C++, along with other
languages like JavaScript, CSS, and HTML. C++ is used for various core components
and performance-critical parts of the browser.
● Features of C++ Usage:
● The rendering engine of Firefox, called Gecko, is written in C++ and handles
tasks like parsing HTML, rendering web pages, and managing the user interface.
● Performance-critical code, such as the JavaScript engine, is often implemented in
C++ to ensure fast execution of web applications.
● C++ is used to manage memory efficiently, which is important for a complex
application like a web browser.
2. Mozilla Thunderbird:
● Description: Mozilla Thunderbird is an email client and news client that provides
features for managing email accounts, organizing messages, and reading news feeds. It is
designed to be a user-friendly and extensible email application.
● Programming Language: Similar to Firefox, Thunderbird is developed using C++ along
with other web technologies.
● Features of C++ Usage:
● C++ is used to build the core components of Thunderbird, including the
mail-handling functionality and user interface elements.
● Thunderbird relies on C++ to efficiently manage email storage, networking, and
handling different email protocols.
● C++ helps Thunderbird achieve a balance between performance and functionality,
allowing users to manage large volumes of emails efficiently.
Both Firefox and Thunderbird are part of the Mozilla project, which emphasizes open-source
development, user privacy, and adherence to web standards. These applications demonstrate the
power and flexibility of C++ in building complex and feature-rich software. The use of C++
enables them to deliver high performance, efficient memory management, and the ability to work
across multiple platforms.
It's worth noting that while C++ plays a significant role in the development of these applications,
they also use other languages and technologies to provide a complete and seamless user
experience.