PL PL
PL PL
Create a class called ‘e-bill’ with data members as integer ‘u’ deno ng the amount of
units of electricity consumed. The task is to calculate electricity bill with the help of
following charges.
1-100 units – Rs 5 per unit
100-200 units – Rs 10 per unit
200-300 units – Rs 15 per unit
Above 300 units – Rs 20 per unit
Aim- To create a class that calculates the electricity bill based on the number of units
consumed.
Program –
#include <iostream>
using namespace std;
class EBill
{
private:
int units;
public:
EBill(int u) : units(u)
{
}
double calculateBill()
{
double bill = 0;
1
else
{
return bill;
}
};
int main()
{
int unitsConsumed;
cout << "Enter the number of units consumed: "<<endl;
cin >> unitsConsumed;
EBill myBill(unitsConsumed);
double totalBill = myBill.calculateBill();
cout << "The total electricity bill for " << unitsConsumed
<< " units is: Rs " << totalBill <<endl;
return 0;
}
Output –
2
2. Write a C++ program for constructor overloading (use all three types of constructors).
#include<iostream>
using namespace std;
class complex
{
float real,img;
public:
complex ()
{
real=0;
img=0;
}
complex(float r, float i)
{
real=r;
img=i;
}
complex(complex &ob)
{
real=ob.real;
img=ob.img;
}
void show()
{
cout<<real <<"+"<< img<<"i"<<endl;
}
};
int main()
{
complex c1,c2(3,7),c3(c2);
c1.show();
c2.show();
c3.show();
return 0;
}
3
Output-
4
3. Write a C++ program to count the no. of objects created.
class Counter {
private:
sta c int objectCount;
public:
Counter() {
objectCount++;
}
int Counter::objectCount = 0;
int main() {
Counter obj1;
Counter obj2;
Counter obj3;
cout << "Number of objects created: " << Counter::getObjectCount() << endl;
return 0;
}
5
Output-
6
4. Create a class called ‘Bank Account’ that has private member variables for account no.
and balance. Include member func ons to deposit and withdraw money from the
account and display the balance amount for n customers.
#include <iostream>
using namespace std;
class BankAccount
{
private:
int account_no;
double balance;
public:
BankAccount(int acc_no, double ini al_balance)
{
account_no = acc_no;
balance = ini al_balance;
}
void deposit(double amount)
{
if (amount > 0)
{
balance += amount;
cout << "Deposited: " << amount << ". New Balance: " << balance << endl;
} else
{
cout << "Invalid deposit amount." << endl;
}
}
void withdraw(double amount)
{
if (amount > 0 && balance >= amount)
{
balance -= amount;
cout << "Withdraw: " << amount << ". New Balance: " << balance << endl;
} else if (amount <= 0)
{
cout << "Invalid withdrawal amount." << endl;
}
else
7
{
cout << "Insufficient funds." << endl;
}
}
void displayBalance() const
{
cout << "Account Number: " << account_no << ", Balance: " << balance << endl;
}
};
int main()
{
account1.deposit(525.0);
account2.withdraw(700.0);
account1.displayBalance();
account2.displayBalance();
return 0;
}
Output-
8
5. Write a C++ program with ‘Employee’ as class. Display the salary of programmer,
developer, trainer, tester by using is A.
Aim- To create a class and display the salary of programmer, developer, trainer, tester.
Program-
#include <iostream>
using namespace std;
class Employee
{
public:
virtual int salary()
{
return 0;
}
};
9
{
public:
int salary()
{
return 20000;
}
};
int main()
{
Programmer p1;
Developer d1;
Trainer t1;
Tester t2;
return 0;
}
Output –
10
6.Write a C++ program for hybrid inheritance.
Aim- To write a C++ program for hybrid inheritance
Program-
#include <iostream>
using namespace std;
class A {
public:
void showA()
{
cout << "A class" << endl;
}
};
class B : public virtual A {
public:
void showB() {
cout << "B class" << endl;
}
};
class C : public virtual A {
public:
void showC() {
cout << "C class" << endl;
}
};
11
}
};
int main() {
D obj;
obj.showD();
obj.showC();
obj.showB();
obj.showA();
return 0;
}
Output-
12
7. Write a C++ program to overload ‘+’ operator.
13
c2.display();
cout << "Sum of complex numbers: ";
sum.display();
return 0;
}
Output-
14
8. Write a C++ program with super outer class, outer class, inner class. Allow inner class to
access data members of super outer class, outer class and inner class.
Aim- To write a C++ program that allows inner class to access data members of super outer
class, outer class and inner class.
Program-
#include <iostream>
using namespace std;
class SuperOuter {
protected:
int superOuterData = 10;
public:
class Outer {
private:
int outerData = 20;
public:
class Inner {
public:
void accessData(const SuperOuter& superOuterObj, const Outer& outerObj) {
cout << "Super Outer Data: " << superOuterObj.superOuterData << endl;
cout << "Outer Data: " << outerObj.outerData << endl;
}
};
};
};
int main() {
SuperOuter superOuter;
SuperOuter::Outer outer;
SuperOuter::Outer::Inner inner;
inner.accessData(superOuter, outer);
return 0;
}Output-
15
9. Create a template to find maximum value from the given two values. Values can be
integer, float, character, double.
Aim- To find maximum value from two given values
Program-
#include <iostream>
Using namespace std;
template<typename T>
T findMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
int intMax = findMax(10, 20);
float floatMax = findMax(3.14f, 2.71f);
char charMax = findMax('a', 'z');
double doubleMax = findMax(3.14159, 2.71828);
cout << "Maximum of 10 and 20 is: " << intMax << endl;
cout << "Maximum of 3.14 and 2.71 is: " << floatMax <<endl;
cout << "Maximum of 'a' and 'z' is: " << charMax <<endl;
cout << "Maximum of 3.14159 and 2.71828 is: " << doubleMax << endl;
return 0;
}
Output-
16
10. Write a C++ program to divide two numbers. Throw an excep on when divisor is 0.
Aim- To write a program to divide two numbers
Program-
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter two numbers: "<<endl;
cin>>a>>b;
try
{
if (b==0)
{
throw(b);
}
else
{
cout<<b/a;
}
}
catch (int b)
{
cout<<"Can't divide with zero"<<endl;
}
return 0;
}
Output-
17