CPP PRC
CPP PRC
1. Write a program to calculate the area of circle, rectangle and square using
function overloading
#include<iostream.h>
#include<conio.h>
#define pi 3.14
void main()
float r,l,b,ar_c,ar_rect;
int length,ar_s;
clrscr();
cin>>r;
ar_c=area(r);
cin>>l>>b;
ar_rect=area(l,b);
1
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"Enter lenth of square:";
cin>>length;
ar_s=area(length);
getch();
float area(float r)
float ans;
ans = pi * r * r;
return ans;
float ans;
ans = l * b;
return ans;
int area(int l)
int ans;
ans= l * l;
2
Instagram mr_faisal_607 Telegram : GUBCA
return ans;
3
Instagram mr_faisal_607 Telegram : GUBCA
2. Write a program to demonstrate the use of default arguments in function
overloading.
#include<iostream.h>
#include<conio.h>
#define pi 3.14
void main()
float r=4,l,b;
int length = 5;
clrscr();
cin>>l>>b;
getch();
float area(float r)
4
Instagram mr_faisal_607 Telegram : GUBCA
{
float ans;
ans = pi * r * r;
return ans;
float ans;
ans = l * b;
return ans;
int area(int l)
int ans;
ans= l * l;
return ans;
5
Instagram mr_faisal_607 Telegram : GUBCA
3. Write a program to demonstrate the use of returning a reference variable.
#include<iostream.h>
#include<conio.h>
void main()
int b=5;
clrscr();
rb=rb+10;
getch();
6
Instagram mr_faisal_607 Telegram : GUBCA
4. Create a class student which stores the detail about roll no,name, marks of
5 subjects, i.e.
science, Mathematics, English,C, C++. The class must have the following:
• Get function to accept value of the data members.
• Display function to display values of data members.
• Total function to add marks of all 5 subjects and Storeit in the data
members named total.
#include<iostream.h>
#include<conio.h>
class student
int roll_no;
char name[20];
float sci,maths,eng,c,cpp;
float total;
public:
void get_data()
cin>>roll_no;
cin>>name;
cin>>sci;
7
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"Enter Marks in maths :";
cin>>maths;
cin>>eng;
cin>>c;
cin>>cpp;
void total_marks()
total=sci+maths+eng+c+cpp;
void display_data()
cout<<"Roll No.:"<<roll_no<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Science:"<<sci<<endl;
cout<<"Maths:"<<maths<<endl;
cout<<"English:"<<eng<<endl;
8
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"c:"<<c;
cout<<"cpp:"<<cpp;
total_marks();
};
void main()
student std1;
student std2;
clrscr();
std1.get_data();
std1.display_data();
std2.get_data();
std2.display_data();
getch();
9
Instagram mr_faisal_607 Telegram : GUBCA
5. Create a function power() to raise a number m to power n, the function
takes a double value for m and int value for n,and returns the result
correctly. Use the default value of 2 for n to make the function calculate
squares when this argument is omitted. Write a main that gets the values
of mand n from the user to test the function.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
double ans,m,n;
char choice;
clrscr();
cin>>m;
cin>>choice;
if(choice=='Y'||choice=='y')
ans=power(m);
10
Instagram mr_faisal_607 Telegram : GUBCA
}
else
cin>>n;
ans=power(m,n);
getch();
double ans;
ans= pow(m,n);
return ans;
11
Instagram mr_faisal_607 Telegram : GUBCA
6. Write a basic program which shows the use of scope resolution operator.
#include<iostream.h>
#include<conio.h>
class calculate
int num1,num2;
public:
void get_data();
void calculate_numbers();
};
void calculate::get_data()
cout<<"Enter num1:";
cin>>num1;
cout<<"Enter num2:";
cin>>num2;
void calculate::calculate_numbers()
int sum,sub,mul,div;
sum=num1+num2;
sub=num1-num2;
12
Instagram mr_faisal_607 Telegram : GUBCA
mul=num1*num2;
div=num1/num2;
cout<<"sum: "<<sum<<endl;
cout<<"sub: "<<sub<<endl;
cout<<"mul: "<<mul<<endl;
cout<<"div: "<<div<<endl;
void main()
calculate c;
clrscr();
c.get_data();
c.calculate_numbers();
getch();
13
Instagram mr_faisal_607 Telegram : GUBCA
7. Write a C++ program to swap the value of private data members from 2
different classes.
#include<iostream.h>
#include<conio.h>
class class2;
class class1
int a;
public:
void get_data()
cin>>a;
void display()
};
class class2
14
Instagram mr_faisal_607 Telegram : GUBCA
int b;
public:
void get_data()
cin>>b;
void display()
};
int temp=0;
temp=t1.a;
t1.a=t2.b;
t2.b=temp;
void main()
15
Instagram mr_faisal_607 Telegram : GUBCA
class1 c1;
class2 c2;
clrscr();
c1.get_data();
c2.get_data();
swap(c1,c2);
c1.display();
c2.display();
getch();
16
Instagram mr_faisal_607 Telegram : GUBCA
8. Write a program to illustrate the use of this pointer.
#include<iostream.h>
#include<conio.h>
class test
private:
int x;
int y;
public:
test &setX(int x)
this->x=x;
return *this;
test &setY(int y)
this->y=y;
return *this;
void print()
cout<<"x= "<<x<<endl;
17
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"y= "<<y;
};
void main()
test t1;
clrscr();
t1.setX(10).setY(20).print();
getch();
18
Instagram mr_faisal_607 Telegram : GUBCA
9. An election is contested by five candidates. The candidates are numbered 1
to 5 and the voting is done by marking the candidate number on the ballot
paper. Write a program to read the ballots and count the votes cast for
each candidate using an array variable count. In case a number is read
outside the range of 1 to 5, the ballot should be considered as a ‘spoilt
ballot’ and the program should also count the number of spoilt ballots.
#include<iostream.h>
#include<conio.h>
class election
{
int count[5],spoiltballots,vote;
char choice;
public:
void initialse();
void readvote();
void display();
};
void election::initialse()
{
spoiltballots=0;
for(int i=0;i<5;i++)
{
count[i]=0;
}
}
void election::readvote()
{
do
{
cout<<"Enter candidate number (1 to 5)";
cin>>vote;
if(vote>1 && vote<=5)
{
19
Instagram mr_faisal_607 Telegram : GUBCA
count[vote-1]++;
}
else
{
spoiltballots++;
}
cout<<"Do you want to vote again (Y/N)";
cin>>choice;
}while(choice!='N');
}
void election::display()
{
for(int i=0;i<5;i++)
{
cout<<"\nTotal votes for candidate "<<i+1<<" is "<<count[i];
}
cout<<"\nTotal spoiltballots :"<<spoiltballots;
}
void main()
{
election e;
clrscr();
e.initialse();
e.readvote();
e.display();
getch();
}
20
Instagram mr_faisal_607 Telegram : GUBCA
10. Write a program to call member functions of class in the main function
using pointer to object and pointer to member function.
#include<iostream.h>
#include<conio.h>
class demo
int num1,num2;
public:
void input();
void largest();
};
void demo::input()
cin>>num1;
cin>>num2;
void demo::largest()
if(num1>num2)
21
Instagram mr_faisal_607 Telegram : GUBCA
cout<<num1<<"is largest";
else
cout<<num2<<"is largest";
void main()
demo d;
demo *p=&d;
clrscr();
p->input();
p->largest();
getch();
22
Instagram mr_faisal_607 Telegram : GUBCA
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.h>
#include<conio.h>
class class2;
class class1
int num1;
public:
class1()
num1=56;
~class1()
};
class class2
23
Instagram mr_faisal_607 Telegram : GUBCA
{
int num2;
public:
class2()
num2=45;
~class2()
};
if(c1.num1>c2.num2)
cout<<c1.num1<<"is greater";
else if(c1.num1<c2.num2)
cout<<c2.num2<<"is greater";
24
Instagram mr_faisal_607 Telegram : GUBCA
}
else
void main()
class1 c1;
class2 c2;
clrscr();
find_max(c1,c2);
getch();
25
Instagram mr_faisal_607 Telegram : GUBCA
2. Using a friend function, find the average of three numbersfrom three
different classes. Write all necessary memberfunctions and constructor for
the classes.
#include<iostream.h>
#include<conio.h>
class class3;
class class2;
class class1
int num1;
public:
class1()
num1=39;
~class1()
};
class class2
26
Instagram mr_faisal_607 Telegram : GUBCA
{
int num2;
public:
class2()
num2=66;
~class2()
};
class class3
int num3;
public:
class3()
num3=78;
27
Instagram mr_faisal_607 Telegram : GUBCA
~class3()
};
float avg;
avg=(c1.num1+c2.num2+c3.num3)/3.0;
void main()
class1 c1;
class2 c2;
class3 c3;
clrscr();
find_avg(c1,c2,c3);
getch();
28
Instagram mr_faisal_607 Telegram : GUBCA
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 Currencyobject. Write parameterized
constructor to initialize the values and use appropriate functions to get the
details from the user and display it.
#include<iostream.h>
#include<conio.h>
class currency
int rupee;
int paisa;
public:
currency()
rupee=paisa=0;
currency(int x,int y)
rupee=x;
paisa=y;
29
Instagram mr_faisal_607 Telegram : GUBCA
void get_data();
void display_data();
~currency()
cout<<"\nDestructor called";
};
void currency::get_data()
cout<<"Enter rupees:";
cin>>rupee;
cout<<"Enter paisa:";
cin>>paisa;
currency c;
c.rupee=c1.rupee + c2.rupee;
c.paisa=c1.paisa + c2.paisa;
30
Instagram mr_faisal_607 Telegram : GUBCA
if(c.paisa>100)
c.rupee=c.rupee+(c.paisa/100);
c.paisa=c.paisa%100;
return c;
cout<<"\nTotal rupes:"<<rupee<<endl;
cout<<"\nTotal paisa:"<<paisa;
void main()
currency cur;
currency c1(0,0);
currency c2(0,0);
clrscr();
c1.get_data();
c2.get_data();
31
Instagram mr_faisal_607 Telegram : GUBCA
cur=addcurrency(c1,c2);
cur.display_data();
getch();
32
Instagram mr_faisal_607 Telegram : GUBCA
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 AddDays to add days to the Calendar
object. Define a display function to show data in “dd/mm/yyyy” format.
#include<iostream.h>
#include<conio.h>
class calender
int day,month,year;
public:
calender()
day=month=year=0;
day=d;
month=m;
year=y;
~calender()
cout<<"\nDestructor called.";
33
Instagram mr_faisal_607 Telegram : GUBCA
}
void display();
void add_days(int);
};
day=day+d;
if(day>31)
month++;
day=day-31;
if(month>12)
month=1;
year++;
34
Instagram mr_faisal_607 Telegram : GUBCA
{
if(day>30)
month++;
day=day-30;
else
if(day>=28)
if(year%4==0)
day=day-29;
else
day=day-28;
month++;
35
Instagram mr_faisal_607 Telegram : GUBCA
}
void calender::display()
cout<<day<<"/"<<month<<"/"<<year<<endl;
void main()
calender c1(10,12,2023);
clrscr();
c1.display();
c1.add_days(23);
c1.display();
getch();
36
Instagram mr_faisal_607 Telegram : GUBCA
5. Create a class named ‘String’ with one data member of typechar *, which
stores a string. Include default, parameterized and copy constructor to
initialize the data member. Write aprogram to test this class.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string1
char *str;
public:
void print_string();
string1()
str="Default string";
string1(char *s)
int len=strlen(s);
str=new char[len+1];
strcpy(str,s);
~string1()
37
Instagram mr_faisal_607 Telegram : GUBCA
{
delete str;
};
void string1::print_string()
cout<<str<<endl;
void main()
string1 s1;
clrscr();
s1.print_string();
string1 s2(txt);
s2.print_string();
string1 s3=s2;
s3.print_string();
getch();
38
Instagram mr_faisal_607 Telegram : GUBCA
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.h>
#include<conio.h>
class employee
protected:
int id;
char name[20];
int scale_of_salary;
public:
void get_empl_details();
void computepay(int);
};
int days;
int hours;
public:
39
Instagram mr_faisal_607 Telegram : GUBCA
void get_data();
};
int hours;
public:
void get_data();
female()
hours=40;
};
void employee::get_empl_details()
cout<<"\nEnter ID:";
cin>>id;
cout<<"\nEnter name:";
cin>>name;
cin>>scale_of_salary;
void male::get_data()
40
Instagram mr_faisal_607 Telegram : GUBCA
{
get_empl_details();
cin>>days;
cin>>hours;
int totalHours=0;
totalHours=days*hours;
computepay(totalHours);
void female::get_data()
get_empl_details();
computepay(hours);
int totalpay=0;
totalpay=scale_of_salary * hrs;
cout<<"\nTotal pay:"<<totalpay<<endl;
41
Instagram mr_faisal_607 Telegram : GUBCA
}
void main()
male m;
female f;
clrscr();
m.get_data();
f.get_data();
getch();
42
Instagram mr_faisal_607 Telegram : GUBCA
7. Create a class called scheme with scheme_id,
scheme_name,outgoing_rate, and message_charge. Derive customer
classform scheme and include cust_id, name and mobile_no data.Define
necessary functions to read and display data. Create amenu driven
program to read call and message informationfor a customer and display
the detail bill.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class scheme
protected:
int scheme_id;
char scheme_name[30];
int call_rate;
int message_rate;
public:
void get_scheme_data();
scheme()
scheme_id=0;
scheme_name[30]='\0';
call_rate=0;
43
Instagram mr_faisal_607 Telegram : GUBCA
message_rate=0;
};
private:
int cust_id;
char cust_name[30];
long mob_num;
int bill;
public:
void get_cust_data();
void read_info();
void show_bill();
customer()
cust_id=0;
cust_name[30]='\0';
mob_num=0;
bill=0;
44
Instagram mr_faisal_607 Telegram : GUBCA
};
void scheme::get_scheme_data()
cin>>scheme_id;
cin>>scheme_name;
cin>>call_rate;
cin>>message_rate;
void customer::get_cust_data()
cin>>cust_id;
cin>>cust_name;
cin>>mob_num;
45
Instagram mr_faisal_607 Telegram : GUBCA
void customer::read_info()
int total_calls,total_msg;
cin>>total_calls;
cin>>total_msg;
bill=(total_calls*call_rate)+(total_msg*message_rate);
void customer::show_bill()
cout<<"custoemr Name:"<<cust_name;
cout<<"\ncustoemr Number:"<<mob_num;
cout<<"\nBill:"<<bill<<endl;
void main()
int choice;
customer c;
clrscr();
46
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"\n3.Give call & message info:";
cout<<"\n4.Show bill:";
cout<<"\n0.Exit:";
do
cin>>choice;
switch(choice)
case 1:
c.get_scheme_data();
break;
case 2:
c.get_cust_data();
break;
case 3:
c.read_info();
break;
case 4:
c.show_bill();
break;
47
Instagram mr_faisal_607 Telegram : GUBCA
case 0:
exit(0);
default:
cout<<"\nInvalid choice";
break;
}while(choice!=0);
getch();
48
Instagram mr_faisal_607 Telegram : GUBCA
8. Write a program with use of inheritance: Define a class publisher that
stores the name of the title. Derive two classesbook and tape, which
inherit publisher. Book class containsmember data called page no and tape
class contain time forplaying. Define functions in the appropriate classes to
get andprint the details.
#include<iostream.h>
#include<conio.h>
class publisher
protected:
char title[30];
};
private:
int page_no;
public:
void get_book_detail();
void display_book_detail();
};
private:
49
Instagram mr_faisal_607 Telegram : GUBCA
int playing_time;
public:
void get_tape_detail();
void display_tape_detail();
};
void book::get_book_detail()
cin>>title;
cin>>page_no;
void tape::get_tape_detail()
cin>>title;
cin>>playing_time;
void book::display_book_detail()
50
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"\nBook page no:"<<page_no;
void tape::display_tape_detail()
cout<<"\nTape title:"<<title;
void main()
book b;
tape t;
clrscr();
b.get_book_detail();
t.get_tape_detail();
b.display_book_detail();
t.display_tape_detail();
getch();
51
Instagram mr_faisal_607 Telegram : GUBCA
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:
#include<iostream.h>
#include<conio.h>
long get_cuttent_balance();
long get_amount();
float get_roi();
class account
protected:
char cust_name[30];
long account_no;
public:
char account_type;
void get_detail();
};
52
Instagram mr_faisal_607 Telegram : GUBCA
void account::get_detail()
cin>>cust_name;
cin>>account_no;
cin>>account_type;
private:
double balance;
public:
void deposite_money();
void withdraw_money();
void compute_interest();
cur_acc()
balance=0;
53
Instagram mr_faisal_607 Telegram : GUBCA
}
};
private:
double balance;
public:
void deposit_money();
void withdraw_money();
void compute_interest();
sav_acc()
balance=0;
};
void cur_acc::deposite_money()
balance=get_cuttent_balance();
long amount=get_amount();
balance=balance+amount;
compute_interest();
54
Instagram mr_faisal_607 Telegram : GUBCA
void cur_acc::withdraw_money()
balance=get_cuttent_balance();
long amount=get_amount();
if(balance>amount)
balance=balance-amount;
else
cout<<"\nInsufficient Balance";
void cur_acc::compute_interest()
float rate_of_interest=get_roi();
balance=balance+((balance * rate_of_interest)/100);
void sav_acc::deposit_money()
55
Instagram mr_faisal_607 Telegram : GUBCA
balance=get_cuttent_balance();
long amount=get_amount();
balance=balance+amount;
compute_interest();
void sav_acc::withdraw_money()
balance=get_cuttent_balance();
long amount=get_amount();
if(balance>amount)
balance=balance-amount;
else
cout<<"\nInsufficient Balance";
void sav_acc::compute_interest()
float rate_of_interest=get_roi();
56
Instagram mr_faisal_607 Telegram : GUBCA
balance=balance+((balance*rate_of_interest)/100);
long get_cuttent_balance()
long balance;
cin>>balance;
return balance;
long get_amount()
long amount;
cout<<"Enter amount:";
cin>>amount;
return amount;
float get_roi()
float roi;
cin>>roi;
57
Instagram mr_faisal_607 Telegram : GUBCA
return roi;
void main()
account acc;
int choice;
sav_acc sb;
cur_acc cr;
clrscr();
acc.get_detail();
cout<<"\n1.Deposite Amount.";
cout<<"\n2.withdraw amount.";
cin>>choice;
if(choice==1)
if(acc.account_type=='s' || acc.account_type=='S')
sb.deposit_money();
58
Instagram mr_faisal_607 Telegram : GUBCA
}
cr.deposite_money();
else
else if(choice==2)
if(acc.account_type=='s' || acc.account_type=='S')
sb.withdraw_money();
cr.withdraw_money();
else
59
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"Invalid account type.";
getch();
60
Instagram mr_faisal_607 Telegram : GUBCA
UNIT:->3
#include<iostrem.h>
#include<conio.h>
class vehicle
protected:
int vehicle_no;
int chasis_no;
public:
};
private:
int no_of_wheels;
char *company;
public:
61
Instagram mr_faisal_607 Telegram : GUBCA
void print_name();
void get_data();
void display_data();
};
private:
long price;
char *company;
public:
void print_name();
void get_data();
void display_data();
};
void vehicle::print_data()
cout<<"\nvehicle Details";
void scooter::print_data()
cout<<"\n\n\tScooter detais"<<endl;
62
Instagram mr_faisal_607 Telegram : GUBCA
void car::printf_data()
cout<<"\n\n\tCar detais"<<endl;
void scooter::get_data()
cin>>vehicle_no;
cin>>chasis_no;
cout>>"Enter no of wheels:";
cin>>no_of_wheels;
cout<<"Enter company:";
cin>>company;
void scooter::display_data()
cout<<"\nChaisa No:"<<chasis_no;
cout<<"\nNo. of wheels:"<<no_of_wheels;
cout<<"\nCompnay:"<<compnay;
63
Instagram mr_faisal_607 Telegram : GUBCA
void car::get_data()
cin>>vehicle_no;
cin>>chasis_no;
cout>>"Enter price:";
cin>>price;
cout<<"Enter compnay:";
cin>>company;
void car::display_data()
cout<<"\nChaisa No:"<<chasis_no;
cout<<"\nprice:"<<price;
cout<<"\nCompnay:"<<compnay;
void main()
vehicle *v;
scooter sc;
64
Instagram mr_faisal_607 Telegram : GUBCA
car cr;
v=≻
clrser();
v->print_name();
sc.get_data();
sc.display_data();
v=&cr;
v->print_name();
cr.get_data();
cr.display_data();
getch();
2. Create a base class shape. Use this class to store two doubletype values
that could be used to compute the area of figures.Derive two specific
classes called triangle and rectangle from the base shape. Add to the base
class, a member function get_data() to initialize the base class data
members and another member function display_area() to compute
anddisplay the area of figures. Make display_area() as a virtual function and
redefine this function in the derived class to suit their requirements.
#include<iostream.h>
#include<conio.h>
class shape
65
Instagram mr_faisal_607 Telegram : GUBCA
{
protected:
double len;
double width;
public:
void get_data()
cin>>len;
cin>>width;
};
void shape::display_area()
public:
void display_area();
66
Instagram mr_faisal_607 Telegram : GUBCA
};
void triangle::display_area()
public:
void display_area();
};
void rectangle::display_area()
void main()
shape *sp;
triangle tr;
rectangle rct;
clrscr();
tr.get_data();
sp=&tr;
67
Instagram mr_faisal_607 Telegram : GUBCA
sp->display_area();
rct.get_data();
sp=&rct;
sp->display_area();
getch();
#include<iostream.h>
#include<conio.h>
class base
protected:
int num1;
int num2;
68
Instagram mr_faisal_607 Telegram : GUBCA
public:
};
private:
int result;
public:
void calculate_data()
cin>>num1;
cin>>num2;
result=num1+num2;
};
private:
int result;
69
Instagram mr_faisal_607 Telegram : GUBCA
public:
void calculate_data()
cin>>num1;
cin>>num2;
result=num1 * num2;
};
void main()
derive1 d1;
derive2 d2;
base *b;
clrscr();
b=&d1;
b->calculate_data();
getch();
70
Instagram mr_faisal_607 Telegram : GUBCA
4 Create a class time with member data hour and minute. Overload ++ unary
operator for class time for increment and -- unary operator for decrement in time
object value.
#include<iostream.h>
#include<conio.h>
class time
int hour;
int minute;
public:
71
Instagram mr_faisal_607 Telegram : GUBCA
void get_time()
cin>>hour>>minute;
void print_time()
cout<<"\nTime : "<<hour<<":"<<minute<<endl;
};
cout<<"prefix ++";
hour=hour+1;
minute=minute+1;
while(minute>=60)
hour=hour+1;
72
Instagram mr_faisal_607 Telegram : GUBCA
minute=minute-60;
cout<<"postfix ++";
hour=hour+1;
minute=minute+1;
while(minute>=60)
hour=hour+1;
minute=minute-60;
cout<<"prefix --";
hour=hour-1;
if(minute>0)
minute=minute-1;
else
73
Instagram mr_faisal_607 Telegram : GUBCA
hour=hour-1;
minute=59;
cout<<"postfix --";
hour=hour-1;
if(minute>0)
minute=minute-1;
else
hour=hour-1;
minute=59;
void main()
time t1,t2;
clrscr();
74
Instagram mr_faisal_607 Telegram : GUBCA
t1.get_time();
t1++;
t1.print_time();
++t1;
t1.print_time();
t2.get_time();
t2--;
t2.print_time();
--t2;
t2.print_time();
getch(); }
5 Create a class string with character array as a data member and write a
program to add two strings with use of operator overloading concept.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
char *str;
public:
string(char *s)
75
Instagram mr_faisal_607 Telegram : GUBCA
{
int len=strlen(s);
str=new char[len+1];
strcpy(str,s);
~string()
delete str;
strcat(str," ");
strcat(str,st.str);
};
void main()
clrscr();
s1+s2;
76
Instagram mr_faisal_607 Telegram : GUBCA
getch();
#include<iostream.h>
#include<conio.h>
class distance
int feet;
int inch;
public:
77
Instagram mr_faisal_607 Telegram : GUBCA
distance()
feet=inch=0;
distance(int f,int i)
feet=f;
inch=i;
void operator==(distance d)
int dist1=(feet*12)+inch;
int dist2=(d.feet*12)+d.inch;
if(dist1==dist2)
else
78
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"\nboth distance are not same ";
int operator<(distance d)
int dist1=(feet*12)+inch;
int dist2=(d.feet*12)+inch;
if(dist1<dist2)
return 1;
else
return 0;
};
void main()
79
Instagram mr_faisal_607 Telegram : GUBCA
{
distance d1(12,34);
distance d2(13,45);
clrscr();
d1==d2;
if(d1<d2)
else
getch();
80
Instagram mr_faisal_607 Telegram : GUBCA
7 Create a class MATRIX of size mxn. Overload + and – operators for addition and
subtraction of the MATRIX.
#include<iostream.h>
#include<conio.h>
class matrix
int arr[2][2];
public:
void get_matrix_data();
};
void matrix::get_matrix_data()
81
Instagram mr_faisal_607 Telegram : GUBCA
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>arr[i][j];
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
82
Instagram mr_faisal_607 Telegram : GUBCA
}
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
void main()
matrix m1,m2;
int i;
clrscr();
m1.get_matrix_data();
m2.get_matrix_data();
83
Instagram mr_faisal_607 Telegram : GUBCA
m1+m2;
m1-m2;
getch();
#include<iostream.h>
#include<conio.h>
class coord
int x;
int y;
public:
void get_xy()
cout<<"Enter x: ";
cin>>x;
cout<<"Enter y: ";
84
Instagram mr_faisal_607 Telegram : GUBCA
cin>>y;
x=x+1;
y=y+1;
x=x+1;
y=y+1;
x=-x;
y=-y;
void print_xy()
};
85
Instagram mr_faisal_607 Telegram : GUBCA
void main()
coord c1,c2;
clrscr();
c1.get_xy();
c1++;
c1.print_xy();
++c1;
c1.print_xy();
c2.get_xy();
-c2;
c2.print_xy();
getch();
86
Instagram mr_faisal_607 Telegram : GUBCA
9 Create one class called Rupees, which has one member data tostore amount in
rupee and create another class called Paise which has member data to store
amount in paise. Write a program to convert one amount to another amount with
use of type conversion.
#include<iostream.h>
#include<conio.h>
class paise
int amount;
public:
amount=val;
void printData()
87
Instagram mr_faisal_607 Telegram : GUBCA
}
};
class rupee
int amount;
public:
rupee()
amount=10;
operator paise()
paise p;
p.set_paise(amount * 100);
return p;
};
void main()
rupee r;
paise p;
clrscr();
88
Instagram mr_faisal_607 Telegram : GUBCA
p=r;
p.printData();
getch();
#include<iostream.h>
#include<conio.h>
class celsius
float cels;
public:
celsius()
cels=0;
celsius(float val)
cels=val;
89
Instagram mr_faisal_607 Telegram : GUBCA
void get_cels()
cin>>cels;
float ret_cels()
return cels;
void display_cels()
cout<<"\nCelsius : "<<cels;
};
class fahrenhiet
float fahr;
public:
fahrenhiet()
fahr=0.0;
90
Instagram mr_faisal_607 Telegram : GUBCA
fahrenhiet(celsius c)
fahr=(((c.ret_cels())*1.8)+32.0);
void get_fahr()
cin>>fahr;
void display_fahr()
cout<<"\nFahrenhiet : "<<fahr;
operator celsius()
float c;
c=(fahr-32.0)/1.8;
return c;
};
void main()
91
Instagram mr_faisal_607 Telegram : GUBCA
celsius c;
fahrenhiet f;
clrscr();
c.get_cels();
f=c;
f.display_fahr();
f.get_fahr();
c=f;
c.display_cels();
getch();
92
Instagram mr_faisal_607 Telegram : GUBCA
93
Instagram mr_faisal_607 Telegram : GUBCA