OOPS NOTES OF C++ FOR EXAM
OOPS NOTES OF C++ FOR EXAM
OOPS NOTES OF C++ FOR EXAM
int main() {
MyClass myObj; // Create an object of MyClass
MyClass() { // Constructor
cout << "Hello World!";
}
};
int main() {
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " <<
carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " <<
carObj2.year << "\n";
return 0;
}
Constructor definition outside the class
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " <<
carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " <<
carObj2.year << "\n";
return 0;
}
Example >>
// Encapsulation
#include<iostream>
class Encapsulation{
private:
int x;
public:
void set(int a)
x =a; }
int get()
{ return x; }
};
int main()
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
#include <iostream>
using namespace std;
class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}
#include <iostream>
class Sum {
private: int a,b,c;
public:
void add(){
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c; }
};
int main(){
sum s;
s.add();
return 0 ; }
Inheritance in C++
>> 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.
#include <bits/stdc++.h>
// Base class
class Parent {
public:
int id_p; };
public:
int id_c; };
// main function
int main() {
Child obj1;
obj1.id_c = 7;
obj1.id_p = 91;
return 0; }
#include <iostream>
#include <string>
// Base class
class Vehicle {
public:
void honk() {
};
// Derived class
public:
};
int main() {
Car myCar;
myCar.honk();
return 0;
}
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.
#include<iostream>
// base class
class Vehicle {
public:
Vehicle(){
cout << "This is a Vehicle\n";
};
};
// main function
int main(){
Car obj;
return 0;}
A class can also be derived from more than one base class, using a comma-
separated list
#include <iostream>
// Base class
class MyClass {
public:
void myFunction() {
}};
class MyOtherClass {
public:
void myOtherFunction() {
}};
// Derived class
};
int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
RETURN 0; }
// C++ program to explain multiple inheritance
#include <iostream>
class Vehicle {
public:
class FourWheeler {
public:
FourWheeler(){
};
};
// main function
int main(){
// Creating object of sub class will invoke the constructor of base classes.
Car obj;
return 0;}
Multilevel Inheritance: In this type of inheritance, a derived class is created
from another derived class.
multilevel inheritance, a class has more than one parent class. For example, if we
take animals as a base class then mammals are the derived class which has features of
animals and then humans are the also derived class that is derived from sub-class
mammals which inherit all the features of mammals.
// base class
class Vehicle {
public:
public:
fourWheeler(){
} };
public:
};
int main(){
// Creating object of sub class will invoke the constructor of base classes.
Car obj;
return 0;}
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
#include <iostream>
// base class
class Vehicle {
public:
};
};
};
int main(){
// Creating object of sub class will invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
POLYMORFISM
Function Overloading: When there are multiple functions with same name
but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of
arguments or/and change in type of arguments.
// C++ program for function overloading
#include <bits/stdc++.h>
class Geeks
public:
void func(int x)
void func(double x)
{
cout << "value of x and y is " << x << ", " << y << endl;
};
int main() {
Geeks obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85,64);
return 0;
#include<iostream>
class Complex {
private:
public:
Complex res;
return res;
void print() { cout << real << " + i" << imag << endl; }
};
int main(){
c3.print(); }
Function overriding on the other hand 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
Function overriding is a feature that allows us to have a same function in child class
which is already present in the parent class. A child class inherits the data members and
member functions of parent class, but when you want to override a functionality in the
child class then you can use function overriding.
To override a function you must have the same signature in child class. By signature I
mean the data type and sequence of parameters. Here we don’t have any parameter in
the parent function so we didn’t use any parameter in the child function.
#include <iostream>
using namespace std;
class BaseClass {
public:
void disp(){
cout<<"Function of Parent Class";
}
};
class DerivedClass: public BaseClass{
public:
void disp() {
cout<<"Function of Child Class";
}
};
int main() {
DerivedClass obj = DerivedClass();
obj.disp();
return 0;
}
#include <bits/stdc++.h>
class base{
public:
void show ()
};
public:
void show ()
};
//main function
int main() {
base *bptr;
derived d;
bptr = &d;
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
return 0 ;
Data abstraction
is one of the most essential and important feature of object oriented programming in C++. Abstraction
means displaying only essential information and hiding the details. Data abstraction refers to providing
only essential information about the data to the outside world, hiding the background details or
implementation.
Consider a real life example of a man driving a car. The man only knows that pressing the accelerators
will increase the speed of car or applying brakes will stop the car but he does not know about how on
pressing accelerator the speed is actually increasing, he does not know about the inner mechanism of
the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is.
Abstraction using Classes: We can implement Abstraction in C++ using classes. Class helps us to
group data members and member functions using available access specifiers. A Class can decide
which data member will be visible to outside world and which is not.
Abstraction in Header files: One more type of abstraction in C++ can be header files. For
example, consider the pow() method present in math.h header file. Whenever we need to calculate
power of a number, we simply call the function pow() present in the math.h header file and pass the
numbers as arguments without knowing the underlying algorithm according to which the function is
actually calculating power of numbers.
Abstraction using access specifiers
Access specifiers are the main pillar of implementing abstraction in C++. We can use access specifiers
to enforce restrictions on class members. For example:
Members declared as public in a class, can be accessed from anywhere in the program.
Members declared as private in a class, can be accessed only from within the class. They are not
allowed to be accessed from any part of code outside the class.
#include <iostream>
class implementAbstraction{
private:
int a, b;
public:
a = x;
b = y; }
void display(){
} };
int main(){
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
What is constructor?
Constructor is a member function of class, whose name is same as the class.
A constructor is a special type of member function of a class which initializes objects of a class. In C++, Constructor is
automatically called when object(instance of 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 constructors.
Constructor does not have a return value, hence they do not have a return type.
Prototype of Constructors:-
<class-name> (list-of-parameters);
Constructors can be defined inside or outside the class declaration: -
1. Syntax for defining the constructor within the class:
<class-name> (list-of-parameters)
{
// constructor definition
}
2. Syntax for defining the constructor outside the class:
<class-name>: :<class-name> (list-of-parameters)
{
// constructor definition
}
#include <iostream>
class student {
int rno;
char name[10];
double fee;
public:
student(){
cin>>rno;
cin>>name;
cin>>fee;
void display(){
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
};
int main()
s.display();
return 0;
#include<iostream>
class student
int rno;
char name[50];
double fee;
public:
student();
void display();
};
student::student()
cin>>rno;
cin>>name;
cin>>fee;
void student::display()
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
int main()
{
student s;
s.display();
return 0;
What is a destructor?
Destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed. Meaning ,
a destructor is the last function that is going to be called before an object is destroyed.
The thing is to be noted here, if the object is created by using new or the constructor uses new to allocate memory which res ides
in the heap memory or the free store, the destructor should use delete to free the memory.
Syntax:
~constructor-name();
Properties of Destructor:
Destructor function is automatically invoked when the objects are destroyed.
It cannot be declared static or const.
The destructor does not have arguments.
It has no return type not even void.
An object of a class with a Destructor cannot become a member of the union.
A destructor should be declared in the public section of the class.
The programmer cannot access the address of destructor.
When is destructor called?
A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
How are destructors different from a normal member function?
No, there can only one destructor in a class with classname preceded by ~, no parameters and no return type.
When do we need to write a user-defined destructor?
If we do not write our own destructor in class, compiler creates a default destructor for us. The default destructor works fi ne
unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class,
we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leak .
Can a destructor be virtual?
Yes, In fact, it is always a good idea to make destructors virtual in base class when we have a virtual function.
class String {
private:
char* s;
int size;
public:
String(char*); // constructor
~String(); // destructor
};
String::String(char* c)
size = strlen(c);
strcpy(s, c);
String::~String() { delete[] s; }
#include <iostream>
class Employee {
public:
Employee(){
cout<<"Constructor Invoked"<<endl; }
~Employee(){
cout<<"Destructor Invoked"<<endl; }
};
int main(void){
return 0; }
RANDOM NOTES
Generics in C++
Generics is the idea to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes and interfaces. For
example, classes like an array, map, etc, which can be used using generics very efficiently. We can use them for any type.
The method of Generic Programming is implemented to increase the efficiency of the code. Generic Programming enables the programmer to
write a general algorithm which will work with all data types. It eliminates the need to create different algorithms if the data type is an integer,
string or a character.
The advantages of Generic Programming are
1. Code Reusability
2. Avoid Function Overloading
3. Once written it can be used for multiple times and cases.
Generics can be implemented in C++ using Templates. Template is a simple and yet very powerful tool in C++. The simple idea is to pass data
type as a parameter so that we don’t need to write the same code for different data types. For example, a software company may need sort() for
different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter.
#include <iostream>
// One function works for all data types. This would work even for user defined types
if operator '>' is overloaded
T myMax(T x, T y){
return (x > y) ? x : y;
int main(){
RETURN 0 ;}
Class: A class in C++ is the building block that leads to Object-Oriented
programming. It is a user-defined data type, which holds its own data members
and member functions, which can be accessed and used by creating an instance of
that class. A C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class
and wheels, speed limits, mileage are their properties.
A Class is a user defined data-type which has data members and member
functions.
Data members are the data variables and member functions are the functions used
to manipulate these variables and together these data members and member
functions defines the properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage
etc and member functions can be apply brakes, increase speed etc.
A class is defined in C++ using keyword class followed by the name of class. The
body of class is defined inside the curly brackets and terminated by a semicolon at
the end.
// Example:
#include<iostream>
using namespace std;
class A{
int x;
public:
A()
x=10;
};
class B{
public:
};
main()
{
A _a;
B _b;
_b.display(_a);
return 0;