0% found this document useful (0 votes)
8 views

OOPS Concepts NOTES

The document outlines key concepts of Object-Oriented Programming (OOP) including classes, objects, encapsulation, polymorphism, and inheritance. It provides examples of C++ programs demonstrating these concepts, such as constructors, function overloading, and operator overloading. Additionally, it explains the types of constructors and the role of friend functions in accessing private members of a class.

Uploaded by

ry6782790
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)
8 views

OOPS Concepts NOTES

The document outlines key concepts of Object-Oriented Programming (OOP) including classes, objects, encapsulation, polymorphism, and inheritance. It provides examples of C++ programs demonstrating these concepts, such as constructors, function overloading, and operator overloading. Additionally, it explains the types of constructors and the role of friend functions in accessing private members of a class.

Uploaded by

ry6782790
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/ 20

 OOPS Concepts:

Class
Objects
Encapsulation
Polymorphism
Inheritance

 Class: A class is a template that defines the structure and


behavior of an object.
 Object: An object is an instance of a class. It is a concrete
representation of the class, containing data (attributes) and
behaviors (methods) that the class defines.
 Encapsulation: Encapsulation is the process of bundling data
and the methods that work with it into a single unit, called a
class.
 Polymorphism: Polymorphism in refers to the ability of
different objects to be treated as instances of the same class
through a common interface, while each object can behave
differently based on its specific class.
 Inheritance: Inheritance is a fundamental concept in that
allows one class to inherit properties and behavior from
another class. inheritance enables code reusability, facilitating
the creation of a new class based on an existing class.
1. The add of data and function together into a single unit.
The unit is called class instance called that unit object and process
as Encapsulation.
2. C++: New

O
O c
P

visibility mode >Public


>Private
>Protccted
Data member(a).
Data member function(r,w)
3. PROGRAM 1:BASIC
Class: #include<iostream>
using namespace std;

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>

using namespace std;

class Table

int a, i, n;

public:

void r()

cin >> n;

void w()

for(i = 1; i <= 10; i++)

a = n * i;

cout<<n<<"*"<<i<<"="<<a<<endl;

};

int main()
{

Table d;

d.r();

d.w();

return 0;

PROGRAM 3:SUM
#include<iostream>

using namespace std;

class Sum

{ class

int a, b;

public:

void g();

void add(); Declaration

};

void Sum::g()

cout << "Enter two numbers: ";

cin >> a >> b;

} Member function definition

cout << "Sum: " << a + b << endl;


}

int main()

Sum s1;

s1.g(); Object

s1.add();

return 0;

PROGRAM 4: TEST
#include<iostream>

using namespace std;

class test

int a, b;

public:

void g(int p, int q)

a = p;

b = q;

void sum()

cout << "Sum is " << a + b << endl;

};
int main()

int x, y,z;

cout << "Enter two numbers: ";

cin >> x >> y;

test t1;

t1.g(x, y);

t1.sum();

return 0;

PROGRAM 5:TIME
#include<iostream>

using namespace std;

class Time

int h;

int m;

public:

void read();

void write();

void sum(Time, Time);

};

void Time::read()

cout << "Enter the value of hour and minute: ";

cin >> h >> m;


}

void Time::write()

cout << "Hour = " << h;

cout << "\nMinute = " << m << endl;

void Time::sum(Time s, Time t)

h = s.h + t.h;

m = s.m + t.m;

if (m >= 60)

h = (m / 60);

m = m % 60;

int main()

Time t1, t2, t3;

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>

using namespace std;

int x = 1;

class c {

int a;

public:

c(){

a = x;

cout << x << endl;

x = x + 1;

void p() {

cout << a << endl;

void g() {

cout << "Enter a value for a: ";

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.

PROGRAM 7: FUNCTION WITH DEFAULT ARGUMENT:


#include<iostream>

using namespace std;

int pw(int m, int n = 2);

int main() {

int s;

s = pw(3);

cout << s << endl;

s = pw(3, 3);

cout << s << endl;

s = pw(5, 4);

cout << s << endl;

return 0;

int pw(int m, int n) {

int z = 1, i = 1;
while (i <= n) {

z = z * m;

i++;

return z;

 Parameterized constructor:

A parameterized constructor is a method that initializes an object's instance


variables by passing one or more parameters. It allows programmers to

create objects with specific properties and attributes without using the setter
methods defined in a class.

 Multiple constructor:

Multiple constructors, also known as constructor overloading, is a feature in


many programming languages that allows you to define multiple
constructors for a class with different parameters. This provides flexibility in
how objects of the class can be initialized.

 Copy constructor:

A copy constructor is a type of constructor that initializes an object using


another object of the same class. In simple terms, a constructor which
creates an object by initializing it with an object of the same class, which has
been created previously is known as a copy constructor.

 PROGRAM 8: Copy constructor:


#include<iostream>

using namespace std;

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.

PROGRAM 8:Demostrative program default:


#include<iostream>

using namespace std;

class c {

int a;

public:

c(int x = 0) {

a = x;

void p() {

cout << a << endl;

};

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.

PROGRAM 9:FUNCTION OVERLOADING:


#include<iostream>

using namespace std;

class f0 {

int result;

public:

f0() {

result = 0;

void print() {

cout << result << endl;

void sum(int a, int b) {

result = a + b;

void sum(int a, int b, int c) {


result = a + b + c;

};

int main() {

f0 f1;

f1.sum(1, 2);

f1.print();

f1.sum(1, 2, 3);

f1.print();

return 0;}

PROGRAM 10:AREA
#include<iostream>

using namespace std;

class c1 {

public:

void area(int base, int h) {

cout << "Area of triangle: " << 0.5 * base * h << endl;

void area(int r) {

cout << "Area of circle: " << 3.14 * r * r << endl;

};

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.

PROGRAM 10:FRIEND FUNCTION


#include<iostream>

using namespace std;

class A {

int x;

public:

A() {

x = 0;

void r(){

cin >> x;

void w() {

cout << x << endl;

friend A sum(A, A);

};

A sum(A p, A q) {

A s;

s.x = p.x + q.x;


return s;

int main() {

A a1, a2, a3;

cout << "Enter value for a1: ";

a1.r();

cout << "Enter value for a2: ";

a2.r();

a3 = sum(a1, a2);

cout << "Sum: ";

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

Dynamic virtual function Static function overload operator

 Operator overloading using member function:


1. U.O.O->M.F
2. U.O.O->F.F
3. B.O.O->M.F
4. B.O.O->F.F
1.1.1. Unary operator overloading ->member function
1.1.2. Binary operator overloading ->friend function

PROGRAM 11:OPERATOR OVERLOADING


#include<iostream>
using namespace std;
class A
{
int x;
public:
A(){
x=0;
}
g(){
cin >> x;
}
p(){
cout << x << endl;
}
A operator+ (A z)
{
A y;
y.x=x+z.x;
return(y);
}
};
int main()
{
A a1,a2,a3;
a1.g();
a2.g();
a3=a1+a2;
a3=a1.operator+(a2);
a3.p();
return 0;
}

PROGRAM 12:UNARY OPERATOR


#include <iostream>

using namespace std;


class A {

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;

PROGRAM 13:BINARY OPERATOR:


#include <iostream>
using namespace std;

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;}

You might also like