Oodp W5
Oodp W5
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
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 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:-
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:-
AARUSHI SARKAR
RA2411031010099
SEC: X-2