0% found this document useful (0 votes)
24 views17 pages

PL PL

The program shows how to create a class that calculates electricity bill based on units consumed. It defines a class EBill with constructor that takes units as parameter. The calculateBill() method calculates bill amount based on slab rates and returns it.

Uploaded by

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

PL PL

The program shows how to create a class that calculates electricity bill based on units consumed. It defines a class EBill with constructor that takes units as parameter. The calculateBill() method calculates bill amount based on slab rates and returns it.

Uploaded by

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

1.

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;

if (units <= 100)


{
bill = units * 5;
}

else if (units <= 200)


{
bill = (100 * 5) + (units - 100) * 10;
}

else if (units <= 300)


{
bill = (100 * 5) + (100 * 10) + (units - 200) * 15;
}

1
else
{

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


}

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

Aim - To write a program for constructor overloading.


Program –

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

Aim- To count no. of objects created.


Program-
#include <iostream>
using namespace std;

class Counter {
private:
sta c int objectCount;

public:

Counter() {
objectCount++;
}

sta c int getObjectCount() {


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

Aim – To create a class that private member variables


Program-

#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()
{

BankAccount account1(1382, 1000.0);


BankAccount account2(6715, 2000.0);
account1.displayBalance();
account2.displayBalance();

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

class Programmer : public Employee


{
public:
int salary()
{
return 60000;
}
};

class Developer : public Employee


{
public:
int salary()
{
return 50000;
}
};

class Trainer : public Employee


{
public:
int salary()
{
return 40000;
}
};

class Tester : public Employee

9
{
public:
int salary()
{
return 20000;
}
};

int main()
{
Programmer p1;
Developer d1;
Trainer t1;
Tester t2;

cout << "Salary of Programmer = " << p1.salary() << endl;


cout << "Salary of Developer = " << d1.salary() << endl;
cout << "Salary of Trainer = " << t1.salary() << endl;
cout << "Salary of Tester = " << t2.salary() << endl;

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

class D : public B, public C {


public:
void showD() {
cout << "D 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.

Aim- To write a C++ program to overload ‘+’ operator


Program-
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex& other) {
Complex result;
result.real = this->real + other.real;
result.imag = this->imag + other.imag;
return result;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2.5, 3.7);
Complex c2(1.8, 4.2);
Complex sum = c1 + c2; // Using overloaded '+' operator
cout << "First complex number: ";
c1.display();
cout << "Second complex number: ";

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

You might also like