0% found this document useful (0 votes)
6 views

Week 5

The document contains code snippets for various C++ classes demonstrating concepts like constructor overloading, function overloading, and operator overloading. It includes a BankAccount class for managing bank accounts, a Complex class for handling complex numbers, and a Car class for a car rental system. Each class showcases specific functionalities such as deposits, withdrawals, and booking cars with different pricing strategies.

Uploaded by

arpita87904
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Week 5

The document contains code snippets for various C++ classes demonstrating concepts like constructor overloading, function overloading, and operator overloading. It includes a BankAccount class for managing bank accounts, a Complex class for handling complex numbers, and a Car class for a car rental system. Each class showcases specific functionalities such as deposits, withdrawals, and booking cars with different pricing strategies.

Uploaded by

arpita87904
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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;
}

Ans The code will not run successfully due to the missing default constructor.
To fix it, either pass an argument to the constructor or define a default constructor.

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.
o Function Overloading to deposit and withdraw using different data types.
o Operator Overloading to compare two accounts based on balance.

Implement a BankAccount class that meets these requirements.

#include <iostream>
using namespace std;

class BankAccount {
int accountNumber;
double balance;

public:
// Constructor Overloading
BankAccount(int accNum, double bal) {
accountNumber = accNum;
balance = bal;
}

BankAccount(int accNum) { // For default balance 0


accountNumber = accNum;
balance = 0.0;
}

// Function Overloading for deposit


void deposit(double amount) {
if (amount > 0)
balance += amount;
}

void deposit(int amount) { // Deposit with integer amount


if (amount > 0)
balance += amount;
}

// Function Overloading for withdraw


void withdraw(double amount) {
if (balance >= amount && amount > 0)
balance -= amount;
}

void withdraw(int amount) { // Withdraw with integer amount


if (balance >= amount && amount > 0)
balance -= amount;
}

// Operator Overloading to compare balances


bool operator >(const BankAccount &other) {
return balance > other.balance;
}

// Display Account details


void display() {
cout << "Account Number: " << accountNumber << endl;
cout << "Balance: " << balance << endl;
}
};

int main() {
BankAccount acc1(101, 5000);
BankAccount acc2(102, 3000);

acc1.deposit(1500.50);
acc2.withdraw(500);

acc1.display();
acc2.display();

if (acc1 > acc2)


cout << "Account 1 has more balance than Account 2" << endl;
else
cout << "Account 2 has more balance than Account 1" << endl;

return 0;
}

3. Complete the following code to overload the << operator to print the object's value using cout.
#include <iostream>
using namespace std;
class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
// Overload << operator here
void display() {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(3, 4);
cout << c1; // Expected Output: 3 + 4i
return 0;
}
Demonstrate the ticket booking and cancellation process with multiple objects.

Ans The complete code is


#include <iostream>
using namespace std;
class Complex {
int real, imag;

public:
Complex(int r, int i) : real(r), imag(i) {}

// Overload << operator here


friend ostream& operator<<(ostream& out, const Complex& c) {
out << c.real << " + " << c.imag << "i";
return out; // Return the stream to allow chaining
}

void display() {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(3, 4);
cout << c1; // Expected Output: 3 + 4i
return 0;
}

This code demonstrates how to create multiple ticket objects, handle ticket bookings and
cancellations, and use static members to manage ticket IDs across different instances of the
Ticket class.

4. 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

#include <iostream>
using namespace std;

class Car {
string model;
double pricePerDay;
bool isAvailable;

public:
// Default Constructor
Car() : model("Unknown"), pricePerDay(0.0), isAvailable(false) {}

// Parameterized Constructor
Car(string m, double price, bool available) : model(m), pricePerDay(price),
isAvailable(available) {}

// Copy Constructor
Car(const Car &c) : model(c.model), pricePerDay(c.pricePerDay), isAvailable(c.isAvailable) {}

// Function Overloading to Book a Car


double bookCar(int days) {
if (isAvailable) {
return pricePerDay * days;
}
return 0.0; // Car not available
}

double bookCar(int days, double discountPercentage) {


if (isAvailable) {
double totalPrice = pricePerDay * days;
return totalPrice - (totalPrice * discountPercentage / 100);
}
return 0.0; // Car not available
}

double bookCar(int days, double discountPercentage, double insurance) {


if (isAvailable) {
double totalPrice = pricePerDay * days;
totalPrice -= (totalPrice * discountPercentage / 100);
totalPrice += insurance; // Add insurance cost
return totalPrice;
}
return 0.0; // Car not available
}
// Operator Overloading to Compare Price Per Day of Two Cars
bool operator >(const Car &c) {
return pricePerDay > c.pricePerDay;
}

// Operator Overloading to Combine Rental Price of Two Cars


double operator +(const Car &c) {
return pricePerDay + c.pricePerDay;
}

// Display car details


void display() {
cout << "Car Model: " << model << endl;
cout << "Price Per Day: " << pricePerDay << endl;
cout << "Availability: " << (isAvailable ? "Available" : "Not Available") << endl;
}
};

int main() {
Car car1("Toyota", 50.0, true);
Car car2("BMW", 100.0, true);

car1.display();
car2.display();

double totalPrice = car1.bookCar(5); // Book car for 5 days


cout << "Total Price for car1: " << totalPrice << endl;

double totalPriceWithDiscount = car1.bookCar(5, 10); // Apply 10% discount


cout << "Total Price for car1 with discount: " << totalPriceWithDiscount << endl;

double totalPriceWithInsurance = car1.bookCar(5, 10, 20); // Add insurance cost


cout << "Total Price for car1 with insurance: " << totalPriceWithInsurance << endl;

if (car1 > car2) {


cout << "Car 1 is more expensive to rent per day" << endl;
} else {
cout << "Car 2 is more expensive to rent per day" << endl;
}

double combinedPrice = car1 + car2; // Combine prices of two cars


cout << "Combined Price for both cars: " << combinedPrice << endl;

return 0;
}

You might also like