0% found this document useful (0 votes)
7 views4 pages

Oodp W5

This document contains tutorial questions and code examples related to Object Oriented Design and Programming. It covers topics such as constructor, method, and operator overloading through practical examples including a Bank Account class and a Car Rental System. The document also includes explanations of errors in code and expected outputs for the provided examples.

Uploaded by

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

Oodp W5

This document contains tutorial questions and code examples related to Object Oriented Design and Programming. It covers topics such as constructor, method, and operator overloading through practical examples including a Bank Account class and a Car Rental System. The document also includes explanations of errors in code and expected outputs for the provided examples.

Uploaded by

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

21CSC201T – Object Oriented Design

and Programming Week - 5 – Tutorial


Questions Constructor, Method and Operator
Overloading
1. Will the following code run successfully? If not, what modifications are required?
#include <iostream>
using namespace std;
class Example {
int x;
public:
Example(int a) { x = a; }
void show() { cout << "Value: " << x << endl; }
};
int main() {
Example obj; // Error? Why?
obj.show();
return 0;
}
No, the code will not run successfully. The error occurs because the class Example has a
parameterized constructor, and no default constructor is defined.

CODE:-

#include <iostream>
using namespace std;
class Example {
int x;
public:
Example(int a) { x = a; }
void show() { cout << "Value: " << x << endl; }
};
int main() {
Example obj(5); // Error? Why?
obj.show();
return 0;
}

OUTPUT:-
Value: 5

2. Design a Bank Account class where:


• The account should be initialized with an account number and balance.
• The account should support: o Constructor Overloading for creating different types
of accounts.
• Function Overloading to deposit and withdraw using different data types.
• Operator Overloading to compare two accounts based on balance. Implement a
BankAccount class that meets these requirements.

CODE:-

#include <iostream>
using namespace std;

class BankAccount {
int accNo;
double balance;

public:
BankAccount(int a, double b = 0) : accNo(a), balance(b) {} // Constructor
Overloading

void deposit(double amt) { balance += amt; }


void withdraw(double amt) { if (amt <= balance) balance -= amt; }
bool operator>(const BankAccount &b) { return balance > b.balance; } // Operator
Overloading

void display() { cout << "Account: " << accNo << ", Balance: " << balance <<
endl; }
};

int main() {
BankAccount a1(101, 5000), a2(102, 3000);
a1.deposit(2000);
a2.withdraw(500);

a1.display();
a2.display();
}

OUTPUT:-

Account: 101, Balance: 7000


Account: 102, Balance: 2500

3. . Develop a Car Rental System where i) Constructor Overloading is used for


initializing the class ‘Car’ with
• Default constructor (sets default values).
• Car model, price per day, and availability status.
• Copy constructor to create a duplicate car. ii) Function Overloading s used by the
customers to book a car in different ways as:
• By specifying the number of days (default price applies).
• By specifying a discount percentage.
• By specifying an extra insurance amount. iii) Operator Overloading where
• The + operator should allow combining the total rental prices of two cars
• The > operator should compare which car is more expensive to rent per day

CODE:-

#include <iostream>
using namespace std;

class Car {
string model;
double pricePerDay;
public:
Car(string m = "Unknown", double price = 0.0) : model(m), pricePerDay(price) {}
void bookCar(int days, double discount = 0, double insurance = 0) {
cout << "Car booked for " << days << " days. Total: $" << (days * pricePerDay *
(1 - discount / 100) + insurance) << endl;
}
double operator+(const Car &c) { return pricePerDay + c.pricePerDay; }
bool operator>(const Car &c) { return pricePerDay > c.pricePerDay; }
};

int main() {
Car car1("Toyota", 50), car2("BMW", 100);
car1.bookCar(5);
car2.bookCar(3, 10);
cout << "Total price per day: $" << car1 + car2 << endl;
cout << (car2 > car1 ? "BMW is more expensive" : "Toyota is more expensive") <<
endl;
return 0;
}

OUTPUT:-

Car booked for 5 days. Total: $250


Car booked for 3 days. Total: $270
Total price per day: $150
BMW is more expensive

AARUSHI SARKAR
RA2411031010099
SEC: X-2

You might also like