C++ PROGRAMMING LANGUAGE
Journal Ques:
1. An electricity board charges the following rates to user.
For the first 100 units 60p per unit.
For the next 200 units 80p per unit.
Beyond 300 units 90p per unit.
All users are charged a minimum of Rs. 50; if the total amount is more than 300
then an additional surcharges of 15% is added. Write a program to accept name
of user consumed and print charges with their rates.
Code
#include<iostream>
#include<iomanip>
using namespace std;
class electricitybill{
private:
string Username;
int units_consumed;
public:
electricitybill(string name,int units):
Username(name),units_consumed(units){};
double calculatebill(){
double totalamount=0.0;
if(units_consumed<=100){
totalamount=units_consumed*0.60;
}
else if(units_consumed<=300){
totalamount=units_consumed*0.60+(units_consumed-100)*0.80;
}
else{
totalamount=100*0.60+200*0.80+(units_consumed-300)*0.90;
}
totalamount<=100;
if(totalamount<50){
totalamount=50;
}
if(totalamount>300){
totalamount+=totalamount*0.15;
}
return totalamount;
}
void printbill(){
double billamount=calculatebill();
cout<<fixed<<setprecision(2);
cout<<"Username:"<<Username<<endl;
cout<<"Units Consumed:"<<units_consumed<<endl;
cout<<"Total Bill Amount:rs"<<billamount<<endl;
}
};
int main(){
string name;
int units;
cout<<"Enter the name of user:";
getline(cin,name);
cout<<"Enter the name of units consumed:";
cin>>units;
electricitybill bill(name,units);
bill.printbill();
return 0;
}
2. Define a class to represent a bank account. Include the following members:
a. Name of the depositor
b. Account number
c. Type of Account
d. Balance amount in the Account Member Functions:
• To assign initial values.
• To deposit an amount.
• To withdraw an amount after checking the balance.
• To display name and balance.
Write a program and handle accounts of 5 customers.
Code
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
string name;
int accountNumber;
string accountType;
double balance;
public:
BankAccount(string n, int accNum, string accType, double bal)
: name(n), accountNumber(accNum), accountType(accType), balance(bal) {}
void deposit(double amount) {
balance += amount;
cout << "Deposit successful. New balance: " << balance << endl;
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
cout << "Withdrawal successful. New balance: " << balance << endl;
} else {
cout << "Insufficient balance." << endl;
}
}
void display() const {
cout << "Name: " << name << endl;
cout << "Account Number: " << accountNumber << endl;
cout << "Account Type: " << accountType << endl;
cout << "Balance: " << balance << endl;
}
};
int main() {
const int NUM_ACCOUNTS = 5;
BankAccount accounts[NUM_ACCOUNTS] = {
BankAccount("John Doe", 1234, "Savings", 1000.0),
BankAccount("Jane Smith", 5678, "Current", 500.0),
BankAccount("Bob Johnson", 9012, "Savings", 2000.0),
BankAccount("Alice Williams", 3456, "Current", 1500.0),
BankAccount("Mike Davis", 7890, "Savings", 3000.0)
};
while (true) {
cout << "\nBank Account Management System" << endl;
cout << "1. Display Account Details" << endl;
cout << "2. Deposit Amount" << endl;
cout << "3. Withdraw Amount" << endl;
cout << "4. Exit" << endl;
cout << "Enter choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1: {
int accountIndex;
cout << "Enter account index (1-5): ";
cin >> accountIndex;
if (accountIndex >= 1 && accountIndex <= NUM_ACCOUNTS) {
accounts[accountIndex - 1].display();
} else {
cout << "Invalid account index." << endl;
}
break;
}
case 2: {
int accountIndex;
double amount;
cout << "Enter account index (1-5): ";
cin >> accountIndex;
cout << "Enter amount to deposit: ";
cin >> amount;
if (accountIndex >= 1 && accountIndex <= NUM_ACCOUNTS) {
accounts[accountIndex - 1].deposit(amount);
} else {
cout << "Invalid account index." << endl;
}
break;
}
case 3: {
int accountIndex;
double amount;
cout << "Enter account index (1-5): ";
cin >> accountIndex;
cout << "Enter amount to withdraw: ";
cin >> amount;
if (accountIndex >= 1 && accountIndex <= NUM_ACCOUNTS) {
accounts[accountIndex - 1].withdraw(amount);
} else {
cout << "Invalid account index." << endl;
}
break;
}
case 4:
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
3. Write a Program to create a class person having members name and age. Derive a class
student having member percentage. Derive another class teacher having member salary. Write
necessary member function to initialize, read and write data. Also write the main function.
Code
#include<iostream>
#include<string>
using namespace std;
class person{
protected:
string name;
int age;
public:
void initialize_person(const string & person_name,int person_age){
name=person_name;
age=person_age;
}
void readperson()const{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
}
};
class student:public person{
protected:
float percentage;
public:
void initialize_student(const string & student_name,int student_age, float student_percent){
initialize_person(student_name,student_age);
percentage=student_percent;
}
void readstudent()const{
readperson();
cout<<"percentage:"<<percentage<<"%"<<endl;
}
};
class teacher:public person{
protected:
float salary;
public:
void initialize_teacher(const string & teacher_name,int teacher_age, float teacher_salary){
salary=teacher_salary;
}
void readteacher()const{
readperson();
cout<<"salary:$"<<salary<<endl;
}
};
int main(){
student student;
student.initialize_student("shivam",20,89.5);
cout<<"student data:"<<endl;
student.readstudent();
cout<<endl;
teacher teacher;
teacher.initialize_teacher("Mr.Deo",25,45000);
cout<<"teacher data:"<<endl;
student.readperson();
cout<<endl;
return 0;
}
4. Write a Program to create a class name student having date member name, no & three
marks. Write a member function to input name, roll no & marks & calculate percentage.
Code
#include<iostream>
#include<string>
using namespace std;
class student{
private:
string name;
int rollno;
float marks[3];
public:
void inputdetails(){
cout<<"Enter student name:";
getline(cin,name);
cout<<"Enter roll number:";
cin>>rollno;
cout<<"Enter marks for three subjects:\n";
for(int i=0;i<3;i++){
cout<<"marks"<<(i+1)<<":";
cin>>marks[i];
}
}
float calculatepercentage()const{
float total=marks[0]+marks[1]+marks[2];
return(total/300.0)*100;
}
void displaydetails()const{
cout<<"\n student details:\n";
cout<<"name:"<<name<<endl;
cout<<"roll number:"<<rollno<<endl;
cout<<"marks:\n";
for(int i=0;i<3;i++){
cout<<"marks"<<(i+1)<<":"<<marks[i]<<endl;
}
};
};
int main(){
student student;
student.inputdetails();
student.displaydetails();
return 0;
}
5. Create one class time which has hour, minute and second as data member. Now write input
function to input class values and find time in the form of minute.
Code
#include<iostream>
using namespace std;
class time{
private:int hour;
int minute;
int second;
public:
void inputtime(){
cout<<"Enter hours:";
cin>>hour;
cout<<"Enter minute:";
cin>>minute;
cout<<"Enter second:";
cin>>second;
}
int convert_to_minutes()const{
return(hour*60)+minute+(second/60);
}
void display_time_in_minutes()const{
cout<<"total time in minutes:"<<convert_to_minutes()<<"minutes"<<endl;
}
};
int main(){
time time;
time.inputtime();
time.display_time_in_minutes();
return 0;
}
6. Write a program to create two classes: DATE and DMY and both classes are building to handled
dates. The difference between them DATE class handles it in string. Where as the DMY class
handles it has three integers representing day.
Code
#include<iostream>
#include<string>
using namespace std;
class DateString{
private:
string date;
public:
void setDate(const string & datestring){
date=datestring;
}
void displayDate()const{
cout<<"date(string format):"<<date<<endl;
}
};
class DMY{
private:
int day;
int month;
int year;
public:
void setDate(int d, int m,int y){
day=d;
month=m;
year=y;
}
void displayDate()const{
cout<<"date(DMY format):"<<day<<"-"<<month<<"-"<<year<<endl;
}
};
int main(){
DateString dateString;
dateString.setDate("2024-09-08");
DMY dateDMY;
dateDMY.setDate(8,9,2024);
dateString.displayDate();
dateDMY.displayDate();
return 0;