OOP-Weekend Lecture 03
OOP-Weekend Lecture 03
1. What is a Constructor?
✅ Key Features:
🔹 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:
✅ 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:
📌 Key Differences:
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:
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:
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:
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:
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:
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:
Solution:
#include <iostream>
class Item{
private:
string itemname;
public:
Item(string b)
itemname=b;
cout<<itemname<<endl;
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>
class Item{
private:
string itemname;
public:
};
itemname=in;
cout<<itemname<<endl;
int main() {
Item i;
i.display("Laptop");
return 0;
}
Error!
How Possible?
#include <iostream>
class Item{
private:
string itemname;
public:
};
itemname=in;
cout<<itemname<<endl;
int main() {
Item i;
i.display("Laptop");
return 0;