0% found this document useful (0 votes)
3 views11 pages

OOP-Weekend Lecture 03

The document explains the concepts of constructors and destructors in object-oriented programming, highlighting their key features and differences. It provides examples in C++ to illustrate how constructors initialize objects and destructors clean up resources. Additionally, it includes case studies demonstrating practical applications of these concepts in managing books, bank accounts, and car rentals.

Uploaded by

ayesha.widemedia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

OOP-Weekend Lecture 03

The document explains the concepts of constructors and destructors in object-oriented programming, highlighting their key features and differences. It provides examples in C++ to illustrate how constructors initialize objects and destructors clean up resources. Additionally, it includes case studies demonstrating practical applications of these concepts in managing books, bank accounts, and car rentals.

Uploaded by

ayesha.widemedia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

OBJECT ORIENTED PROGRAMING

1. What is a Constructor?

A constructor is a special function that initializes an object as soon as it is created.

✅ Key Features:

 Has the same name as the class.


 No return type (not even void).
 Runs automatically when an object is created.
 Can accept parameters to set values.

🔹 Example (C++):

#include <iostream>
using namespace std;

class Car {
public:
Car() { // Constructor
cout << "Car object created!" << endl;
}
};

int main() {
Car myCar; // Constructor is called
return 0;
}

🖥 Output:

Car object created!


2. What is a Destructor?

A destructor is a special function that is automatically called when an object is destroyed. It


helps free up memory and other resources.

✅ Key Features:

 Has the same name as the class but starts with ~ (tilde).
 No return type and no parameters.
 Runs automatically when an object is destroyed.

🔹 Example (C++):

#include <iostream>
using namespace std;

class Car {
public:
Car() { // Constructor
cout << "Car object created!" << endl;
}
~Car() { // Destructor
cout << "Car object destroyed!" << endl;
}
};

int main() {
Car myCar; // Constructor is called
return 0; // Destructor is called automatically at the end
}

🖥 Output:

Car object created!


Car object destroyed!
3. Constructor vs. Destructor

📌 How They Differ:

Feature Constructor Destructor


Purpose Sets up an object Cleans up an object
Runs When? When an object is created When an object is destroyed
Name Same as the class Same as the class with ~ before it
Parameters Can have parameters Cannot have parameters
Return Type No return type No return type

4. Constructor vs. Regular Function

📌 Key Differences:

Feature Constructor Regular Function


When It Runs Automatically when an object is created Only when called manually
Purpose Initializes an object Performs a specific task
Return Type No return type Can have any return type
Name Same as the class Can have any name

5. Why Are Constructors and Destructors Important?

✔ Makes object creation easier.


✔ Helps manage memory and resources efficiently.
✔ Reduces errors and makes code cleaner.

Using constructors and destructors correctly makes programming in OOP easier and more
efficient! 🚀
Case Study 1:

A library needs a system to manage books. Whenever a new book is added, a message should
display "Book added to the library!". When a book record is deleted, it should display "Book
removed from the library!".

Task:

 Create a class Book.


 Use a constructor to display "Book added to the library!" when a book object is created.
 Use a destructor to display "Book removed from the library!" when an object is
destroyed.

Solution:

#include <iostream>
using namespace std;

class Book {
public:
Book() { // Constructor
cout << "Book added to the library!" << endl;
}
~Book() { // Destructor
cout << "Book removed from the library!" << endl;
}
};

int main() {
Book b1; // Object creation
return 0;
}

Expected Output:

Book added to the library!


Book removed from the library!
Case Study 2:

A bank needs a program to create customer accounts. Each account should store the customer's
name and balance. When an account is created, a message should display account details.
When an account is deleted, a message should confirm its removal.

Task:

 Create a class BankAccount with attributes name and balance.


 Use a parameterized constructor to initialize the name and balance.
 Use a destructor to print "Account closed for [name]." when an object is destroyed.

Solution:

#include <iostream>
using namespace std;

class BankAccount {
string name;
double balance;
public:
BankAccount(string n, double b) { // Parameterized Constructor
name = n;
balance = b;
cout << "Account created for " << name << " with balance $" << balance << endl;
}
~BankAccount() { // Destructor
cout << "Account closed for " << name << "." << endl;
}
};

int main() {
BankAccount acc1("Alice", 5000.00);
return 0;
}

Expected Output:

Account created for Alice with balance $5000


Account closed for Alice.
Case Study 3:

A car rental service manages different car models. Each time a car is rented, a message should
display the car model. When the rental ends, the system should confirm the car is returned.

Task:

 Create a class Car with a constructor that prints "Car [model] rented!".
 Use a destructor to print "Car [model] returned!".
 Create multiple Car objects in main().
 Observe how destructors are called in reverse order.

Solution:

#include <iostream>
using namespace std;

class Car {
string model;
public:
Car(string m) { // Constructor
model = m;
cout << "Car " << model << " rented!" << endl;
}
~Car() { // Destructor
cout << "Car " << model << " returned!" << endl;
}
};

int main() {
Car car1("Tesla");
Car car2("BMW");
return 0;
}

Expected Output:

Car Tesla rented!


Car BMW rented!
Car BMW returned!
Car Tesla returned!
Can we copy the value of value of one object of class to another object of class?

Answer: Yes but not directly, only with the help of copy constructor.

Case Study 5:

An online shopping platform allows users to copy their cart items to a wishlist.

Task:

 Create a class Item that stores the item name.


 Implement a copy constructor that duplicates an existing item.
 Display both original and copied item names.

Solution:

#include <iostream>

using namespace std;

class Item{

private:

string itemname;

public:

Item(string b)

itemname=b;

cout<<itemname<<endl;

Item (Item &d)

itemname=d.itemname;
cout<<itemname<<endl;

};

int main()

Item i("laptop");

Item g=i;

return 0;

}
Can we declare function inside the class and define outside the class?

Yes, Possible

#include <iostream>

using namespace std;

class Item{

private:

string itemname;

public:

void display(string in);

};

void display(string in)

itemname=in;

cout<<itemname<<endl;

int main() {

Item i;

i.display("Laptop");

return 0;

}
Error!

How Possible?

Using Scope Resoultion Operator

#include <iostream>

using namespace std;

class Item{

private:

string itemname;

public:

void display(string in);

};

void Item::display(string in)

itemname=in;

cout<<itemname<<endl;

int main() {

Item i;
i.display("Laptop");

return 0;

You might also like