OODP Unit-2 Notes
OODP Unit-2 Notes
Constructors in C++:
Example
#include <iostream>
using namespace std;
class student {
int rno;
char name[10];
double fee;
public:
student()
{
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}
void display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}
};
int main()
{
student s; // constructor gets called automatically when
// we create the object of the class
s.display();
return 0;
}
● Constructor has same name as the class itself
● Default Constructors don’t have input argument however, Copy and Parameterized
Constructors have input arguments
● Constructors don’t have return type
● A constructor is automatically called when an object is created.
● It must be placed in public section of class.
● If we do not specify a constructor, C++ compiler generates a default constructor for
object (expects no parameters and has an empty body).
Types of Constructors
Default Constructors: Default constructor is the constructor which doesn’t take any
argument. It has no parameters. It is also called a zero-argument constructor.
EXAMPLE 1
// concept of Constructors
#include <iostream>
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
};
int main()
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 1;
Output
a: 10
b: 20
EXAMPLE 2
#include<iostream>
class student
int rno;
char name[50];
double fee;
public:
{
cout<<"Enter the RollNo:";
cin>>rno;
cin>>name;
cin>>fee;
void display()
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
};
int main()
student s;
s.display();
return 0;
EXAMPLE 1
#include <iostream>
class Point {
private:
int x, y;
public:
// Parameterized Constructor
x = x1;
y = y1;
};
int main()
// Constructor called
return 0;
Output
p1.x = 10, p1.y = 15
EXAMPLE 2
#include<iostream>
#include<string.h>
class student
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
void display();
};
student::student(int no,char n[],double f)
rno=no;
strcpy(name,n);
fee=f;
void student::display()
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
int main()
student s(1001,"Ram",10000);
s.display();
return 0;
3. Copy Constructor:
A copy constructor is a member function that initializes an object using another
object of the same class. A detailed article on Copy Constructor.
Whenever we define one or more non-default constructors( with parameters ) for a class,
a default constructor( without parameters ) should also be explicitly defined as the
compiler will not provide a default constructor in this case. However, it is not necessary
but it’s considered to be the best practice to always define a default constructor.
Copy constructor takes a reference to an object of the same class as an argument.
Sample(Sample &t)
{
id=t.id;
}
// Example: copy constructor
#include <iostream>
using namespace std;
class Sample
{
int id;
public:
void init(int x)
{
id=x;
}
Sample(){} //default constructor with empty body
#include<string.h>
class student
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
{
rno=t.rno;
strcpy(name,t.name);
fee=t.fee;
void display();
};
rno=no;
strcpy(name,n);
fee=f;
void student::display()
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
int main()
{
student s(1001,"Manjeet",10000);
s.display();
manjeet.display();
return 0;
OUTPUT
}
void display();
void disp()
{
cout<<endl<<rno<<"\t"<<name;
}
};
student::student(int no, char n[],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s(1001,"Manjeet",10000);
s.display();
return 0;
}
OUTPUT
Destructor:
EXAMPLE
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << "\n Constructor executed"; }
return 0;
}
Output
Constructor executed
Destructor executed
EXAMPLE 2:
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << "\n Constructor executed"; }
main()
{
Test t, t1, t2, t3;
return 0;
}
Output
Constructor executed
Constructor executed
Constructor executed
Constructor executed
Destructor executed
Destructor executed
Destructor executed
Destructor executed
EXAMPLE 3
#include <iostream>
int count = 0;
class Test {
public:
Test()
count++;
~Test()
--count;
};
main()
return 0;
Output
No. of Object created: 1
No. of Object created: 2
No. of Object created: 3
No. of Object created: 4
No. of Object destroyed: 4
No. of Object destroyed: 3
No. of Object destroyed: 2
No. of Object destroyed: 1
Characteristics of a destructor:-
C++ Polymorphism
The word “polymorphism” means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one
form. Polymorphism is considered one of the important features of Object-Oriented
Programming.
Types of Polymorphism
● Compile-time Polymorphism.
● Runtime Polymorphism.
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.
A. Function Overloading
When there are multiple functions with the same name but different parameters, then the
functions are said to be overloaded, hence this is known as Function Overloading.
Functions can be overloaded by changing the number of arguments or/and changing
the type of arguments. In simple terms, it is a feature of object-oriented programming
providing many functions to have the same name but distinct parameters when numerous
tasks are listed under one function name. There are certain Rules of Function
Overloading that should be followed while overloading a function.
Below is the C++ program to show function overloading or compile-time polymorphism:
#include <bits/stdc++.h>
B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this
ability is known as operator overloading. For example, we can make use of the addition
operator (+) for string class to concatenate two strings. We know that the task of this
operator is to add two operands. So a single operator ‘+’, when placed between integer
operands, adds them and when placed between string operands, concatenates them.
Unary Operator Overloading
Unary operator overloading in C++ is polymorphism in which we overload an operator to
perform a similar operation with the class objects. We do operator overloading to give
operators a user-defined meaning, which is utilized for user-defined data type (object of a
class) operations
#include <iostream>
class unary
{
int x,y,z;
public:
void get(int a,int b,int c);
void disp();
void operator-();
};
void unary::get(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void unary::disp()
{
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
}
void unary::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
unary u;
u.get(10,20,-30);
u.disp();
-u;
u.disp();
return 0;
}
OUTPUT:
10
20
-30
-10
-20
30
Unary operator overloading in matrix:
#include <iostream>
class matrix
{
int a[5][5],m,n,i,j;
public:
matrix()
{
}
void get()
{
cout<<"no of rows"<<endl;
cin>>m;
cout<<"no of cols"<<endl;
cin>>n;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void disp()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
}
void operator-()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=-a[i][j];
}
}
}
};
int main()
{
matrix m;
m.get();
cout<<"the given matrix is"<<endl;
m.disp();
-m;
cout<<"resultant matrix"<<endl;;
m.disp();
return 0;
}
OUTPUT:
no of rows
3
no of cols
3
12345678 9
the given matrix is
123
456
789
resultant matrix
-1-2-3
-4-5-6
-7-8-9
class assign
{
int a;
public:
assign()
{
}
assign(int x)
{
a=x;
}
void operator=(assign);
void disp()
{
cout<<"the value of a is"<<a;
}
};
void assign::operator=(assign as)
{
a=as.a;
}
int main()
{
assign a1(10);
assign a2;
a2=a1;
a2.disp();
return 0;
}
OUTPUT
class oversub
{
int a[5];
public:
void operator[](int i);
};
void oversub::operator[](int i)
{
if(i>0 && i<4)
{
cout<<"valid subscript"<<endl;
}
else
{
cout<<"subscript out of range"<<endl;
}
}
int main()
{
oversub s;
s[2];
s[7];
return 0;
}
OUTPUT
valid subscript
subscript out of range
OVERLOADING FUNCTION CALL () OPERATOR (UNARY)
#include <iostream>
class overfun
{
public:
void operator()(int v)
{
cout<<(v<10?"yes":"no");
}
};
int main()
{
overfun of;
of(10);
of(-5);
return 0;
}
OUTPUT
No
Yes
OVERLOADING INCREMENT OPERATOR (UNARY)-PREFIX
#include <iostream>
class incr
{
int count;
public:
incr()
{
count=5;
}
void operator ++()
{
++count;
cout<<count;
}
};
int main()
{
incr i;
++i;
return 0;
}
OUTPUT
6
OVERLOADING DECREMENT OPERATOR (UNARY)-PREFIX
#include <iostream>
class dec
{
int count;
public:
dec()
{
count=5;
}
void operator --()
{
--count;
cout<<count;
}
};
int main()
{
dec i;
--i;
return 0;
}
OUTPUT
6
OVERLOADING INCREMENT OPERATOR (UNARY)-POSTFIX
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check()
{
i=0;
}
// Notice int inside barcket which indicates postfix increment.
Check operator ++ (int)
{
Check temp;
temp.i = i++;
return temp;
}
void Display()
{
cout << "i = "<< i <<endl;
}
};
int main()
{
Check obj,obj1;
obj.Display();
return 0;
}
OUTPUT
i=0
i=1
i=0
OVERLOADING DECREMENT OPERATOR (UNARY)-POSTFIX
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check()
{
i=2;
}
// Notice int inside barcket which indicates postfix increment.
Check operator -- (int)
{
Check temp;
temp.i = i--;
return temp;
}
void Display()
{
cout << "i = "<< i <<endl;
}
};
int main()
{
Check obj,obj1;
obj.Display();
return 0;
}
OUTPUT
i=2
i=1
i=2
class comp{
float real;
float imag;
public:
comp()
{
real=imag=0;
}
void get()
{
cout<<"enter the real part"<<endl;
cin>>real;
cout<<"enter the imag part"<<endl;
cin>>imag;
}
void disp()
{
cout<<real<<"+i"<<imag<<endl;
}
comp operator +(comp);
comp operator -(comp);
};
comp comp::operator +(comp c)
{
comp temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
comp comp::operator -(comp c)
{
comp temp;
temp.real=real-c.real;
temp.imag=imag-c.imag;
return temp;
}
int main()
{
comp c1,c2,c3;
c1.get();
c2.get();
c3=c1+c2;
c3.disp();
return 0;
}
OUTPUT
enter the real part
2
enter the imag part
3
enter the real part
3
enter the imag part
2
5+i5
Among the in-built operators which operators cannot be overloaded using the friend function but can be
overloaded by member functions are as follows:
UML Sequence Diagrams are interaction diagrams that detail how operations are carried out. They
capture the interaction between objects in the context of a collaboration. Sequence Diagrams are time
focus and they show the order of the interaction visually by using the vertical axis of the diagram to
represent time what messages are sent and when.