C++ Notes
C++ Notes
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 –
#include <iostream>
class Box {
public:
int main() {
// 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 of box 2
return 0;
When the above code is compiled and executed, it produces the following result −
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 −
#include <iostream>
class Box {
public:
double getVolume(void);
};
double Box::getVolume(void) {
length = len;
breadth = bre;
height = hei;
}
Ambernath Branch
9370844446
C++ Notes
int main() {
// 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();
// volume of box 2
Ambernath Branch
9370844446
C++ Notes
volume = Box2.getVolume();
return 0;
When the above code is compiled and executed, it produces the following result −
Access Modifiers:-
There are three types of Access Modifiers- 1. Public
2. Private
3. Protected
class Base {
public:
// public members go here
protected:
};
Ambernath Branch
9370844446
C++ Notes
#include <iostream>
class Line {
public:
double length;
};
double Line::getLength(void) {
return length ;
length = len;
}
Ambernath Branch
9370844446
C++ Notes
int main() {
Line line;
line.setLength(6.0);
return 0;
When the above code is compiled and executed, it produces the following result −
Length of line : 6
Length of line : 10
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>
class Box {
public:
double length;
private:
double width;
};
return width ;
width = wid;
int main() {
Box box;
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
#include <iostream>
class Box {
protected:
double width;
};
public:
};
Ambernath Branch
9370844446
C++ Notes
double SmallBox::getSmallWidth(void) {
return width ;
width = wid;
int main() {
SmallBox box;
box.setSmallWidth(5.0);
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>
class Line {
public:
private:
double length;
};
Line::Line(void) {
length = len;
Ambernath Branch
9370844446
C++ Notes
}
return length;
int main() {
Line line;
line.setLength(6.0);
return 0;
When the above code is compiled and executed, it produces the following result −
Object is being created
Length of line : 6
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>
class Line {
public:
private:
double length;
};
Line::Line(void) {
length = len;
int main() {
Line line;
line.setLength(6.0);
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>
class Line {
public:
private:
double length;
};
cout << "Object is being created, length = " << len << endl;
length = len;
length = len;
return length;
int main() {
Line line(10.0);
Ambernath Branch
9370844446
C++ Notes
// get initially set length.
line.setLength(6.0);
return 0;
When the above code is compiled and executed, it produces the following result −
3. Copy Constructor
#include <iostream>
class Line {
public:
~Line(); // destructor
private:
int *ptr;
};
Line::Line(int len) {
*ptr = len;
Line::~Line(void) {
Ambernath Branch
9370844446
C++ Notes
cout << "Freeing memory!" << endl;
delete ptr;
return *ptr;
int main() {
Line line(10);
display(line);
return 0;
When the above code is compiled and executed, it produces the following result −
#include <iostream>
class Line {
public:
~Line(); // destructor
private:
int *ptr;
};
Line::Line(int len) {
*ptr = len;
Line::~Line(void) {
delete ptr;
return *ptr;
int main() {
Line line1(10);
display(line1);
display(line2);
return 0;
When the above code is compiled and executed, it produces the following result –
#include <iostream>
class Line {
public:
private:
double length;
};
Line::Line(void) {
Line::~Line(void) {
length = len;
return length;
int main() {
Line line;
line.setLength(6.0);
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:-
class Area
{
private:
int length;
int breadth;
public:
// Constructor with no arguments
Area(): length(5), breadth(2) { }
void GetLength()
Ambernath Branch
9370844446
C++ Notes
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}
int main()
{
Area A1, A2(2, 1);
int temp;
For object A2, 2 and 1 are passed as arguments while creating the object.
Output
Area: 10
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
... .. ...
... .. ...
};
#include <iostream>
using namespace std;
class Test
{
Ambernath Branch
9370844446
C++ Notes
private:
int count;
public:
Test(): count(5){}
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).
Overloadable/Non-overloadableOperators
Following is the list of operators which can 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>
class printData {
public:
void print(int i) {
void print(double f) {
void print(char* c) {
};
int main(void) {
printData pd;
pd.print(5);
Ambernath Branch
9370844446
C++ Notes
// Call print to print float
pd.print(500.263);
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.
Consider a base class Shape and its derived class Rectangle as follows –
Ambernath Branch
9370844446
C++ Notes
#include <iostream>
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
void setHeight(int h) {
height = h;
protected:
int width;
int height;
};
// Derived class
public:
int getArea() {
};
Ambernath Branch
9370844446
C++ Notes
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
When the above code is compiled and executed, it produces the following result –
Total area: 35
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 –
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.
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/