OOPS Concepts NOTES
OOPS Concepts NOTES
Class
Objects
Encapsulation
Polymorphism
Inheritance
O
O c
P
class Demo {
int a;
public:
void r() {
cin >> a; }
void w() {
out << a;
};
Object:int main() {
Demo d;
d.r();
d.w();
return 0;
4. PROGRAM 2:TABLE
#include<iostream>
class Table
int a, i, n;
public:
void r()
cin >> n;
void w()
a = n * i;
cout<<n<<"*"<<i<<"="<<a<<endl;
};
int main()
{
Table d;
d.r();
d.w();
return 0;
PROGRAM 3:SUM
#include<iostream>
class Sum
{ class
int a, b;
public:
void g();
};
void Sum::g()
int main()
Sum s1;
s1.g(); Object
s1.add();
return 0;
PROGRAM 4: TEST
#include<iostream>
class test
int a, b;
public:
a = p;
b = q;
void sum()
};
int main()
int x, y,z;
test t1;
t1.g(x, y);
t1.sum();
return 0;
PROGRAM 5:TIME
#include<iostream>
class Time
int h;
int m;
public:
void read();
void write();
};
void Time::read()
void Time::write()
h = s.h + t.h;
m = s.m + t.m;
if (m >= 60)
h = (m / 60);
m = m % 60;
int main()
t1.read();
t2.read();
t3.sum(t1, t2);
t3.write();
return 0;
5. CONSTRUCTORS:
A constructors is a ‘special’ member function whose task is to initialize of its class .it is
special because its name is the same as the class name
PROGRAM 6: CONSTRUCTORS
#include<iostream>
int x = 1;
class c {
int a;
public:
c(){
a = x;
x = x + 1;
void p() {
void g() {
cin >> a;
};
int main() {
c c1[100];
int y;
return 0;
}
6. TYPES OF CONSTRUCTORS:
1. Default constructor
2. Parameterized constructor/constructor with argument.
3. Multiple constructor.
4. Copy constructor .
5. Dynamic constructor.
6. Default constructor
Default constructor:
A default constructor is a constructor that can be called without providing
any arguments. It can be either a constructor with no parameters or a
constructor where all parameters have default arguments.
int main() {
int s;
s = pw(3);
s = pw(3, 3);
s = pw(5, 4);
return 0;
int z = 1, i = 1;
while (i <= n) {
z = z * m;
i++;
return z;
Parameterized constructor:
create objects with specific properties and attributes without using the setter
methods defined in a class.
Multiple constructor:
Copy constructor:
class c {
int a, b;
public:
c() {
a = 0;
b = 0;
c(int x, int y) {
a = x;
b = y;
c(const c &z) {
a = z.a;
b = z.b;
void p() {
cout << "a = " << a << ", b = " << b << endl;
};
int main() {
c c1;
c c2(5, 6);
c c3(c2);
c1.p();
c2.p();
c3.p();
return 0;
}
Default constructor:
A default constructor is a constructor in a computer programming language
that is automatically generated by the compiler if there are no programmer-
defined constructors.
class c {
int a;
public:
c(int x = 0) {
a = x;
void p() {
};
int main() {
c c1;
c c2(8);
c1.p();
c2.p();
return 0;
DEFAULT CONSTRUCTOR
D.C C.D.A(Constructor with default
argu)
FUNCTION OVERLOADING:
Function overloading is a feature of object-oriented programming where two or more
functions can have the same name but different parameters. When a function name is
overloaded with different jobs it is called Function Overloading.
class f0 {
int result;
public:
f0() {
result = 0;
void print() {
result = a + b;
};
int main() {
f0 f1;
f1.sum(1, 2);
f1.print();
f1.sum(1, 2, 3);
f1.print();
return 0;}
PROGRAM 10:AREA
#include<iostream>
class c1 {
public:
cout << "Area of triangle: " << 0.5 * base * h << endl;
void area(int r) {
};
int main() {
c1 c0;
c0.area(5, 7);
c0.area(5);
return 0;
FRIEND FUNCTION:
A friend function can be granted special access to private and protected members of a
class in C++. They are not the member functions of the class but can access and
manipulate the private and protected members of that class for they are declared as
friends.
class A {
int x;
public:
A() {
x = 0;
void r(){
cin >> x;
void w() {
};
A sum(A p, A q) {
A s;
int main() {
a1.r();
a2.r();
a3 = sum(a1, a2);
a3.w();
return 0;
PROTOTYPE: Returntype
Name
Argument/ parameter
POLYMORPHISM:
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
POLYMORPHISM
int x;
public:
A(){
x=0;
void g() {
cin >> x;
void p() {
cout <<x;
A operator--() {
A s;
s.x=--x;
};
int main() {
A a1;
a1.g();
A a2 = --a1;
a2.p();
return 0;
class A {
int x;
public:
A() {
x=0;
void g(){
cin>>x;
void p(){
cout<<x<< endl;
friend A operator+(A,A);
};
A operator+(A p, A q) {
A s;
s.x=p.x+q.x;
return s;
int main(){
A a1,a2,a3;
a1.g();
a2.g();
a3=a1+a2;
a3.p();
return 0;}