0% found this document useful (0 votes)
21 views22 pages

Unit II

The document discusses different types of inheritance in C++ including single, multiple, multilevel, hierarchical and hybrid inheritance. It provides syntax and code examples for each type of inheritance to demonstrate how it can be implemented.

Uploaded by

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

Unit II

The document discusses different types of inheritance in C++ including single, multiple, multilevel, hierarchical and hybrid inheritance. It provides syntax and code examples for each type of inheritance to demonstrate how it can be implemented.

Uploaded by

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

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

class sub_class : public parent_class

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

class sub_class : parent_class


Private inheritance does not inherit anything. When private access specifier is used, public and
protected members of the base class also become private.

Protected Inheritance

General Syntax

class sub_class:protected parent_class

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.

What is the visibility mode


In inheritance whenever the derived class inherits from the base class than which of the member of the
parent class can be accessible in the child class is controlled by the visibility mode. By default visibility
mode is always set to private.
Syntax is:
class derived_class_name :: visibility_mode base_class_name
{
//Lines of code
}
Types of Visibility Mode in C++
There are total 3 types of visibility mode in C++ that are:
● Private visibility mode,
● Protected visibility mode,
● Public visibility mode
1. Private visibility mode:
When we inherit a derived class from the base class with private visibility mode then the public and
protected members of the base class become private members of the derived class.
#include <iostream> //As the visibility mode is private none of the
class X{ member of base class is inherited to derived
private: class
int a; };
protected: int main()
int b; {
public: //Nothing can be accessed using Class Y object
int c; because there are no members in class Y.
}; return 0;
class Y : private X{ }

2. Protected visibility mode:


When we inherit a derived class from a base class with protected visibility mode the protected and
public members of the base class become protected members of the derived class
#include <iostream> //protected: int b,int c inherited from class X
class X{ };
private: int main()
int a; {
protected: //As the members in the class Y are protected
int b; they cannot be accessed in main using Class Y
public: object.
int c; return 0;
}; }
class Y : protected X{
//As the visibility mode is protected the
protected and public members of class X
becomes the protected members of Class Y

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;
}

};

class employee: public person


{
private:
int empid;
int salary;

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;
}
};

class studmark : private student


{
int mark1,mark2,sum;

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;

cout<<"\ntotal marks of student :"<<sum;


cout<<"\naverage marks of student :"<<avg;
}
};

class studrecord: public studmark


{
public:
void display()
{
getdata();
inputmark();
showdata();
total();
}
};

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;
}
};

class sports: public student


{
protected:
int score;

public:

void getscore()
{
cout<<"enter score";
cin >>score;
}

void showscore()
{
cout<<"\nscore is\t"<<score;
}
};

class studmarks: public student


{
int m1;

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;
}
};

class test: public student


{
protected:
int m1,m2;

public:
void getmarks()
{
cout<<"enter marks";
cin >>m1>>m2;
}

void showmarks()
{
cout<<"\nmarks are \t"<<m1<<m2;
}
};

class sport: public student


{
protected:
int score;

public:
void getscore()
{
cout<<"enter score";
cin >>score;
}

void showscore()
{
cout<<"\nscore is\t"<<score;
}
};

class result: public test,public sport


{
int total;

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

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;
}
};

class test: virtual public student


{
protected:
int m1,m2;

public:
void getmarks()
{
cout<<"enter marks";
cin >>m1>>m2;
}

void showmarks()
{
cout<<"\nmarks are \t"<<m1<<m2;
}
};

class sport: virtual public student


{
protected:
int score;

public:
void getscore()
{
cout<<"enter score";
cin >>score;
}

void showscore()
{
cout<<"\nscore is\t"<<score;
}
};

class result: public test,public sport


{
int total;

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;
}

Ambiguity in multiple inheritance & multipath inheritance

Multiple inheritance and multipath inheritance are concepts in object-oriented programming


(OOP) that deal with situations where a class inherits from multiple base classes. These concepts
can introduce ambiguity in certain scenarios, and they are typically handled differently in
different programming languages

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.

To address this ambiguity, some programming languages provide mechanisms to disambiguate


method calls. For instance:

● 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.

2. Multipath Inheritance: Multipath inheritance refers to a scenario where a class indirectly


inherits from a common base class through different paths in the inheritance hierarchy. This can
lead to situations where the same base class's methods or attributes are inherited multiple times
through different intermediate classes.

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() {

cout << "Base Constructor" << endl;

};

class Derived : public Base {

public:

Derived() {

cout << "Derived Constructor" << endl;

};
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.

Here's a continuation of the previous C++ example:

class Base {

public:

Base() {

cout << "Base Constructor" << endl;

~Base() {

cout << "Base Destructor" << endl;

};
class Derived : public Base {

public:

Derived() {

cout << "Derived Constructor" << endl;

~Derived() {

cout << "Derived Destructor" << endl;

};

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.

You might also like