0% found this document useful (0 votes)
13 views42 pages

C++ Notes

notes for c programing

Uploaded by

binoymoitra16
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)
13 views42 pages

C++ Notes

notes for c programing

Uploaded by

binoymoitra16
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/ 42

Ambernath Branch

9370844446
C++ Notes
C++ Programming

 What is C++?
C++ is a general-purpose object-oriented programming (OOP) language,
developed by Bjarne Stroustrup in 1981, and is an extension of the C language. It is
therefore possible to code C++ in a "C style" or "object-oriented style."

 What is OOP’s?
Object-oriented programming (OOP) is a software programming model
constructed around objects. This model compartmentalizes data into objects (data
fields) and describes object contents and behavior through the declaration of classes
(methods).

 OOP’s Features:-
 Encapsulation: This makes the program structure easier to manage because each
object’s implementation and state are hidden behind well-defined boundaries.
 Polymorphism: This means abstract entities are implemented in multiple ways.
 Inheritance: This refers to the hierarchical arrangement of implementation
fragments.

 What is Class?

A class definition starts with the keyword class followed by the class name; and
the class body, enclosed by a pair of curly braces. A class definition must be followed
either by a semicolon or a list of declarations. For example, we defined the Box data
type using the keyword class as follows –

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
Ambernath Branch
9370844446
C++ Notes
};
The keyword public determines the access attributes of the members of the class
that follows it. A public member can be accessed from outside the class anywhere
within the scope of the class object. You can also specify the members of a class
as private or protected which we will discuss in a sub-section.

 What is Objects?
A class provides the blueprints for objects, so basically an object is created from a
class. We declare objects of a class with exactly the same sort of declaration that we
declare variables of basic types. Following statements declare two objects of class Box –

Box Box1; // Declare Box1 of type Box


Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.

 How to Access Data Members?


The public data members of objects of a class can be accessed using the direct
member access operator (.)

#include <iostream>

using namespace std;

class Box {

public:

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box


Ambernath Branch
9370844446
C++ Notes
};

int main() {

Box Box1; // Declare Box1 of type Box

Box Box2; // Declare Box2 of type Box

double volume = 0.0; // Store the volume of a box here

// box 1 specification

Box1.height = 5.0;

Box1.length = 6.0;

Box1.breadth = 7.0;

// box 2 specification

Box2.height = 10.0;

Box2.length = 12.0;

Box2.breadth = 13.0;

// volume of box 1

volume = Box1.height * Box1.length * Box1.breadth;

cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2

volume = Box2.height * Box2.length * Box2.breadth;


Ambernath Branch
9370844446
C++ Notes
cout << "Volume of Box2 : " << volume <<endl;

return 0;

When the above code is compiled and executed, it produces the following result −

Volume of Box1 : 210


Volume of Box2 : 1560
It is important to note that private and protected members can not be accessed
directly using direct member access operator (.). We will learn how private and
protected members can be accessed.

 What is Class Member Functions?


A member function of a class is a function that has its definition or its prototype
within the class definition like any other variable. It operates on any object of the class
of which it is a member, and has access to all the members of a class for that object.
Let us take previously defined class to access the members of the class using a
member function instead of directly accessing them –

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void);// Returns box volume
};
Member functions can be defined within the class definition or separately
using scope resolution operator, : −. Defining a member function within the class
definition declares the function inline, even if you do not use the inline specifier. So
either you can define Volume() function as below –

class Box {
public:
double length; // Length of a box
Ambernath Branch
9370844446
C++ Notes
double breadth; // Breadth of a box
double height; // Height of a box

double getVolume(void) {
return length * breadth * height;
}
};
If you like, you can define the same function outside the class using the scope
resolution operator (::) as follows −

double Box::getVolume(void) {
return length * breadth * height;
}
Here, only important point is that you would have to use class name just before ::
operator. A member function will be called using a dot operator (.) on a object where it
will manipulate data related to that object only as follows −

Box myBox; // Create an object

myBox.getVolume(); // Call member function for the object


Let us put above concepts to set and get the value of different class members in a class

#include <iostream>

using namespace std;

class Box {

public:

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box


Ambernath Branch
9370844446
C++ Notes

// Member functions declaration

double getVolume(void);

void setLength( double len );

void setBreadth( double bre );

void setHeight( double hei );

};

// Member functions definitions

double Box::getVolume(void) {

return length * breadth * height;

void Box::setLength( double len ) {

length = len;

void Box::setBreadth( double bre ) {

breadth = bre;

void Box::setHeight( double hei ) {

height = hei;

}
Ambernath Branch
9370844446
C++ Notes

// Main function for the program

int main() {

Box Box1; // Declare Box1 of type Box

Box Box2; // Declare Box2 of type Box

double volume = 0.0; // Store the volume of a box here

// box 1 specification

Box1.setLength(6.0);

Box1.setBreadth(7.0);

Box1.setHeight(5.0);

// box 2 specification

Box2.setLength(12.0);

Box2.setBreadth(13.0);

Box2.setHeight(10.0);

// volume of box 1

volume = Box1.getVolume();

cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2
Ambernath Branch
9370844446
C++ Notes
volume = Box2.getVolume();

cout << "Volume of Box2 : " << volume <<endl;

return 0;

When the above code is compiled and executed, it produces the following result −

Volume of Box1 : 210


Volume of Box2 : 1560

 Access Modifiers:-
There are three types of Access Modifiers- 1. Public
2. Private
3. Protected

Data hiding is one of the important features of Object Oriented Programming


which allows preventing the functions of a program to access directly the internal
representation of a class type. The access restriction to the class members is specified
by the labeled public, private, and protected sections within the class body. The
keywords public, private, and protected are called access specifiers.
A class can have multiple public, protected, or private labeled sections. Each
section remains in effect until either another section label or the closing right brace of
the class body is seen. The default access for members and classes is private.

class Base {
public:
// public members go here
protected:

// protected members go here


private:
// private members go here

};
Ambernath Branch
9370844446
C++ Notes

1. The Public Members:-


A public member is accessible from anywhere outside the class but within a
program. You can set and get the value of public variables without any member
function as shown in the following example −

#include <iostream>

using namespace std;

class Line {

public:

double length;

void setLength( double len );

double getLength( void );

};

// Member functions definitions

double Line::getLength(void) {

return length ;

void Line::setLength( double len) {

length = len;

}
Ambernath Branch
9370844446
C++ Notes

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

// set line length without member function

line.length = 10.0; // OK: because length is public

cout << "Length of line : " << line.length <<endl;

return 0;

When the above code is compiled and executed, it produces the following result −

Length of line : 6
Length of line : 10

2. The Private Members:-


A private member variable or function cannot be accessed, or even viewed from
outside the class. Only the class and friend functions can access private members.By
default all the members of a class would be private, for example in the following
class width is a private member, which means until you label a member, and it will be
assumed a private member –
Ambernath Branch
9370844446
C++ Notes

class Box {
double width;

public:
double length;
void setWidth( double wid );
double getWidth( void );
};
Practically, we define data in private section and related functions in public
section so that they can be called from outside of the class as shown in the following
program.

#include <iostream>

using namespace std;

class Box {

public:

double length;

void setWidth( double wid );

double getWidth( void );

private:

double width;

};

// Member functions definitions


Ambernath Branch
9370844446
C++ Notes
double Box::getWidth(void) {

return width ;

void Box::setWidth( double wid ) {

width = wid;

// Main function for the program

int main() {

Box box;

// set box length without member function

box.length = 10.0; // OK: because length is public

cout << "Length of box : " << box.length <<endl;

// set box width without member function

// box.width = 10.0; // Error: because width is private

box.setWidth(10.0); // Use member function to set it.

cout << "Width of box : " << box.getWidth() <<endl;

return 0;

}
Ambernath Branch
9370844446
C++ Notes
When the above code is compiled and executed, it produces the following result −

Length of box : 10
Width of box : 10

3. The Protected Members:-


A protected member variable or function is very similar to a private member but
it provided one additional benefit that they can be accessed in child classes which are
called derived classes.
You will learn derived classes and inheritance in next chapter. For now you can
check following example where I have derived one child class SmallBox from a parent
class Box.
Following example is similar to above example and here width member will be
accessible by any member function of its derived class SmallBox.

#include <iostream>

using namespace std;

class Box {

protected:

double width;

};

class SmallBox:Box { // SmallBox is the derived class.

public:

void setSmallWidth( double wid );

double getSmallWidth( void );

};
Ambernath Branch
9370844446
C++ Notes

// Member functions of child class

double SmallBox::getSmallWidth(void) {

return width ;

void SmallBox::setSmallWidth( double wid ) {

width = wid;

// Main function for the program

int main() {

SmallBox box;

// set box width using member function

box.setSmallWidth(5.0);

cout << "Width of box : "<< box.getSmallWidth() << endl;

return 0;

When the above code is compiled and executed, it produces the following result −

Width of box : 5

 What is Constructor?
Ambernath Branch
9370844446
C++ Notes
A class constructor is a special member function of a class that is executed
whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any
return type at all, not even void. Constructors can be very useful for setting initial values
for certain member variables.
Following example explains the concept of constructor –

#include <iostream>

using namespace std;

class Line {

public:

void setLength( double len );

double getLength( void );

Line(); // This is the constructor

private:

double length;

};

// Member functions definitions including constructor

Line::Line(void) {

cout << "Object is being created" << endl;

void Line::setLength( double len ) {

length = len;
Ambernath Branch
9370844446
C++ Notes
}

double Line::getLength( void ) {

return length;

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;

When the above code is compiled and executed, it produces the following result −
Object is being created
Length of line : 6

 Types of Constructor:- 1. Default Constructor


2. Parameterized Constructor
3. Copy Constructor

1. Default Constructor:-
Ambernath Branch
9370844446
C++ Notes
A default constructor does not have any parameter.
Following is the example of Default Constructor-

#include <iostream>

using namespace std;

class Line {

public:

void setLength( double len );

double getLength( void );

Line(); // This is the constructor

private:

double length;

};

// Member functions definitions including constructor

Line::Line(void) {

cout << "Object is being created" << endl;

void Line::setLength( double len ) {

length = len;

double Line::getLength( void ) {


Ambernath Branch
9370844446
C++ Notes
return length;

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;

2. Parameterized Constructor:-
A default constructor does not have any parameter, but if you need, a
constructor can have parameters. This helps you to assign initial value to an object at
the time of its creation as shown in the following example −

#include <iostream>

using namespace std;

class Line {

public:

void setLength( double len );


Ambernath Branch
9370844446
C++ Notes
double getLength( void );

Line(double len); // This is the constructor

private:

double length;

};

// Member functions definitions including constructor

Line::Line( double len) {

cout << "Object is being created, length = " << len << endl;

length = len;

void Line::setLength( double len ) {

length = len;

double Line::getLength( void ) {

return length;

// Main function for the program

int main() {

Line line(10.0);
Ambernath Branch
9370844446
C++ Notes
// get initially set length.

cout << "Length of line : " << line.getLength() <<endl;

// set line length again

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;

When the above code is compiled and executed, it produces the following result −

Object is being created, length = 10


Length of line : 10
Length of line : 6

Using Initialization Lists to Initialize Fields


In case of parameterized constructor, you can use following syntax to initialize the
fields −

Line::Line( double len): length(len) {


cout << "Object is being created, length = " << len << endl;
}
Above syntax is equal to the following syntax −

Line::Line( double len) {


cout << "Object is being created, length = " << len << endl;
length = len;
}
If for a class C, you have multiple fields X, Y, Z, etc., to be initialized, then use can
use same syntax and separate the fields by comma as follows −
Ambernath Branch
9370844446
C++ Notes
C::C( double a, double b, double c): X(a), Y(b), Z(c) {
....
}

3. Copy Constructor

The copy constructor is a constructor which creates an object by initializing it with an


object of the same class, which has been created previously. The copy constructor is
used to −
 Initialize one object from another of the same type.
 Copy an object to pass it as an argument to a function.
 Copy an object to return it from a function.
If a copy constructor is not defined in a class, the compiler itself defines one.If the
class has pointer variables and has some dynamic memory allocations, then it is a must
to have a copy constructor. The most common form of copy constructor is shown here

lassname (const classname &obj) {


// body of constructor
}
Here, obj is a reference to an object that is being used to initialize another object.

#include <iostream>

using namespace std;

class Line {

public:

int getLength( void );

Line( int len ); // simple constructor


Ambernath Branch
9370844446
C++ Notes
Line( const Line &obj); // copy constructor

~Line(); // destructor

private:

int *ptr;

};

// Member functions definitions including constructor

Line::Line(int len) {

cout << "Normal constructor allocating ptr" << endl;

// allocate memory for the pointer;

ptr = new int;

*ptr = len;

Line::Line(const Line &obj) {

cout << "Copy constructor allocating ptr." << endl;

ptr = new int;

*ptr = *obj.ptr; // copy the value

Line::~Line(void) {
Ambernath Branch
9370844446
C++ Notes
cout << "Freeing memory!" << endl;

delete ptr;

int Line::getLength( void ) {

return *ptr;

void display(Line obj) {

cout << "Length of line : " << obj.getLength() <<endl;

// Main function for the program

int main() {

Line line(10);

display(line);

return 0;

When the above code is compiled and executed, it produces the following result −

Normal constructor allocating ptr


Ambernath Branch
9370844446
C++ Notes
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Let us see the same example but with a small change to create another object
using existing object of the same type –

#include <iostream>

using namespace std;

class Line {

public:

int getLength( void );

Line( int len ); // simple constructor

Line( const Line &obj); // copy constructor

~Line(); // destructor

private:

int *ptr;

};

// Member functions definitions including constructor

Line::Line(int len) {

cout << "Normal constructor allocating ptr" << endl;


Ambernath Branch
9370844446
C++ Notes

// allocate memory for the pointer;

ptr = new int;

*ptr = len;

Line::Line(const Line &obj) {

cout << "Copy constructor allocating ptr." << endl;

ptr = new int;

*ptr = *obj.ptr; // copy the value

Line::~Line(void) {

cout << "Freeing memory!" << endl;

delete ptr;

int Line::getLength( void ) {

return *ptr;

void display(Line obj) {

cout << "Length of line : " << obj.getLength() <<endl;


Ambernath Branch
9370844446
C++ Notes
}

// Main function for the program

int main() {

Line line1(10);

Line line2 = line1; // This also calls copy constructor

display(line1);

display(line2);

return 0;

When the above code is compiled and executed, it produces the following result –

Normal constructor allocating ptr


Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!
Ambernath Branch
9370844446
C++ Notes
 What is Destructor?
A destructor is a special member function of a class that is executed whenever an
object of it's class goes out of scope or whenever the delete expression is applied to a
pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~) and it
can neither return a value nor can it take any parameters. Destructor can be very useful
for releasing resources before coming out of the program like closing files, releasing
memories etc.
Following example explains the concept of destructor −

#include <iostream>

using namespace std;

class Line {

public:

void setLength( double len );

double getLength( void );

Line(); // This is the constructor declaration

~Line(); // This is the destructor: declaration

private:

double length;

};

// Member functions definitions including constructor

Line::Line(void) {

cout << "Object is being created" << endl;


Ambernath Branch
9370844446
C++ Notes
}

Line::~Line(void) {

cout << "Object is being deleted" << endl;

void Line::setLength( double len ) {

length = len;

double Line::getLength( void ) {

return length;

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;

When the above code is compiled and executed, it produces the following result –
Object is being created
Ambernath Branch
9370844446
C++ Notes
Length of line : 6
Object is being deleted

 Constructor Overloading:-

Constructor can be overloaded in a similar way as Function Overloading.


Overloaded constructors have the same name (name of the class) but different number
of arguments. Depending upon the number and type of arguments passed, specific
constructor is called. Since, there are multiple constructors present, argument to the
constructor should also be passed while creating an object.

// Source Code to demonstrate the working of overloaded constructors


#include <iostream>
using namespace std;

class Area
{
private:
int length;
int breadth;

public:
// Constructor with no arguments
Area(): length(5), breadth(2) { }

// Constructor with two arguments


Area(int l, int b): length(l), breadth(b){ }

void GetLength()
Ambernath Branch
9370844446
C++ Notes
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}

int AreaCalculation() { return length * breadth; }

void DisplayArea(int temp)


{
cout << "Area: " << temp << endl;
}
};

int main()
{
Area A1, A2(2, 1);
int temp;

cout << "Default Area when no argument is passed." << endl;


temp = A1.AreaCalculation();
A1.DisplayArea(temp);

cout << "Area when (2,1) is passed as argument." << endl;


temp = A2.AreaCalculation();
A2.DisplayArea(temp);
Ambernath Branch
9370844446
C++ Notes
return 0;
}

For object A1, no argument is passed while creating the object.

Thus, the constructor with no argument is invoked which initialises length to 5


and breadthto 2. Hence, area of the object A1 will be 10.

For object A2, 2 and 1 are passed as arguments while creating the object.

Thus, the constructor with two arguments is invoked which


initialises length to l (2 in this case) and breadth to b (1 in this case). Hence, area of the
object A2 will be 2.

Output

Default Area when no argument is passed.

Area: 10

Area when (2,1) is passed as argument.

Area: 2

 Operator Overloading:-

This feature in C++ programming that allows programmer to redefine the meaning of an
operator (when they operate on class objects) is known as operator overloading. The meaning
of an operator is always same for variable of basic types like: int, float, double etc. For
example: To add two integers, + operator is used.
However, for user-defined types (like: objects), you can redefine the way operator
works. For example:
If there are two objects of a class that contains string as its data members. You can
redefine the meaning of + operator and use it to concatenate those strings.
Ambernath Branch
9370844446
C++ Notes

To overload an operator, a special operator function is defined inside the class as:

class className

... .. ...

public

returnType operator symbol (arguments)

... .. ...

... .. ...

};

 Here, returnType is the return type of the function.


 The returnType of the function is followed by operator keyword.
 Symbol is the operator symbol you want to overload. Like: +, <, -, ++
 You can pass arguments to the operator function in similar way as functions.

Example Of operator Overloading:-

#include <iostream>
using namespace std;

class Test
{
Ambernath Branch
9370844446
C++ Notes
private:
int count;

public:
Test(): count(5){}

void operator ++()


{
count = count+1;
}
void Display() { cout<<"Count: "<<count; }
};

int main()
{
Test t;
// this calls "function void operator ++()" function
++t;
t.Display();
return 0;
}

Output

Count: 6

This function is called when ++ operator operates on the object of Test class (object t in
this case).

In the program,void operator ++ () operator function is defined (inside Test class).

This function increments the value of count by 1 for t object.


Ambernath Branch
9370844446
C++ Notes
 Things to remember
1. Operator overloading allows you to redefine the way operator works for user-defined types only
(objects, structures). It cannot be used for built-in types (int, float, char etc.).
2. Two operators = and & are already overloaded by default in C++. For example: To copy objects
of same class, you can directly use = operator. You do not need to create an operator function.
3. Operator overloading cannot change the precedence and associatively of operators. However, if
you want to change the order of evaluation, parenthesis should be used.
4. There are 4 operators that cannot be overloaded in C++. They are :: (scope
resolution), . (member selection), .* (member selection through pointer to function) and ?:
(ternary operator).

 Overloadable/Non-overloadableOperators
Following is the list of operators which can be overloaded −

+ - * / % ^

& | ~ ! , =

< > <= >= ++ --

<< >> == != && ||

+= -= /= %= ^= &=

|= *= <<= >>= [] ()

-> ->* new new [] delete delete []

Following is the list of operators, which cannot be overloaded −

:: .* . ?:

 Function Overloading:-
Ambernath Branch
9370844446
C++ Notes
You can have multiple definitions for the same function name in the same
scope. The definition of the function must differ from each other by the types
and/or the number of arguments in the argument list. You cannot overload
function declarations that differ only by return type.

Following is the example where same function print() is being used to print
different data types –

#include <iostream>

using namespace std;

class printData {

public:

void print(int i) {

cout << "Printing int: " << i << endl;

void print(double f) {

cout << "Printing float: " << f << endl;

void print(char* c) {

cout << "Printing character: " << c << endl;

};

int main(void) {

printData pd;

// Call print to print integer

pd.print(5);
Ambernath Branch
9370844446
C++ Notes
// Call print to print float

pd.print(500.263);

// Call print to print character

pd.print("Hello C++");

return 0;

When the above code is compiled and executed, it produces the following result –

Printing int: 5
Printing float: 500.263
Printing character: Hello C++

 Inheritance:-
Inheritance means Reusability.
Inheritance is one of the key features of Object-oriented programming in C++. It
allows user to create a new class (derived class) from an existing class(base class). The
derived class inherits all the features from the base class and can have additional features of its own.
When creating a class, instead of writing completely new data members and
member functions, the programmer can designate that the new class should inherit
the members of an existing class. This existing class is called the base class, and the
new class is referred to as the derived class.

 Base and Derived Classes:-


A class can be derived from more than one classes, which means it can inherit
data and functions from multiple base classes. To define a derived class, we use a
class derivation list to specify the base class(es). A class derivation list names one or
more base classes and has the form –
class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-


class is the name of a previously defined class. If the access-specifier is not used,
then it is private by default.

Consider a base class Shape and its derived class Rectangle as follows –
Ambernath Branch
9370844446
C++ Notes
#include <iostream>

using namespace std;

// Base class

class Shape {

public:

void setWidth(int w) {

width = w;

void setHeight(int h) {

height = h;

protected:

int width;

int height;

};

// Derived class

class Rectangle: public Shape {

public:

int getArea() {

return (width * height);

};
Ambernath Branch
9370844446
C++ Notes
int main(void) {

Rectangle Rect;

Rect.setWidth(5);

Rect.setHeight(7);

// Print the area of the object.

cout << "Total area: " << Rect.getArea() << endl;

return 0;

When the above code is compiled and executed, it produces the following result –
Total area: 35

 Access Control and Inheritance:-

A derived class can access all the non-private members of its base class.
Thus base-class members that should not be accessible to the member functions
of derived classes should be declared private in the base class.
We can summarize the different access types according to - who can access
them in the following way –

Access public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no

A derived class inherits all base class methods with the following exceptions −
Ambernath Branch
9370844446
C++ Notes
 Constructors, destructors and copy constructors of the base class.

 Overloaded operators of the base class.

 The friend functions of the base class.

 Types of Inheritance:-
C++ supports six types of inheritance as follows:

1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
6. Multipath Inheritance

1. Single Inheritance
A derived class with only one base class is called single inheritance

2. Multilevel Inheritance
A derived class with one base class and that base class is a derived class of another is
called multilevel inheritance.
Ambernath Branch
9370844446
C++ Notes

3. Multiple Inheritance
A derived class with multiple base class is called multiple inheritance.

4. Hierarchical Inheritance
Multiple derived classes with same base class is called hierarchical inheritance.

5. Hybrid Inheritance
Ambernath Branch
9370844446
C++ Notes
Combination of multiple and hierarchical inheritance is called hybrid inheritance.

6. Multipath Inheritance
A derived class with two base classes and these two base classes have one common base
class is called multipath inheritance.

 Function Overriding:-
Inheritance allows software developers to derive a new class from the existing class.
The derived class inherits features of the base class (existing class).
Ambernath Branch
9370844446
C++ Notes
Suppose, both base class and derived class have a member function with same name
and arguments (number and type of arguments).
If you create an object of the derived class and call the member function which exists in
both classes (base and derived), the member function of the derived class is invoked and the
function of the base class is ignored.
This feature in C++ is known as function overriding.

https://www.geeksforgeeks.org/inheritance-in-c/

You might also like