- Click below to download Unit 2 programs in pdf file: -
Unit 2
/* 1. Using friend function find the maximum number from given two numbers from two different classes. Write all necessary functions and constructors for the program. */
#include <iostream>
using namespace std;
void findmax();
class A
{
private:
int x;
public:
A()
{
cout << "Enter Number 1 :" << endl;
cin>> x;
}
friend void findmax();
};
class B
{
private:
int y;
public:
B()
{
cout << "Enter Number 2 :" << endl;
cin >> y;
}
friend void findmax();
};
void findmax()
{
A n1;
B n2;
if(n1.x < n2.y)
{
cout << "Number 2 is Maximum :" << endl;
}
else
{
cout << "Number 1 is Maximum :" << endl;
}
}
int main()
{
findmax();
return 0;
}
/* 2. Using a friend function, find the average of three numbers from three different classes. Write all necessary member functions and constructor for the classes. */
#include<iostream>
using namespace std;
void findavg();
class A
{
private:
int x;
public:
A()
{
cout<< "Enter Number 1:" <<endl;
cin>> x;
}
friend void findavg();
};
class B
{
private:
int y;
public:
B()
{
cout<< "Enter Number 2:" <<endl;
cin>> y;
}
friend void findavg();
};
class C
{
private:
int z;
public:
C()
{
cout << " Enter Number 3 :"<< endl;
cin >> z;
}
friend void findavg();
};
void findavg()
{
float j;
A n1;
B n2;
C n3;
j=float(n1.x + n2.y + n3.z)/3;
cout << "Average is : "<<j<<endl;
}
int main()
{
findavg();
}
/*3. Define currency class which contains rupees and paisa as data members. Write a friend function named AddCurrency( )which add 2 different Currency objects and returns a Currency object. Write parameterized constructor to initialize the values and use appropriate functions to get the details from the user and display it. */
#include<iostream>
using namespace std;
class currency;
currency AddCurrency(currency x1,currency x2);
class currency
{
int rupees,paisa;
public:
currency(){}
currency(int x,int y)
{
rupees= x;
paisa = y;
}
void display()
{
cout << "Rupees : " << rupees << endl;
cout << "paisa : "<<paisa << endl;
}
friend currency AddCurrency(currency x1,currency x2);
};
currency AddCurrency(currency x1,currency x2)
{
currency m;
m.rupees = x1.rupees + x2.rupees;
m.paisa = x1.paisa + x2.paisa;
m.rupees += m.paisa /100;
m.paisa %=100;
return m;
}
int main()
{
int r,p;
cout << "Enter the rupees :"<<endl;
cin >> r ;
cout << "Enter the paisa :"<<endl;
cin >> p ;
currency x1(r,p);
cout << "Enter the rupees :"<<endl;
cin >> r ;
cout << "Enter the paisa :"<<endl;
cin >> p ;
currency x2(r,p);
currency x3 = AddCurrency(x1,x2);
x3.display();
return 0;
}
/* 4. Create Calendar class with day, month and year as data members. Include default and parameterized constructors to initialize a Calendar object with a valid date value. Define a function Add Days to add days to the Calendar object. Define a display function to show data in “dd/mm/yyyy” format. */
#include<iostream>
using namespace std;
class calendar
{
int date,month,year;
public:
calendar()
{
date=month=year=0;
}
calendar(int a,int b,int c)
{
date=a;
month=b;
year=c;
}
void add()
{
int a;
cout<<"How many days to want to add : ";
cin>>a;
date=date+a;
do
{
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12) && (date>31))
{
date=date-31;
month++;
}
if((month==4||month==6||month==9||month==10||month==11) && (date>30))
{
date=date-30;
month++;
}
if((month==2) && (date>29))
{
date=date-29;
month++;
}
if(month>12)
{
month=month-12;
year++;
}
}while(date>31 || month>12);
}
void display()
{
cout<<date<<'/'<<month<<'/'<<year;
}
};
int main()
{
int a,b,c;
cout<<"Enter the date :";
cin>>a;
cout<<"Enter the month :";
cin>>b;
cout<<"Enter the year :";
cin>>c;
calendar obj1(a,b,c);
obj1.add();
obj1.display();
}
/* 5. Create a class named ‘String’ with one data member of type char *, which stores a string. Include default, parameterized and copy constructor to initialize the data member. Write a program to test this class. */
#include<iostream>
using namespace std;
class String
{
private:
char *str;
public:
String()
{
str = "String 1";
}
String(char *s2)
{
str = s2;
cout<<str<<endl;
}
void display()
{
cout<<str<<endl;
}
};
int main()
{
String s1;
s1.display();
String s2("BCA4GU");
String s3 = s1;
s3.display();
}
/* 6. Write a base class named Employee and derive classes Male employee and Female Employee from it. Every employee has an id, name and a scale of salary. Make a function ComputePay(in hours) to compute the weekly payment of every employee. A male employee is paid on the number of days and hours he works. The female employee gets paid the wages for 40 hours a week, no matter what the actual hours are. Test this program to calculate the pay of employee. */
#include<iostream>
using namespace std;
class employee
{
protected:
int id;
string name;
int salary;
};
class male:public employee
{
private:
int days;
int hours;
public:
void set_data()
{
cout<<"------Enter the data for Male employee------"<<endl;
cout<<"Enter id : ";
cin>>id;
cout<<"Enter name : ";
cin>>name;
cout<<"Enter total days : ";
cin>>days;
cout<<"Enter total hours he works : ";
cin>>hours;
}
void compute_salary()
{
salary = (days*hours)*100;
}
void display()
{
cout<<"Employee id : "<<id<<endl;
cout<<"Employee name : "<<name<<endl;
cout<<"Total salary of employee : "<<salary<<endl;
}
};
class female:public employee
{
private:
int week;
public:
void set_data()
{
cout<<"------Enter the data for Female employee------"<<endl;
cout<<"Enter id : ";
cin>>id;
cout<<"Enter name : ";
cin>>name;
cout<<"Enter total week : ";
cin>>week;
}
void compute_salary()
{
salary = (week*40)*100;
}
void display()
{
cout<<"Employee id : "<<id<<endl;
cout<<"Employee name : "<<name<<endl;
cout<<"Total salary of employee : "<<salary<<endl;
}
};
int main() {
male m;
female f;
m.set_data();
m.compute_salary();
m.display();
f.set_data();
f.compute_salary();
f.display();
}
/* 7. Create a class called scheme with scheme_id, scheme_name,outgoing_rate, and message_charge. Derive customer class form scheme and include cust_id, name and mobile_no data.Define necessary functions to read and display data. Create a menu driven program to read call and message information for a customer and display the detail bill. */
#include<iostream>
using namespace std;
class scheme
{
protected:
int id,out,msg;
char name[20];
};
class customer:public scheme
{
int cu_id;
char cu_name[10],no[10];
public:
void cu_info()
{
cout<<"CUSTOMER ID :";
cin>>cu_id;
cout<<"CUSTOMER NAME :";
cin>>cu_name;
cout<<"PHONE NUMBER :";
cin>>no;
cout<<"OUTGOING CALL :";
cin>>out;
cout<<"OUTGOING MSG :";
cin>>msg;
}
void set_scheme(int n)
{
id=n;
if(id==1)
{
out=out*2;
msg=msg*0.6;
}
else if(id==2)
{
out=out*2.2;
msg=msg*0.7;
}
else if(id==3)
{
out=out*2.4;
msg=msg*0.7;
}
cout<<"SCHEME NAME :";
cin>>name;
}
void disp_bill()
{
cout<<"\nY O U R B I L L";
cout<<"\nCUSTOMER ID :"<<cu_id;
cout<<"\nCUSTOMER NAME :"<<cu_name;
cout<<"\nPHONE NUMBER :"<<no;
cout<<"\nSCHEME ID :"<<id;
cout<<"\nSCHEME NAME :"<<name;
cout<<"\nOUTGOING RATE :"<<out<<"Rs.";
cout<<"\nMESSAGE CHARGE:"<<msg<<"Rs.";
}
};
int main()
{
int ch;
customer obj1;
obj1.cu_info();
first:
cout<<"S.ID SIM SCHEME ";
cout<<"\n1 JIO ->CALL:2Rs. MSG:0.6Rs.";
cout<<"\n2 AIRTEL->CALL:2.2Rs. MSG:0.7Rs.";
cout<<"\n3 IDEA ->CALL:2.4Rs. MSG:0.7Rs.\nEnter the Scheme ID:";
cin>>ch;
if(ch<4)
{
obj1.set_scheme(ch);
obj1.disp_bill();
}
else
{
cout<<"Enter Valid Scheme ID.\n";
goto first;
}
}
/* 8. Write a program with use of inheritance: Define a class publisher that stores the name of the title. Derive two classes book and tape, which inherit publisher. Book class contains member data called page no and tape class contain time for playing. Define functions in the appropriate classes to get and print the details. */
#include<iostream>
using namespace std;
class publisher
{
protected:
char title[30];
};
class book:public publisher
{
private:
int page_no;
public:
void set_data()
{
cout<<"Enter book title : ";
cin>>title;
cout<<"Enter page_no : ";
cin>>page_no;
}
void display()
{
cout<<"Book title : "<<title<<endl;
cout<<"Page no : "<<page_no<<endl;
}
};
class tape:public publisher
{
private:
char tape_play_time[10];
public:
void set_data()
{
cout<<"Enter book title : ";
cin>>title;
cout<<"Enter tape_play_time : ";
cin>>tape_play_time;
}
void display()
{
cout<<"Book title : "<<title<<endl;
cout<<"Tape_play_time : "<<tape_play_time<<endl;
}
};
int main() {
book b;
tape t;
b.set_data();
b.display();
t.set_data();
t.display();
}
/* 9. Create a class account that stores customer name, account no,types of account. From this derive classes cur_acc and sav_acc to include necessary member function to do the following:
• Accepts deposit from customer and update balance
• Compute and Deposit interest
• Permit withdrawal and Update balance.
*/
#include<iostream>
using namespace std;
class account
{
public:
string cus_name;
int acc_no;
string acc_type;
};
class cur_acc:public account
{
private:
int dep;
int bal = 100000;
int interest;
float ans;
int with;
public:
void set_data()
{
cout<<"Enter customer name : ";
cin>>cus_name;
cout<<"Enter account number : ";
cin>>acc_no;
}
void deposit_bal()
{
cout<<"Enter deposit : ";
cin>>dep;
bal = bal + dep;
cout<<"Now balance is : "<<bal<<endl;
}
void com_int()
{
cout<<"Enter deposit interest : ";
cin>>interest;
ans = (float)(bal*interest)/100;
cout<<"Deposite interest is : "<<ans<<endl;
}
void withdrawal()
{
cout<<"Enter withdrawal amount : ";
cin>>with;
if(with>bal)
{
cout<<"Withdraw not possible"<<endl;
}
else
{
cout<<"Withdraw of "<<with<<" is successful"<<endl;
bal = bal-with;
cout<<"Now balance is : "<<bal<<endl;
}
}
};
class sav_acc:public account
{
private:
int dep;
int bal = 60000;
int interest;
float ans;
int with;
public:
void set_data()
{
cout<<"Enter customer name : ";
cin>>cus_name;
cout<<"Enter account number : ";
cin>>acc_no;
}
void deposit_bal()
{
cout<<"Enter deposite : ";
cin>>dep;
bal = bal + dep;
cout<<"Now balance is : "<<bal<<endl;
}
void com_int()
{
cout<<"Enter deposite interest : ";
cin>>interest;
ans = (float)(bal*interest)/100;
cout<<"Deposite interest is : "<<ans<<endl;
}
void withdrawal()
{
cout<<"Enter withdrawal amount : ";
cin>>with;
if(with>bal)
{
cout<<"Withdraw not possible"<<endl;
}
else
{
cout<<"Withdraw of "<<with<<" is successful"<<endl;
bal = bal-with;
cout<<"Now balance is : "<<bal<<endl;
}
}
};
int main()
{
account a;
cur_acc c;
sav_acc s;
cout<<"Enter account type : ";
cin>>a.acc_type;
if(a.acc_type=="Saving")
{
s.set_data();
s.deposit_bal();
s.com_int();
s.withdrawal();
}
else
{
c.set_data();
c.deposit_bal();
c.com_int();
c.withdrawal();
}
}
/* 10. Write a base class named Employee and derive classes Male employee and Female Employee from it. Every employee has an id, name and a scale of salary. Make a functionComputePay (in hours) to compute the weekly payment of every employee. A male employee is paid on the number of days and hours he works. The female employee gets paid the wages for 40 hours a week, no matter what the actual hours are. Test this program to calculate the pay of employee */
#include<iostream>
using namespace std;
class employee
{
protected:
int id;
string name;
int salary;
};
class male:public employee
{
private:
int days;
int hours;
public:
void set_data()
{
cout<<"------Enter the data for Male employee------"<<endl;
cout<<"Enter id : ";
cin>>id;
cout<<"Enter name : ";
cin>>name;
cout<<"Enter total days : ";
cin>>days;
cout<<"Enter total hours he works : ";
cin>>hours;
}
void compute_salary()
{
salary = (days*hours)*100;
}
void display()
{
cout<<"Employee id : "<<id<<endl;
cout<<"Employee name : "<<name<<endl;
cout<<"Total salary of employee : "<<salary<<endl;
}
};
class female:public employee
{
private:
int week;
public:
void set_data()
{
cout<<"------Enter the data for Female employee------"<<endl;
cout<<"Enter id : ";
cin>>id;
cout<<"Enter name : ";
cin>>name;
cout<<"Enter total week : ";
cin>>week;
}
void compute_salary()
{
salary = (week*40)*100;
}
void display()
{
cout<<"Employee id : "<<id<<endl;
cout<<"Employee name : "<<name<<endl;
cout<<"Total salary of employee : "<<salary<<endl;
}
};
int main()
{
male m;
female f;
m.set_data();
m.compute_salary();
m.display();
f.set_data();
f.compute_salary();
f.display();
}
0 Comments