PL Finall

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

(1) Aim: Write a C++ program to count the number of objects created

Code:

Output:
(2) Aim: Create a class called bank acc that has private member variables for acc no and balance.
Include member function to deposite and withdraw money from the acc and display the balance
amount for n consumers

Code:

#include <iostream>

class BankAccount {

private:

int accNo;

double balance;

public:

BankAccount(int accNo, double balance) : accNo(accNo), balance(balance) {}

void deposit(double amount) {

if (amount > 0) {

balance += amount;

cout << "Deposit successful for account " << accNo << endl;

} else {

cout << "Invalid deposit amount for account " << accNo << endl;

void withdraw(double amount) {

if (amount > 0 && amount <= balance) {


balance -= amount;

cout << "Withdrawal successful for account " << accNo << endl;

} else {

cout << "Insufficient funds or invalid withdrawal amount for account " << accNo << endl;

void displayBalance() const {

cout << "Account Number: " << accNo << endl;

cout << "Balance: " << balance << endl;

};

int main() {

int n;

cout << "Enter the number of consumers (accounts): ";

cin >> n;

for (int i = 0; i < n; ++i) {

int accNo;

double balance;

cout << "Enter account number for consumer " << i + 1 << ": ";

cin >> accNo;

cout << "Enter initial balance for consumer " << i + 1 << ": ";
cin >> balance;

BankAccount account(accNo, balance);

int choice;

do {

cout << "\nMenu for account " << accNo << endl;

cout << "1. Deposit\n";

cout << "2. Withdraw\n";

cout << "3. Display Balance\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: {

double depositAmount;

cout << "Enter amount to deposit: ";

cin >> depositAmount;

account.deposit(depositAmount);

break;

case 2: {

double withdrawAmount;

cout << "Enter amount to withdraw: ";


cin >> withdrawAmount;

account.withdraw(withdrawAmount);

break;

case 3:

account.displayBalance();

break;

default:

cout << "Invalid choice." << endl;

} while (choice != 4);

return 0;

Output:

Enter the number of consumers (accounts): 2

Enter account number for consumer 1: 1234

Enter initial balance for consumer 1: 1000.0

Enter account number for consumer 2: 5678

Enter initial balance for consumer 2: 2500.0


Menu for account 1234

1. Deposit

2. Withdraw

3. Display Balance

4. Exit

Enter your choice: 1

Enter amount to deposit: 500.0

Deposit successful for account 1234

Menu for account 1234

1. Deposit

2. Withdraw

3. Display Balance

4. Exit

Enter your choice: 2

Enter amount to withdraw: 750.0

Withdrawal successful for account 1234

Menu for account 1234

1. Deposit

2. Withdraw

3. Display Balance

4. Exit
Enter your choice: 3

Account Number: 1234

Balance: 750.0

Menu for account 1234

1. Deposit

2. Withdraw

3. Display Balance

4. Exit

Enter your choice: 4

Menu for account 5678

1. Deposit

2. Withdraw

3. Display Balance

4. Exit

Enter your choice: 3

Account Number: 5678

Balance: 2500.0
(3) Aim: Write a C++ program to print complex no. using constructor overloading ( use all three
types of constructor)

Code:

#include <iostream>

class Complex {

private:

double real;

double imag;

public:

Complex() : real(0.0), imag(0.0) {}

Complex(double real) : real(real), imag(real) {}

Complex(const Complex& other) : real(other.real), imag(other.imag) {}

void display() const {

cout << real << (imag >= 0 ? "+" : "") << imag << "i" << endl;

};
int main() {

Complex c1;

c1.display();

Complex c2(5.0);

c2.display();

// Using copy constructor

Complex c3(c2);

c3.display();

return 0;

Output:

5+5i

5+5i
(4) Aim: Creta a class called e- bill with data members as integers 'u' denoting the amount of units of
electricity consumed, the task is to calculate the electricity bill with the help of below charges

(1 to 100 units -- 5 per unit)

(100 to 200 -- 10 per unit)

( 200 to 300 -- 15 per unit)

( above 300 --- 20 per unit)

Code:

#include <iostream>

Using namespace std;

class EBill {

private:

int units;

public:

EBill(int units) : units(units) {}

int calculateBill() {

int billAmount = 0;

if (units <= 100) {

billAmount = units * 5;

} else if (units <= 200) {

billAmount = 100 * 5 + (units - 100) * 10;

} else if (units <= 300) {

billAmount = 100 * 5 + 100 * 10 + (units - 200) * 15;


} else {

billAmount = 100 * 5 + 100 * 10 + 100 * 15 + (units - 300) * 20;

return billAmount;

void displayBill() {

cout << "Units Consumed: " << units << endl;

cout << "Bill Amount: " << calculateBill() << endl;

};

int main() {

int consumedUnits;

cout << "Enter the number of units consumed: ";

cin >> consumedUnits;

EBill bill(consumedUnits);

bill.displayBill();

return 0;

}
OUTPUT:

Enter the number of units consumed: 150

Units Consumed: 150

Bill Amount: 1000


(5) Aim: Create a class called employee.Display the salary of software developer, trainer, tester, and
programmer. Use is-a relationship( inheritance)

Code:

#include <iostream>

Using namespace std;

class Employee {

public:

virtual double calculateSalary() const = 0; implementation in derived classes)

void displaySalary() const {

cout << "Employee Salary: " << calculateSalary() << endl;

};

class SoftwareDeveloper : public Employee {

private:

static const double baseSalary = 1200.0;

public:

double calculateSalary() const override {

return baseSalary + (baseSalary * 0.2);

};

class Trainer : public Employee {

private:

static const double baseSalary = 1100.0;


public:

double calculateSalary() const override {

return baseSalary + (baseSalary * 0.1);

};

class Tester : public Employee {

private:

static const double baseSalary = 1000.0;

public:

double calculateSalary() const override {

return baseSalary; // No bonus

};

class Programmer : public Employee {

private:

static const double baseSalary = 1050.0;

public:

double calculateSalary() const override {

return baseSalary + (baseSalary * 0.05); // 5% bonus

}
};

int main() {

SoftwareDeveloper dev;

Trainer trainer;

Tester tester;

Programmer programmer;

dev.displaySalary();

trainer.displaySalary();

tester.displaySalary();

programmer.displaySalary();

return 0;

Output:

Employee Salary: 1440.0(Software Developer)

Employee Salary: 1210.0 (Trainer)

Employee Salary: 1000.0 (Tester)

Employee Salary: 1102.5 (Programmer)


(6) Aim: Write a C++ program with super outer class, outer class and inner class. Allow inner class to
access the data members of outer class and super outer class.

Code:

Output:
(7) Aim: For hybrid inheritance for following scenario.

"Ferrari" Class derived from car and racing class, car class is derived from class vehicle.

Code:
Output:
(8) Aim: Write a C++ program to overload '+' operator.

Code:

Output:
(9) Aim: Create a template to find maximum value from given to values. ( values can be character,
integer, double or float).

Code:
(10)Aim: Write a C++ program to divide two numbers, throws an exception when the divisor is zero.

Code:

You might also like