OODP Unit 2 Notes
OODP Unit 2 Notes
Unit 2
Types of constructor (Default, Parameter, Copy), Static constructor
Types of constructor: Constructors are of three types
Default Constructor
Parameterized Constructor
Copy Constructor Object
Default Constructor
Default constructor is the constructor which doesn’t take any argument.
It has no parameters
Even if we do not define any constructor explicitly, the compiler will automatically
provide a default constructor implicitly.
Sample program
#include <iostream>
using namespace std;
class constructor
{ public:
int a;
// Default Constructor
constructor()
{ a = 10; }
};
int main()
{ constructor c;
cout << "a: " << c.a << endl;
cout << "area of square is: " << c.a * c.a << endl;
return 0; }
Output: a: 10
area of square is: 100
1
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Parameterized Constructors
These are the constructors with parameter.
Using this Constructor you can provide different values to data members of different
objects, by passing the appropriate values as argument.
When you define the constructor’s body, use the parameters to initialize the object.
It is used to overload constructors ( Constructor Overloading)
Sample program
#include <iostream>
using namespace std;
class constructor
{ public:
int a, b;
// parameterized Constructor
constructor(int x)
{ a = x; }
constructor(int x, int y)
{ a = x; b=y;}
};
int main()
{ constructor c1(10);
cout << "a: " << c1.a << endl;
cout << "area of square1 is: " << c1.a * c1.a << endl;
constructor c2(20);
cout << "a: " << c2.a << endl;
cout << "area of square2 is: " << c2.a * c2.a << endl;
constructor c3(10,30);
cout << "a:" << c3.a <<" " <<"b:" << c3.b << endl;
cout << "area of rectangale is : " << c3.a * c3.b << endl;
return 0; }
2
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Output:
a: 10
area of square1 is: 100
a: 20
area of square2 is: 400
a:10 b:30
area of rectangale is : 300
Copy Constructors
These are special type of Constructors which takes an object as argument, and is used
to copy values of data members of one object into other object.
it creates a new object, which is exact copy of the existing copy, hence it is called copy
constructor.
Using this Constructor you can provide different values to data members of different
objects, by passing the appropriate values as argument.
When you define the constructor’s body, use the parameters to initialize the object.
Sample program
#include <iostream>
using namespace std;
class constructor
{ public:
int a, b;
// normal Constructor
constructor(int x, int y)
{ a = x; b=y; }
// Copy Constructor
constructor (const constructor &c1)
{ a = c1.a;
b = c1.b; }
void display()
{ cout << "a:" << a <<" " <<"b:" << b << endl;
cout << "area of rectangale is : " << a * b << endl; }
};
3
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
int main()
{ constructor c1(10,20);
constructor c2 = c1;
cout<< "The normal constructor"<<endl;
c1.display();
cout<< "The Copy constructor"<<endl;
c2.display();
return 0;
}
Output:
The normal constructor
a:10 b:20
area of rectangale is : 200
The Copy constructor
a:10 b:20
area of rectangale is : 200
Static constructor
C++ doesn't have static constructors but you can emulate them using a static instance of a nested
class. In C++, there is no static constructor. In C# and java, you can define static constructor
which is called automatically by the runtime so as to initialize static members.
Feature Polymorphism:
Polymorphism:
Polymorphism means the ability to take more than one form.
An operation have different behavior in different instances.
The behavior depends upon the type of the data used in the operation
Constructor overloading or Multiple Constructors
Overloaded constructors can have more constructor with the same name as class name
But, Each constructor has a different list of arguments and type of arguments.
A constructor is called depending upon the number of arguments and type of arguments
passed.
4
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
While creating the object, arguments must be passed to let compiler know, which
constructor needs to be called
This concept is known as Constructor Overloading and is quite similar to function
overloading.
Sample program
#include <iostream>
using namespace std;
class constructor
{ public:
int a, b;
// defaultConstructor
constructor()
{ a = 10; b=20;}
// Parameterized Constructor
constructor(int x, int y)
{ a = x; b=y; }
// Copy Constructor
constructor (const constructor &c1)
{ a = c1.a;
b = c1.b; }
void display()
{ cout << "a:" << a <<" " <<"b:" << b << endl;
cout << "area of rectangale is : " << a * b << endl;
}
};
int main()
{ constructor c1;
constructor c2(20,30);
constructor c3 = c2;
5
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
cout<< "The default constructor"<<endl;
c1.display();
cout<< "The parametrized constructor"<<endl;
c2.display();
cout<< "The Copy constructor"<<endl;
c3.display();
return 0;
}
Output:
The default constructor
a:10 b:20
area of rectangale is : 200
The parametrized constructor
a:20 b:30
area of rectangale is : 600
The Copy constructor
a:20 b:30
area of rectangale is : 600
6
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Run time Polymorphism
o Run time polymorphism is achieved when the object's method is invoked at the
run time instead of compile time.
o It is achieved by method overriding which is also known as dynamic binding or
late binding.
7
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Function or Method overloading
Function overloading is a feature of OOPs where two or more functions can have the
same name but different parameters.
i.e. The function is redefined by using either different types of arguments or a different
number of arguments.
It is only through these differences compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the readability of the
program because you don't need to use different names for the same action.
8
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Output:
30
55
60.6
195
9
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Rules for Operator Overloading are
An operators semantic can be extended but syntax must not be altered
Existing operators can only be overloaded, but the new operators cannot be overloaded.
The overloaded operator contains atleast one operand of the user-defined data type.
We cannot use friend function to overload certain operators like = ( ) [ ] . However,
the member function can be used to overload those operators.
When Unary operators are overloaded through a member function
o take no explicit arguments, and return type is void.
o but, if they are overloaded by a friend function, takes one argument (as object
reference) and return type is void.
When binary operators are overloaded through a member function
o takes one explicit argument, and return type is class type. The left side operand
must be class type
o and if they are overloaded through a friend function takes two explicit arguments
and return type is class type
10
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Example for Unary Operator overloading (++ increment operator)
#include <iostream>
using namespace std;
class Uoperator
{ private:
int num;
public:
Uoperator()
{ num= 8; }
Output:
The Value of num after incerement is: 10
Example for Unary decrement (--) Operator overloading using friend function
#include <iostream>
using namespace std;
class Uoperator
{
private:
int num;
public:
Uoperator()
{ num= 8; }
void display()
{ cout<<"The Value of num after overloaded unary decrement is: "<<num; }
};
11
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
void operator --(Uoperator &x)
{ x.num = x.num-2; }
int main()
{
Uoperator tt;
--tt; // calling of a function "void operator --()"
tt.display();
return 0;
}
Output:
The Value of num after overloaded unary decrement is: 6
public:
void display()
{cout<<x<<" "<<y<<" "<<z<<endl; }
int main()
{
uminus um; um.getdata(10,-20,30);
um.display();
-um;
cout<<"after negation\n";
um.display(); }
12
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Output:
10 -20 30
after negation
-10 20 -30
13
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
b = x.b;
}
int main()
{ constructor c1;
constructor c2(10,20);
cout<< "The default constructor"<<endl;
c1.display();
cout<< "The overloaded constructor"<<endl;
c2.display();
//overloading assignment operator
c1 = c2;
cout<< "The assignment operator overloaded"<<endl;
c1.display();
return 0;
}
Output:
The default constructor
a:0 b:0
area of rectangle is : 0
The overloaded constructor
a:10 b:20
area of rectangle is : 200
The assignment operator overloaded
a:10 b:20
area of rectangle is : 200
Note: the object c1 data members a and b are assigned 10, 20 using assignment operator
overloading
14
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Overloading Binary Operators
An binary operator means, an operator which works on two operands. For example, + is
an binary operator, it takes two operand (c+d). So, when overloading an binary operator,
it takes one argument (one is object itself and other one is passed argument).
Syntax for Binary Operator (Inside a class)
return-type operator operatorsymbol (argument)
{ //body of the function }
Syntax for Binary Operator definition (Outside a class)
return-type classname:: operator operatorsymbol (argument)
{ //body of the function }
friend complex operator +(complex &ob1, complex &ob2); // using friend function
16
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
void display()
{ cout << a << "+" << b << "i" << "\n"; }
};
complex operator +(complex &obj1, complex &obj2)
{ complex t;
t.a = obj1.a + obj2.a;
t.b = obj2.b + obj2.b;
return (t); }
int main()
{ complex obj1, obj2, result, result1;
obj1.getdata(); obj2.getdata();
result = obj1 + obj2;
cout << "Input Values:\n";
obj1.display();
obj2.display();
cout << "Result:";
result.display();
return 0; }
Output:
Enter the value of Complex Numbers a,b:Enter the value of Complex Numbers a,b:Input
Values:
4+3i
2+1i
Result:6+2i
17
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
The derived class inherits the features( data and methods) from the base class and can
have additional features( data and methods) of its own
During the process of inheritance, the base class remains unchanged
Reuses existing code eliminating tedious, error prone task of developing new code.
private
Private is the highest level of data hiding. When a base class is privately
inherited by a derived class, then
Public and protected members of the base class become private members of the
derived class.
Private members cannot be accessed
19
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
public:
Public is the lowest and the most open level of data hiding. When a base class is
publicly inherited by a derived class, then
public members of the base class become public members of the derived class.
protected members of the base class become protected members of the derived
class
Private members cannot be accessed
protected:
The protected visibility mode allows the derived class to access
public and protected members of the base class in the protected mode.
20
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Types of Inheritance in C++
Single Inheritance: one derived class inherits from one base class.
Multiple Inheritance: one derived class inherits from multiple base class(es)
Multilevel Inheritance: wherein subclass acts as a base class for other classes. i.e a
derived class is created from another derived class.
Hierarchical Inheritance: wherein multiple subclasses inherited from one base class
Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more
than one type of inheritance. reflects any legal combination of other four types of
inheritance
21
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
UML Interaction Diagrams, Sequence Diagram, Collaboration Diagram,
Example Diagram
In OOD dynamic modeling can be represented by following diagrams
Behavior Diagram / Interaction diagram : Sequence diagrams, Collaboration
diagrams
Statechart Diagram
Activity Diagram
Behavior Diagram / Interaction diagram
are used in UML to establish communication between objects.
capture the dynamic behavior of any system.
mostly focus on message passing and how these messages make up one functionality of a
system.
It is a diagram that describes how groups of objects collaborate to get the job done
The critical component in an interaction diagram is lifeline and messages.
22
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
A message to create an instance
A message to destroy an instance
For sending a signal
Example: Synchronous message (sender of a message keeps waiting for the
receiver), Asynchronous message (The sender does not wait for a return from the
receiver), Object destruction (The sender destroys the created instance), Lost
message (The message never reaches the destination, and it is lost in the
interaction)
Operator: An operator specifies an operation on how the operands are going to be
executed.
Operator Name of the Operator Meaning
An operand is executed if the condition is true. e.g., If
Opt Option
else
The operand, whose condition is true, is executed. e.g.,
Alt Alternative
switch
Loop Loop It is used to loop an instruction for a specified period.
Par Parallel All operands are executed in parallel.
State invariants and constraints: When an instance or a lifeline receives a message, it can
cause it to change the state, upon it satisfies some constraint.
In an interaction and sequence diagram Iteration, Branching can also represented.
Iteration: An iteration expression consists of an iteration specifier and an optional
iteration clause, used to denote iteration using an iteration expression.
Branching: We can represent branching by adding guard conditions to the messages.
Guard conditions are used to check if a message can be sent forward or not.
Sequence diagram
Used to visualize the sequence of a message flow in the system. It shows the interaction
between two lifelines as a time-ordered sequence of events
It shows the objects participating in the interaction by their life lines and the messages
they exchange, arranged in a time sequence.
In a sequence diagram, a lifeline is represented by a vertical bar.
23
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
A message flow between two or more objects is represented using a vertical dotted line
which extends across the bottom of the page.
24
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Example 2 :Sequence Diagram for Withdrawing Amount from ATM
Collaboration diagram
Collaboration Diagram depicts the relationships and interactions among software objects.
They are used to understand the object architecture, focus on the element within a system
rather than focusing the flow of a message as in a sequence diagram.
They are also known as “Communication Diagrams.”
Sequence diagrams can be easily converted into a collaboration diagram.
25
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
Example for Communication/collaboration diagram:
For Student Management System
26
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST
To Deposit Cash in ATM Machine
References:
1. Reema Thareja, Object Oriented Programming with C++, 1st ed., Oxford University Press,
2015
2. https://www.programiz.com/cpp-programming
3. https://www.codesdope.com/practice/practice_cpp/
4. https://www.tutorialspoint.com/cplusplus/
5. https://www.javatpoint.com/cpp-tutorial
6. https://www.sitesbay.com/cpp/index
7. https://docs.staruml.io/working-with-uml-diagrams/use-case-diagram
8. https://www.guru99.com/interaction-collaboration-sequence-diagrams-examples.html
27
Prepared by Dr J Faritha Banu, Assistant Professor/ SRMIST