oops lab 2
oops lab 2
2
DD/MM/YYYY
Lab outcomes:
After completing this lab, students will be able to;
Understand how constructors initialize objects and destructors handle
cleanup.
Implement default, parameterized, and copy constructors.
Apply constructor overloading and memory management in C++
classes.
Theory:
In C++, constructors and destructors are special member functions of a class that are used to
initialize and clean up objects when they are created and destroyed, respectively.
1. Constructors:
o Purpose: Initialize an object when it is created.
o A constructor has the same name as the class.
o It does not have a return type, not even void.
o It can be overloaded to provide different ways to initialize objects.
The primary role of a constructor is to ensure that an object is in a valid state when it is
first created. For example, in a class Person, the constructor may initialize the name, age,
and address fields of a person.
2. Destructors:
o Purpose: Clean up any resources an object holds before it is destroyed.
o A destructor has the same name as the class but is preceded by a tilde ( ~).
o Like constructors, destructors do not have a return type, and they cannot be
overloaded.
o Destructors are primarily used to release memory or resources like file handles,
network connections, etc.
1. Default Constructor:
o A constructor that takes no parameters or has default values for all its parameters.
o If no constructor is defined by the user, the compiler automatically provides a
default constructor.
o A default constructor is useful when you want to create objects without providing
any arguments at the time of creation.
2. Parameterized Constructor:
o A constructor that takes parameters to initialize an object with specific values.
o It allows an object to be initialized with custom values when it is created.
3. Copy Constructor:
o A constructor that initializes a new object as a copy of an existing object.
o It takes a reference to an object of the same class as a parameter.
o The copy constructor is used when objects are passed by value or returned by
value from functions.
1. Default Constructor
The default constructor does not take any parameters and initializes an object with default values
#include<iostream>
using namespace std;
class Person {
string name;
int age;
public:
// Default Constructor
Person() {
name = "Unknown";
age = 0;
cout << "Default Constructor called!" << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p1; // Default constructor is called
p1.display();
return 0;
}
Output:
Default Constructor called!
Name: Unknown, Age: 0
2. Parameterized Constructor
A parameterized constructor takes parameters and uses them to initialize
the object.
#include<iostream>
using namespace std;
class Person {
string name;
int age;
public:
// Parameterized Constructor
Person(string n, int a) {
name = n;
age = a;
cout << "Parameterized Constructor called!" << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p1("John", 25); // Parameterized constructor is called
p1.display();
return 0;
}
Output
Parameterized Constructor called!
Name: John, Age: 25
3. Copy Constructor
The copy constructor is invoked when an object is copied to initialize another
object of the same class.
#include<iostream>
using namespace std;
class Person {
string name;
int age;
public:
// Parameterized Constructor
Person(string n, int a) {
name = n;
age = a;
cout << "Parameterized Constructor called!" << endl;
}
// Copy Constructor
Person(const Person &p) {
name = p.name;
age = p.age;
cout << "Copy Constructor called!" << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p1("Alice", 30); // Parameterized constructor is called
Person p2 = p1; // Copy constructor is called
p2.display();
return 0;
}
Output
Parameterized Constructor called!
Copy Constructor called!
Name: Alice, Age: 30
4. Destructor
The destructor is automatically called when an object goes out of scope or is
explicitly deleted. Its role is to release any resources allocated during the
lifetime of the object.
#include<iostream>
using namespace std;
class Person {
string name;
int age;
public:
// Parameterized Constructor
Person(string n, int a) {
name = n;
age = a;
cout << "Constructor called!" << endl;
}
// Destructor
~Person() {
cout << "Destructor called for " << name << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p1("Charlie", 40); // Constructor is called
p1.display();
return 0;
}
Output
Constructor called!
Name: Charlie, Age: 40
Destructor called for Charlie
OUTPUT:
Img 4: Lab Task 2
OUTPUT:
Img 6: Lab Task 3
OUTPUT:
Img 8: Lab Task 4