0% found this document useful (0 votes)
16 views17 pages

Module - 3 C++

C++
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)
16 views17 pages

Module - 3 C++

C++
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/ 17

Module -3

The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
 Sub Class: The class that inherits properties from another class is called Subclass or Derived
Class.
 Super Class: The class whose properties are inherited by a subclass is called Base Class or
Superclass.
 Why and when to use inheritance?
 Consider a group of vehicles. You need to create classes for Bus, Car, and Truck. The methods
fuelAmount(), capacity(), applyBrakes() will be the same for all three classes. If we create
these classes avoiding inheritance then we have to write all of these functions in each of the
three classes as shown below figure:

Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
Example:
1. class ABC : private XYZ //private derivation
{ }
2. class ABC : public XYZ //public derivation
{ }
3. class ABC : protected XYZ //protected derivation
{ }
4. class ABC: XYZ //private derivation by default
{ }
// Example: define member function without argument within
// the class

#include <iostream>
using namespace std;
class Person {
int id;
char name[100];

public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}

void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};

class Student : private Person {


char course[50];
int fee;

public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}

void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};

int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Output: Enter the Id: 101
Enter the Name: Dev
Enter the Course Name: GCS
Enter the Course Fee:70000

Id: 101
Name: Dev
Course: GCS
Fee: 70000
Modes of Inheritance: There are 3 modes of inheritance.
1. Public Mode: If we derive a subclass from a public base class. Then the
public member of the base class will become public in the derived class
and protected members of the base class will become protected in the
derived class.
2. Protected Mode: If we derive a subclass from a Protected base class.
Then both public members and protected members of the base class will
become protected in the derived class.
3. Private Mode: If we derive a subclass from a Private base class. Then
both public members and protected members of the base class will
become Private in the derived class.

Types Of Inheritance:-
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Types of Inheritance in C++
1. Single Inheritance: In single inheritance, a class is allowed to inherit from
only one class. i.e. one subclass is inherited by one base class only.

class subclass_name : access_mode base_class


{
// body of subclass
};
OR
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
// C++ program to explain
// Single inheritance
#include<iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};

// sub class derived from a single base classes


class Car : public Vehicle {

};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output
This is a Vehicle
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class
can inherit from more than one class. i.e one subclass is inherited from
more than one base class.

class subclass_name : access_mode base_class1, access_mode


base_class2, ....
{
// body of subclass
};
class B
{
... .. ...
};
class C
{
... .. ...
};
class A: public B, public C
{
... ... ...
};
// C++ program to explain
// multiple inheritance
#include <iostream>
using namespace std;

// first base class


class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

// second base class


class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};

// sub class derived from two base classes


class Car : public Vehicle, public FourWheeler {
};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
Multilevel Inheritance: In this type of inheritance, a derived class is created
from another derived class.
Syntax:-
class C
{
... .. ...
};
class B:public C
{
... .. ...
};
class A: public B
{
... ... ...
};
// C++ program to implement
// Multilevel Inheritance
#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

// first sub_class derived from class vehicle


class fourWheeler : public Vehicle {
public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
// sub class derived from the derived base class fourWheeler
class Car : public fourWheeler {
public:
Car() { cout << "Car has 4 Wheels\n"; }
};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
Hierarchical Inheritance: In this type of inheritance, more than one
subclass is inherited from a single base class. i.e. more than one derived
class is created from a single base class.
Syntax:-
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.
}
// C++ program to implement
// Hierarchical Inheritance
#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// first sub class
class Car : public Vehicle {
};

// second sub class


class Bus : public Vehicle {
};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by
combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple
inheritances:
// C++ program for Hybrid Inheritance

#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};

// first sub class


class Car : public Vehicle {
};

// second sub class


class Bus : public Vehicle, public Fare {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
This is a Vehicle
Fare of Vehicle

C++ 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.

Types of Polymorphism
 Compile-time Polymorphism
 Runtime Polymorphism

1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator
overloading.
Function Overloading
When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is known
as Function Overloading.
// C++ program to demonstrate
// function overloading or
// Compile-time Polymorphism
#include <bits/stdc++.h>

using namespace std;


class Geeks {
public:
// Function with 1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}

// Function with same name but


// 1 double parameter
void func(double x)
{
cout << "value of x is " << x << endl;
}

// Function with same name and


// 2 int parameters
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y
<< endl;
}
};

// Driver code
int main()
{
Geeks obj1;

// Function being called depends


// on the parameters passed
// func() is called with int value
obj1.func(7);

// func() is called with double value


obj1.func(9.132);
// func() is called with 2 int values
obj1.func(85, 64);
return 0;
}
value of x is 7
value of x is 9.132
value of x and y is 85, 64
Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data
type, this ability is known as operator overloading. For example, we can
make use of the addition operator (+) for string class to concatenate two
strings. We know that the task of this operator is to add two operands. So a
single operator ‘+’, when placed between integer operands, adds them and
when placed between string operands, concatenates them.
2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding
and dynamic polymorphism are other names for runtime polymorphism. The
function call is resolved at runtime in runtime polymorphism.
. Function Overriding
Function Overriding occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to be
overridden.
Constructors in C++
Constructor in C++ is a special method that is invoked automatically at the
time of object creation. It is used to initialize the data members of new
objects generally. The constructor in C++ has the same name as the class or
structure. It constructs the values i.e. provides data for the object which is
why it is known as a constructor.
 Constructor is a member function of a class, whose name is the same as
the class name.
 Constructor is a special type of member function that is used to initialize
the data members for an object of a class automatically when an object of
the same class is created.
 Constructor is invoked at the time of object creation. It constructs the
values i.e. provides data for the object that is why it is known as a
constructor.
 Constructors do not return value, hence they do not have a return type.
Syntax
<class-name> (list-of-parameters);
Syntax for Defining the Constructor Within the Class
<class-name> (list-of-parameters)
{
// constructor definition
}
Syntax for Defining the Constructor Outside the Class
<class-name>: :<class-name>(list-of-parameters)
{
// constructor definition
}
Types of Constructor in C++
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor in C++
A default constructor is a constructor that doesn’t take any argument. It has no
parameters. It is also called a zero-argument constructor.
Syntax of Default Constructor
className() {
// body_of_constructor
}

Parameterized Constructor in C++


Parameterized Constructors make it possible to pass arguments to
constructors.
Syntax of Parameterized Constructor
className (parameters...) {
// body
}
Copy Constructor in C++
A copy constructor is a member function that initializes an object using
another object of the same class.
ClassName (ClassName &obj)
{
// body_containing_logic
}
Destructors in C++
A destructor is also a special member function as a constructor. Destructor
destroys the class objects created by the constructor. Destructor has the
same name as their class name preceded by a tilde (~) symbol.
Syntax of Destructors in C++
~ <class-name>(){}
The syntax for defining the destructor outside the class
<class-name>: : ~<class-name>(){}
#include <iostream>
using namespace std;

class Test {
public:
Test() { cout << "\n Constructor executed"; }

~Test() { cout << "\n Destructor executed"; }


};
main()
{
Test t;

return 0;
}
Constructor executed
Destructor executed

You might also like