C++ Unit 3
C++ Unit 3
2 marks
1. What is an Inheritance in C++?
• Inheritance is one of the key features of object-oriented programming in C++.
• It allows us to create a new class (derived class) from an existing class (base class).
• The derived class inherits the features from the base class and can have additional
features of its own.
• Derived class (child) - The class that inherits from another class
• Base class (parent) - The class being inherited from
};
Where 'A' is the base class, and 'B' is the derived class.
Syntax:
Syntax:
class A
... .. ...
};
class B: public A
... .. ...
};
Here, class B is derived from the base class A.
Example:
#include <iostream>
class A
public:
void displayA ()
};
class B : public A
public:
void displayB ()
};
int main ()
{
B obj;
obj.displayA();
obj.displayB();
return 0;
OutPut:
Multilevel Inheritance
If a class is derived from another derived class, then it is called multilevel inheritance.
So in C++ multilevel inheritance, a class has more than one parent class.
As shown in above block diagram, class C has class B and class A as parent classes. Depending
on the relation the level of inheritance can be extended to any level.
Syntax:
{
...........
};
...........
};
...........
};
Example:
#include <iostream>
class A
public:
void displayA ()
};
class B : public A
public:
void displayB ()
};
class C : public B
public:
void displayc ()
};
int main ()
C obj;
obj.displayA();
obj.displayB();
obj.displayc();
return 0;
}
Output:
Example 2:
#include <iostream>
class base
public:
int x;
void getdata()
};
public:
int y;
void readdata()
{
};
private:
int z;
public:
void indata()
void product()
};
int main()
a.readdata();
a.indata();
a.product();
return 0;
Output
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18
Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes from two
or more classes.
Syntax:
class A
{
..........
};
class B
...........
};
...........
};
Example 1:
#include <iostream>
class A
public:
void displayA ()
};
class B
public:
void displayB ()
};
public:
void displayc ()
};
int main ()
C obj;
obj.displayA();
obj.displayB();
obj.displayc();
return 0;
}
Output:
Example 2:
#include
class A
public:
int x;
void getx()
};
class B
public:
int y;
void gety()
{
};
public:
void sum()
};
int main()
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
Output
enter value of x: 5
enter value of y: 4
Sum = 9
Suppose, two base classes have a same function which is not overridden in derived class.
If you try to call the function using the object of the derived class, compiler shows error. It's
because compiler doesn't know which function to call. For example,
class base1 {
public:
};
class base2 {
};
int main() {
derived obj;
obj.someFunction() // Error!
This problem can be solved using the scope resolution function to specify which function to class
either base1or base2
int main() {
● In C++ hierarchical inheritance, the feature of the base class is inherited onto more than
one sub-class.
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D: public A
{
// body of class D.
}
Example:
#include <iostream>
using namespace std;
class A
public:
void displayA ()
};
class B: public A
public:
void displayB ()
};
class C : public A
public:
void displayc ()
};
int main ()
B obj;
C obj1;
obj.displayA();
obj.displayB();
obj1.displayA();
obj1.displayc();
return 0;
Output:
Example 2:
#include <iostream>
public:
int x, y;
void getdata()
};
public:
void product()
};
public:
void sum()
}
};
int main()
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
Output
Product= 6
Sum= 5
Explanation
In this example, there is only one base class A from which two class B and C are derived.
Both derived classes have their own members as well as base class members.
The product is calculated in the derived class B, whereas, the sum is calculated in the derived
class C but both use the values of x and y from the base class.
● Above block diagram shows the hybrid combination of single inheritance and multiple
inheritance.
● Hybrid inheritance is used in a situation where we need to apply more than one
inheritance in a program.
.........
};
class B : public A
{
..........
};
class C
...........
};
...........
};
As shown in block diagram class B is derived from class A which is single inheritance and then
Class D is inherited from B and class C which is multiple inheritance. So single inheritance and
multiple inheritance jointly results in hybrid inheritance.
#include <iostream>
class A
public:
int x;
};
class B : public A
public:
x = 10;
};
class C
public:
int y;
y = 4;
};
{
public:
void sum()
};
int main()
obj1.sum();
return 0;
} //end of program
Output
Sum= 14
Visibility modes
o Public: When the member is declared as public, it is accessible to all the functions of the
program.
o Private: When the member is declared as private, it is accessible within the class only.
o Protected: When the member is declared as protected, it is accessible within its own
class as well as the class immediately derived from it.
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
// x is public
// y is protected
// x is protected
// y is protected
};
// x is private
// y is private
};
#include <iostream>
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
int getPVT() {
return pvt;
};
public:
int getProt() {
return prot;
};
int main() {
PublicDerived object1;
return 0;
}
Output
Private = 1
Protected = 2
Public = 3
private protected
Accessibility public members
members members
● As 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.
Example:
#include <iostream>
class A {
public:
void show()
}
};
class B : public A {
};
class C : public A {
};
};
int main()
D object;
object.show();
Output:
Compile Errors:
return 0;
}
Output
a = 10
Explanation :
The class A has just one data member a which is public. This class is virtually
inherited in class B and class C. Now class B and class C use the virtual base class A
and no duplication of data member a is done; Classes B and C share a single copy of
the members in the virtual base class A.
Types of constructors
• Default constructor
• Parameterized constructor
• Overloaded constructor
• Constructor with default value
• Copy constructor
• Inline constructor
The prototype of the constructor looks like
<class-name> (list-of-parameters);
Constructor can be defined inside the class declaration or outside the class declaration
a. Syntax for defining the constructor within the class
<class-name>(list-of-parameters)
{
//constructor definition
}
b. Syntax for defining the constructor outside the class
<class-name>: :<class-name>(list-of-parameters)
{
//constructor definition
}
Here,
A2(): virtual base constructor
A1(): base constructor
S(): derived constructor
Example:
#include<iostream>
using namespace std;
class A1
{
public:
A1()
{
cout << "Constructor of the base class A1 \n";
}
};
class A2
{
public:
A2()
{
cout << "Constructor of the base class A2 \n";
}
};
// Driver code
int main()
{
S obj;
return 0;
}
Output
Constructor of the base class A2
Constructor of the base class A1
Constructor of the derived class S
Run-time Polymorphism
• The word “polymorphism” means having many forms.
• In simple words, we can define polymorphism as the ability of a message to be displayed
in more than one form.
• Polymorphism is considered one of the important features of Object-Oriented
Programming.
Types of Polymorphism
● Compile-time Polymorphism
● Runtime Polymorphism
Runtime Polymorphism
•Virtual functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for the function call.
• They are mainly used to achieve Runtime polymorphism.
• Functions are declared with a virtual keyword in a base class.
• The resolving of a function call is done at runtime.
Rules for Virtual Functions
The rules for the virtual functions in C++ are as follows:
#include <iostream>
using namespace std;
class base {
public:
virtual void print() { cout << "print base class\n"; }
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}
Output
print derived class
show base class
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}
Output: Derived Function
• Here, the same function print() is defined in both Base and Derived classes.
• So, when we call print() from the Derived object derived1, the print() from
Derived is executed by overriding the function in Base.
As we can see, the function was overridden because we called the function from an object
of the Derived class.
Had we called the print() function from an object of the Base class, the function would
not have been overridden.
We can also access the overridden function by using a pointer of the base class to point to
an object of the derived class and then calling the function from that pointer.
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1, derived2;
derived1.print();
Output:
Derived Function
Base Function
Here, this statement
derived2.Base::print();
accesses the print() function of the Base class.
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
Output
Base Function
In this program, we have created a pointer of Base type named ptr. This pointer points to
the Derived object derived1.
• A pure virtual function is a "do nothing" function. Here "do nothing" means that it
just provides the template, and derived class implements the function.
• It can be considered as an empty function means that the pure virtual function
does not have any definition relative to the base class.
• Programmers need to redefine the pure virtual function in the derived class as it
has no definition in the base class.
• A class having pure virtual function cannot be used to create direct objects of its
own. It means that the class is containing any pure virtual function then we cannot
create the object of that class.
• This type of class is known as an abstract class.
Syntax
Example:
#include <iostream>
using namespace std;
class Base {
public:
// pure virtual function
virtual void show() = 0;
};
int main(void)
{
// creating a pointer of type Base pointing to an object of type Derived
Base* bp = new Derived();
return 0;
}
Output: In Derived
The classes which are containing virtual The classes which are containing pure virtual
functions are not abstract classes. function are the abstract classes.
The base class that contains a virtual The base class that contains a pure virtual function
function can be instantiated. becomes an abstract class, and that cannot be
instantiated.
If the derived class will not redefine the If the derived class does not define the pure virtual
virtual function of the base class, then there function; it will not throw any error but the derived
will be no effect on the compilation. class becomes an abstract class.
All the derived classes may or may not All the derived classes must define the pure virtual
redefine the virtual function. function.
• Argument types
If the constructor for an abstract class calls a pure virtual function, either directly or indirectly,
the result is undefined.
However, constructors and destructors for abstract classes can call other member functions.
Example:
#include <iostream>
using namespace std;
class Class1
{
int a;
public:
virtual void func1() = 0;
void func2()
{
cout << "base class\n";
}
};
class Class2 : public Class1
{
public:
void func1()
{
cout << "func1 in derived class\n";
}
};
int main()
{
//Class1 b; //---------- > this line will cause an error
Class1 *b = new Class2();//pointer can be created, so this is correct
b -> func1();
b->func2();
}
Output:
base class
Code Explanation: Here, in the above function, Class1 is the base class, and as it has a pure
virtual function (func1), it has become an abstract class. Class2 is derived from the parent class
Class1. The func1 is defined in the derived class. In the main function, when we try to create an
object of type base class, we will get an error, as objects cannot be created for abstract class.
Whereas when we try to create a pointer of base class type, it will be created successfully, and
we can point it to the derived class. This pointer can be used to call the derived class function.