1. Create a class named time with data members hours, minutes and second. Use member function settime() to set time, gettime() to read time from user, addtime() to add two time and showtome() to display time in HH:MM:SS format. Implement above specification in C++.
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
class time
{
private:
int hours;
int minutes;
int seconds;
public:
void gettime();
void settime(int hrs,int mins,int secs)
{
hours=hrs;
minutes=mins;
seconds=secs;
}
void addtime(time t1,time t2);
void showtime();
};
void time::gettime()
{
cout<<"Hours=";cin>>hours;
cout<<"Minutes=";cin>>minutes;
cout<<"Seconds=";cin>>seconds;
}
void time::showtime()
{
if(hours<=9)
cout<<setw(2)<<setfill('0')<<hours<<":";
else
cout<<hours<<":";
if(minutes<10)
cout<<setw(2)<<setfill('0')<<minutes<<":";
else
cout<<minutes<<":";
if(seconds<10)
cout<<setw(2)<<setfill('0')<<seconds<<endl;
else
cout<<seconds<<endl;
}
void time::addtime(time t1,time t2)
{
seconds=t1.seconds+t2.seconds;
minutes=seconds/60;
seconds%=60;
minutes+=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours+=t1.hours+t2.hours;
}
int main()
{
time t1,t2,t3;
t1.settime(10,50,53);
cout<<"Enter details of t2:"<<endl;
t2.gettime();
t3.addtime(t1,t2);
cout<<"Time t3 is\n";
t3.showtime();
getch();
return 0;
}
2. Define a class to represent a bank account. Include the following members:
Data members
a. Name of the depositor
b. Account number
c. Type of account
d. Balance amount in the account.
Member functions:
a. To assign initial values
b. To deposit an amount
c. To withdraw an amount after checking balance
d. To display name and balance.
Write a main program to test the program.
#include<conio.h>
#include<string.h>
using namespace std;
class bank_account
{
private:
char depositor_name[20];
int acc_no;
char acc_type[20];
double amount;
public:
void setdata(char *dname,int a_no,char *atype,int amt)
{
strcpy(depositor_name,dname);
acc_no = a_no;
strcpy(acc_type,atype);
amount = amt;
}
void display();
void deposit(bank_account &,double);
void withdraw(bank_account &,double);
};
void bank_account::display()
{
cout<<"Name of depositor: "<<depositor_name<<endl;
cout<<"Account No.= "<<acc_no<<endl;
cout<<"Account type= "<<acc_type<<endl;
cout<<"Balance Amount= "<<amount<<endl<<endl;
}
void bank_account::deposit(bank_account &a,double b)
{
// amount=amount-b;
a.amount=a.amount+b;
}
void bank_account::withdraw(bank_account &a,double c)
{
if(c>a.amount)
{
cout<<"Insufficient balance!!!"<<endl;
}
else
{
//amount=amount+c;
a.amount=a.amount-c;
}
}
//main program
int main()
{
bank_account ac1,ac2;
cout<<"Details of Bank account\n_______________________"<<endl<<endl;
ac1.setdata("AMIR SHARMA",5432,"SAVING",10000);
ac1.display();
double b;
cout<<"Enter amount to deposit in the account: ";
cin>>b;
ac2.deposit(ac1,b);
cout<<"\n===================\nAfter money deposit\n==================="<<endl<<endl;
cout<<"Details of Bank account\n_______________________"<<endl<<endl;
ac1.display();
double c;
cout<<"Enter amount to withdraw from the account: ";
cin>>c;
ac2.withdraw(ac1,c);
cout<<"\n===================\nAfter money withdraw\n==================="<<endl<<endl;
cout<<"Details of Bank account\n_______________________"<<endl<<endl;
ac1.display();
getch();
return 0;
}
3. Define a class distance with
Data members
a. Feet in int
b. Inches in float
Member functions
a. To read data member
b. To display distance in format feet'-inches"
c. To add two distance object and returning distance object.
Write a main program to test the program.
#include<conio.h>
using namespace std;
class dist
{
private:
int feet;
float inches;
public:
void getdata()
{
cout<<"Feet= ";cin>>feet;
cout<<"Inches= ";cin>>inches;
}
void display()
{
cout<<feet<<"\'-"<<inches<<"\""<<endl;
}
dist add(dist);
};
dist dist::add(dist d2)
{
dist temp;
temp.feet=0;
temp.inches=inches+d2.inches;
while(temp.inches>=12)
{
temp.inches-=12;
temp.feet++;
}
temp.feet+=feet+d2.feet;
return(temp);
}
int main()
{
dist d1,d2;
cout<<"Enter details of d1:"<<endl;
d1.getdata();
cout<<"Enter details od d2:"<<endl;
d2.getdata();
dist d3;
d3=d1.add(d2);
cout<<"\nResult after adding d1 and d2 is: "<<endl;
d3.display();
getch();
return 0;
}
4. Define a class distance with
Data members
c. Feet in int
d. Inches in float
Member functions
d. To read data member
Friend functions
a. To display distance in format feet'-inches"
b. To add two distance object and returning distance object.
Write a main program to test the program.
#include<iostream>
#include<conio.h>
using namespace std;
class dist
{
private:
int feet;
float inches;
public:
void getdata()
{
cout<<"Feet= ";cin>>feet;
cout<<"Inches= ";cin>>inches;
}
friend void display(dist);
friend dist add(dist d1,dist d2);
};
void display(dist d)
{
cout<<d.feet<<"\'-"<<d.inches<<"\""<<endl;
}
dist add(dist d1,dist d2)
{
dist temp;
temp.feet=0;
temp.inches=d1.inches+d2.inches;
while(temp.inches>=12)
{
temp.inches-=12;
temp.feet++;
}
temp.feet+=d1.feet+d2.feet;
return(temp);
}
int main()
{
dist d1,d2;
cout<<"Enter details of d1:"<<endl;
d1.getdata();
cout<<"Enter details od d2:"<<endl;
d2.getdata();
dist d3;
d3=add(d1,d2);
cout<<"\nResult after adding d1 and d2 is: ";
display(d3);
getch();
return 0;
}
5. Define a class Amount with
Data members
a. Rupees
b. Paisa
Member functions
e. To read data member
f. To display amount in appropriate format.
Friend functions
a. To add two Amounts.
#include<iostream>
#include<conio.h>
using namespace std;
class amount
{
int rupees;
int paisa;
public:
void getdata()
{
cout<<"Rupee= ";cin>>rupees;
cout<<"Paisa= ";cin>>paisa;
}
void showdata(amount a)
{
cout<<"Rs."<<a.rupees<<" and "<<a.paisa<<" paisa"<<endl;
}
friend amount add(amount a1,amount a2);
};
amount add(amount a1,amount a2)
{
amount temp;
temp.paisa=a1.paisa+a2.paisa;
temp.rupees=0;
while(temp.paisa>=100)
{
temp.paisa-=100;
temp.rupees++;
}
temp.rupees+=a1.rupees+a2.rupees;
return(temp);
}
int main()
{
amount a1,a2,a3;
cout<<"Enter details of amount a1:"<<endl;
a1.getdata();
cout<<"Enter details of amount a2:"<<endl;
a2.getdata();
a3=add(a1,a2);
cout<<"sum of a1 and a2 is:"<<endl;
a3.showdata(a3);
getch();
return 0;
}
6. Define a class Matrix with
Data members
a. Two dimensional array for a 3×3 matrix
Member functions
a. To read matrix
b. To display matrix in matrix format.
c. To transpose a matrix and returning a matrix object.
Write a main program to test the program.
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
class matrix
{
private:
int a[3][3];
public:
void read()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>a[i][j];
}
}
}
void display()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<setw(3)<<a[i][j];
}
cout<<endl;
}
}
matrix transpose(matrix);
};
matrix matrix::transpose(matrix )
{
matrix TM;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
TM.a[i][j]=a[j][i];
}
}
return(TM);
}
int main()
{
matrix m1;
cout<<"Enter elements of 3*3 matrix:"<<endl;
m1.read();
cout<<"\nMatrix you entered:"<<endl<<endl;
m1.display();
matrix m2;
m2=m1.transpose(m1);
cout<<"\nTranspose of the above matrix:"<<endl<<endl;
m2.display();
getch();
return 0;
}
7. Define a class Matrix with
Data members
a. Two dimensional array for a 3×3 matrix
Member functions
a. To read matrix
b. To display matrix in matrix format.
c. To multiply two matrices and returning a resultant matrix object.
Write a main program to test the program.
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
class matrix
{
private:
int a[3][3];
public:
void read()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>a[i][j];
}
}
}
void display()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<setw(3)<<a[i][j];
}
cout<<endl;
}
}
matrix multiply(matrix);
};
matrix matrix::multiply(matrix m2)
{
matrix result;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
result.a[i][j]=0;
for(int k=0;k<3;k++)
{
result.a[i][j]+=a[i][k]*m2.a[k][j];
}
}
}
return(result);
}
int main()
{
matrix m1,m2;
cout<<"Enter elements of 1st 3*3 matrix:"<<endl;
m1.read();
cout<<"Enter elements of 2nd 3*3 matrix:"<<endl;
m2.read();
cout<<"\n1st Matrix you entered:"<<endl<<endl;
m1.display();
cout<<"\n2nd Matrix you entered:"<<endl<<endl;
m2.display();
matrix m3;
m3=m1.multiply(m2);
cout<<"\nMultiplication of m1 and m2 is:"<<endl<<endl;
m3.display();
getch();
return 0;
}
8. Create two classes DM and DB which stores the value of distances. DM stores values in meter and centimeters and DB in feet and inches. Write a program that can read values for the classes objects and add one object of DM with another object of DB. Use friend function to carry out operation and the object that stores the result be a DB object.
#include<iostream>
#include<conio.h>
using namespace std;
class DB;
class DM
{
private:
float meters;
float centimeters;
public:
void read()
{
cout<<"meters=";cin>>meters;
cout<<"centimeters=";cin>>centimeters;
}
void convert()
{
float DMfeet=3.281*meters;
float DMinches=0.3937*centimeters;
}
friend int add(DB,DM);
};
class DB
{
private:
float DBfeet;
float DBinches;
public:
void read()
{
cout<<"DBfeet=";cin>>DBfeet;
cout<<"DBinches=";cin>>DBinches;
}
friend int add(DB a,DM b);
};
int add(DB a,DM b)
{
float feet=a.DBfeet+b.DMfeet;
float inches=a.DBinches+b.DMinches;
return (feet);
}
int main()
{
DB a;
DM b;
a.read();
b.read();
b.convert();
cout<<"DMfeet="<<b.DMfeet;
cout<<"DMinches="<<b.DMinches;
cout<<"Sum="<<add(a,b);
getch();
return 0;
}
No comments:
Post a Comment