Unit 2 OODP
Unit 2 OODP
PROGRAMMING
( PROFESSIONAL CORE)
L-2, T-1, C-3
TOPICS
Constructors- Types of constructors – Static
constructor and Copy constructor -Destructor -
Polymorphism: Constructor overloading -
Method Overloading Operator Overloading -
UML Interaction Diagrams -Sequence Diagram
- Collaboration Diagram - Example Diagram
(9 hours)
Constructors
• A constructor in C++ is a special method that is
automatically called when an object of a class is
created.
• The constructor in C++ has the same name as the
class or structure.
• Constructor does not have a return value, hence they
do not have a return type.
• The prototype of Constructors is as follows:
<class-name> (list-of-parameters){
//initialize values to the data members
}
Constructors Example
class MyClass { // The class
public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
Parameterized Constructor
• Constructors can also take parameters (just like regular functions), which can be
useful for setting initial values for attributes.
Example
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z; } };
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;
}
Constructors defined outside the
class
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z); // Constructor declaration
};
// Constructor definition outside the class
Car::Car(string x, string y, int z) {
brand = x;
model = y;
year = z; }
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;
}
Types of Constructors
• Default/Static Constructors: Default constructor is
the constructor which doesn’t take any argument.
It has no parameters. It is also called a
zero-argument constructor.
• Parameterized Constructors: It is possible to pass
arguments to constructors. Typically, these
arguments help initialize an object when it is
created.
• Copy Constructor: A copy constructor is a member
function that initializes an object using another
object of the same class.
Default/Static Constructor
• A constructor which has no argument is known as default constructor. It is
invoked at the time of creating object.
Example
#include <iostream>
using namespace std;
class Employee {
public:
Employee() {
cout<<"Default Constructor Invoked"<<endl; }
};
int main(void) {
Employee e1; //creating an object of Employee
Employee e2;
return 0; }
Parameterized Constructor
• A constructor which has parameters is called parameterized constructor. It is used to
provide different values to distinct objects.
Example
#include <iostream>
using namespace std;
class Employee {
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Copy Constructor
• A member function known as a copy constructor initializes an item using another object
#include <iostream>
using namespace std;
from the same class class Employee {
Example private:
string name;
int age;
#include <iostream> public:
// Default Constructor
using namespace std; Employee() {
name = "John Doe";
// declare a class age = 30;
cout << "Default Constructor Invoked" << endl;
class Wall { }
// Parameterized Constructor
private: Employee(string newName, int newAge) {
name = newName;
double length; age = newAge;
cout << "Parameterized Constructor Invoked" << endl;
double height; }
} };
} }
return 0;
};
Destructor
• A destructor is also a special member function as a constructor.
• Destructor destroys the class objects created by the constructor.
• Destructor has the same name as their class name preceded by
a tilde (~) symbol. It is not possible to define more than one
destructor.
• The destructor is only one way to destroy the object created by
the constructor. Hence destructor can-not be overloaded.
• Destructor neither requires any argument nor returns any value.
It is automatically called when the object goes out of scope.
• Destructors release memory space occupied by the objects
created by the constructor.
• In destructor, objects are destroyed in the reverse of object
creation.
Destructor
• The syntax for defining the destructor within the class
~ <class-name>()
{
}
• Example
#include <iostream>
using namespace std;
class Test {
public:
Test() {
cout << "\n Constructor executed"; }
~Test()
Polymorphism
}
Operator Overloading