You are on page 1of 93

UNIT :->1

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

float area(float r);

float area(float l,float b);

int area(int i);

void main()

float r,l,b,ar_c,ar_rect;

int length,ar_s;

clrscr();

cout<<"Enter the radius of circle :";

cin>>r;

ar_c=area(r);

cout<<"Area of circle is :"<<ar_c<<endl;

cout<<"Enter length and breadth";

cin>>l>>b;

ar_rect=area(l,b);

cout<<"Area of ractangle is :"<<ar_rect<<endl;

1
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"Enter lenth of square:";

cin>>length;

ar_s=area(length);

cout<<"Area of squre is :"<<ar_s;

getch();

float area(float r)

float ans;

ans = pi * r * r;

return ans;

float area(float l,float b)

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

float area(float r);

float area(float l,float b);

int area(int =9);

void main()

float r=4,l,b;

int length = 5;

clrscr();

cout<<"Area of circle is :"<<area(r)<<endl;

cout<<"Enter length and breadth";

cin>>l>>b;

cout<<"Area of ractangle :"<<area(l,b)<<endl;

cout<<"Area of square is :"<<area()<<endl;

cout<<"Area of square is :"<<area(length);

getch();

float area(float r)

4
Instagram mr_faisal_607 Telegram : GUBCA
{

float ans;

ans = pi * r * r;

return ans;

float area(float l,float b)

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;

int *p=&b;//pointer to be with diff. add.

int &rb=b;//rb has same add as b

int *pb=&rb;//pointer to rb with diff. add.

clrscr();

cout<<"Address and value of the variable b:"<<&b<<"\t"<<b;

cout<<"\n Address and value of the variable rb:"<<&rb<<"\t"<<rb;

cout<<"\nValue and Address of pointer p:"<<p<<"\t"<<&p;

cout<<"\nvalue and Address of pointer pb"<<pb<<"\t"<<&pb;

rb=rb+10;

cout<<"\nValue of b using pointer varivable ="<<*p;

cout<<"\nValue of rb using pointer variable ="<<*pb;

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

cout<<endl<<"Enter student roll no :";

cin>>roll_no;

cout<<"Enter student name :";

cin>>name;

cout<<"Enter Marks in science :";

cin>>sci;

7
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"Enter Marks in maths :";

cin>>maths;

cout<<"Enter Marks in English :";

cin>>eng;

cout<<"Enter Marks in c :";

cin>>c;

cout<<"Enter Marks in c++ :";

cin>>cpp;

void total_marks()

total=sci+maths+eng+c+cpp;

cout<<"\nTotal marks :"<<total;

void display_data()

cout<<"Roll No.:"<<roll_no<<endl;

cout<<"Name:"<<name<<endl;

cout<<"Marks in following subject"<<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>

double power(double m,int n=2);

void main()

double ans,m,n;

char choice;

clrscr();

cout<<"Enter value of m: ";

cin>>m;

cout<<"Do you want to consider default argument ? (Y/N)";

cin>>choice;

if(choice=='Y'||choice=='y')

ans=power(m);

cout<<"The power of "<<m<<" is "<<ans;

10
Instagram mr_faisal_607 Telegram : GUBCA
}

else

cout<<"Enter value of n:";

cin>>n;

ans=power(m,n);

cout<<"The power of ="<<m<<" is :"<<ans;

getch();

double power(double m,int n)

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;

friend void swap(class1 &,class2 &);

public:

void get_data()

cout<<"Enter value of a:";

cin>>a;

void display()

cout<<"\n After swap a="<<a;

};

class class2

14
Instagram mr_faisal_607 Telegram : GUBCA
int b;

friend void swap(class1 &,class2 &);

public:

void get_data()

cout<<"Enter value of b:";

cin>>b;

void display()

cout<<"\n After swap b="<<b;

};

void swap (class1 &t1, class2 &t2)

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

cout<<"Enter num1: ";

cin>>num1;

cout<<"Enter num2: ";

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;

friend void find_max(class1,class2);

public:

class1()

num1=56;

~class1()

cout<<"\nClass 1 Destructor called";

};

class class2

23
Instagram mr_faisal_607 Telegram : GUBCA
{

int num2;

friend void find_max(class1,class2);

public:

class2()

num2=45;

~class2()

cout<<"\nClass 2 Destructor called";

};

void find_max(class1 c1,class2 c2)

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

cout<<"Both are equal";

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;

friend void find_avg(class1,class2,class3);

public:

class1()

num1=39;

~class1()

cout<<"\nClass1 Destructor called.";

};

class class2

26
Instagram mr_faisal_607 Telegram : GUBCA
{

int num2;

friend void find_avg(class1,class2,class3);

public:

class2()

num2=66;

~class2()

cout<<"\nClass2 destructor called.";

};

class class3

int num3;

friend void find_avg(class1,class2,class3);

public:

class3()

num3=78;

27
Instagram mr_faisal_607 Telegram : GUBCA
~class3()

cout<<"\nClass3 destructor called.";

};

void find_avg(class1 c1,class2 c2,class3 c3)

float avg;

avg=(c1.num1+c2.num2+c3.num3)/3.0;

cout<<"Average of "<<c1.num1<<","<<c2.num2<<","<<c3.num3<<" is:


"<<avg;

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;

friend currency addcurrency(currency,currency);

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 addcurrency(currency c1,currency c2)

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;

void currency :: display_data()

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;

calender(int d,int m,int y)

day=d;

month=m;

year=y;

~calender()

cout<<"\nDestructor called.";

33
Instagram mr_faisal_607 Telegram : GUBCA
}

void display();

void add_days(int);

};

void calender :: add_days(int d)

day=day+d;

if(month==1 || month==3 || month==5 || month==7 || month==8 ||


month==10 ||month==12)

if(day>31)

month++;

day=day-31;

if(month>12)

month=1;

year++;

else if(month==4 || month==6 || month==9 || month==11)

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

char *txt="Hello world!";

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

};

class male:public employee

int days;

int hours;

public:

39
Instagram mr_faisal_607 Telegram : GUBCA
void get_data();

};

class female:public employee

int hours;

public:

void get_data();

female()

hours=40;

};

void employee::get_empl_details()

cout<<"\nEnter ID:";

cin>>id;

cout<<"\nEnter name:";

cin>>name;

cout<<"\nEnter scale of salary on hourly basi:";

cin>>scale_of_salary;

void male::get_data()

40
Instagram mr_faisal_607 Telegram : GUBCA
{

get_empl_details();

cout<<"\nEnter no of days worked:";

cin>>days;

cout<<"\nEnter no of hours worked per day:";

cin>>hours;

int totalHours=0;

totalHours=days*hours;

computepay(totalHours);

void female::get_data()

get_empl_details();

computepay(hours);

void employee::computepay(int hrs)

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;

};

class customer:public scheme

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

cout<<"Enter scheme ID:";

cin>>scheme_id;

cout<<"Enter scheme Name:";

cin>>scheme_name;

cout<<"Enter outgoing Rate:";

cin>>call_rate;

cout<<"Enter message rate:";

cin>>message_rate;

void customer::get_cust_data()

cout<<"Enter customer ID:";

cin>>cust_id;

cout<<"Enter customer Name:";

cin>>cust_name;

cout<<"Enter mobile number:";

cin>>mob_num;

45
Instagram mr_faisal_607 Telegram : GUBCA
void customer::read_info()

int total_calls,total_msg;

cout<<"Enter Total calls:";

cin>>total_calls;

cout<<"Enter Total Message:";

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();

cout<<"\n1.Give scheme Details:";

cout<<"\n2.Give customer Details:";

46
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"\n3.Give call & message info:";

cout<<"\n4.Show bill:";

cout<<"\n0.Exit:";

do

cout<<"\nEnter your choice:";

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

};

class book:public publisher

private:

int page_no;

public:

void get_book_detail();

void display_book_detail();

};

class tape:public publisher

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

cout<<"Enter book Title:";

cin>>title;

cout<<"Enter page no:";

cin>>page_no;

void tape::get_tape_detail()

cout<<"Enter tape tilte:";

cin>>title;

cout<<"Enter playing time:";

cin>>playing_time;

void book::display_book_detail()

cout<<"\nBook title :"<<title;

50
Instagram mr_faisal_607 Telegram : GUBCA
cout<<"\nBook page no:"<<page_no;

void tape::display_tape_detail()

cout<<"\nTape title:"<<title;

cout<<"\nPlaying time: "<<playing_time;

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:

• Accepts deposit from customer and update balance

• Compute and Deposit interest

• Permit withdrawal and Update balance.

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

cout<<"Enter customer name: ";

cin>>cust_name;

cout<<"Enter Account no: ";

cin>>account_no;

cout<<"Enter Account Type (s/c): ";

cin>>account_type;

class cur_acc : public account

private:

double balance;

public:

void deposite_money();

void withdraw_money();

void compute_interest();

cur_acc()

balance=0;

53
Instagram mr_faisal_607 Telegram : GUBCA
}

};

class sav_acc : public account

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;

cout<<"\nBalance After withdrwel :"<<balance;

else

cout<<"\nInsufficient Balance";

void cur_acc::compute_interest()

float rate_of_interest=get_roi();

balance=balance+((balance * rate_of_interest)/100);

cout<<"\nBalance after interest :"<<balance;

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;

cout<<"\nBalance After withdrwel :"<<balance;

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

cout<<"\nBalance After interest:"<<balance;

long get_cuttent_balance()

long balance;

cout<<"Enter current balance:";

cin>>balance;

return balance;

long get_amount()

long amount;

cout<<"Enter amount:";

cin>>amount;

return amount;

float get_roi()

float roi;

cout<<"Enter Rate of interset:";

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<<"\nSelect from below options";

cout<<"\n1.Deposite Amount.";

cout<<"\n2.withdraw amount.";

cout<<"\nEnter your choice:";

cin>>choice;

if(choice==1)

if(acc.account_type=='s' || acc.account_type=='S')

sb.deposit_money();

58
Instagram mr_faisal_607 Telegram : GUBCA
}

else if(acc.account_type=='C' || acc.account_type=='c')

cr.deposite_money();

else

cout<<"Invalid account type.";

else if(choice==2)

if(acc.account_type=='s' || acc.account_type=='S')

sb.withdraw_money();

else if(acc.account_type=='C' || acc.account_type=='c')

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

1. Create a class vehicle which stores the vehicleno and chassisno as a


member. Define another class for scooter, which inherits the data members
of the class vehicle and has a data member for a storing wheels and
company. Define another class for which inherits the data member of the
classvehicle and has a data member for storing price and company. Display
the data from derived class. Use virtual function.

#include<iostrem.h>

#include<conio.h>

class vehicle

protected:

int vehicle_no;

int chasis_no;

public:

virtual void print_name();

};

class scooter:public vehicle

private:

int no_of_wheels;

char *company;

public:

61
Instagram mr_faisal_607 Telegram : GUBCA
void print_name();

void get_data();

void display_data();

};

class car:public vehicle

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

cout<<"\nEnter vehicle No:";

cin>>vehicle_no;

cout<<"Enter chasis no:";

cin>>chasis_no;

cout>>"Enter no of wheels:";

cin>>no_of_wheels;

cout<<"Enter company:";

cin>>company;

void scooter::display_data()

cout<<"\nVehicle No: "<<vehicle_no;

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

cout<<"\nEnter vehicle No:";

cin>>vehicle_no;

cout<<"Enter chasis no:";

cin>>chasis_no;

cout>>"Enter price:";

cin>>price;

cout<<"Enter compnay:";

cin>>company;

void car::display_data()

cout<<"\nVehicle No: "<<vehicle_no;

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=&sc;

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

cout<<"Enter length: ";

cin>>len;

cout<<"Enter width: ";

cin>>width;

virtual void display_area();

};

void shape::display_area()

cout<<"This funtion is used to calculate the area.";

class triangle:public shape

public:

void display_area();

66
Instagram mr_faisal_607 Telegram : GUBCA
};

void triangle::display_area()

cout<<"Area of triangle is: "<<(len * width)/2<<endl;

class rectangle:public shape

public:

void display_area();

};

void rectangle::display_area()

cout<<"Area of Rectangle is : "<<(len * width)<<endl;

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();

3 Write a program to demonstrate the use of pure virtual function.

#include<iostream.h>

#include<conio.h>

class base

protected:

int num1;

int num2;

68
Instagram mr_faisal_607 Telegram : GUBCA
public:

virtual void calculate_data()=0;

};

class derive1:public base

private:

int result;

public:

void calculate_data()

cout<<"Enter num1: ";

cin>>num1;

cout<<"Enter num2: ";

cin>>num2;

result=num1+num2;

cout<<"The sum of number is :"<<result<<endl;

};

class derive2:public base

private:

int result;

69
Instagram mr_faisal_607 Telegram : GUBCA
public:

void calculate_data()

cout<<"Enter num1: ";

cin>>num1;

cout<<"Enter num2: ";

cin>>num2;

result=num1 * num2;

cout<<"The multiplication of number is :"<<result<<endl;

};

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

cout<<"Enter hour and minute:";

cin>>hour>>minute;

void print_time()

cout<<"\nTime : "<<hour<<":"<<minute<<endl;

void operator ++();//prefix

void operator ++(int);//potfix

void operator --();//prefix

void operator --(int);//potfix

};

void time::operator ++()

cout<<"prefix ++";

hour=hour+1;

minute=minute+1;

while(minute>=60)

hour=hour+1;

72
Instagram mr_faisal_607 Telegram : GUBCA
minute=minute-60;

void time::operator ++(int)

cout<<"postfix ++";

hour=hour+1;

minute=minute+1;

while(minute>=60)

hour=hour+1;

minute=minute-60;

void time::operator --()

cout<<"prefix --";

hour=hour-1;

if(minute>0)

minute=minute-1;

else

73
Instagram mr_faisal_607 Telegram : GUBCA
hour=hour-1;

minute=59;

void time::operator --(int)

cout<<"postfix --";

hour=hour-1;

if(minute>0)

minute=minute-1;

else

hour=hour-1;

minute=59;

void main()

time t1,t2;

clrscr();

cout<<"Increment time "<<endl;

74
Instagram mr_faisal_607 Telegram : GUBCA
t1.get_time();

t1++;

t1.print_time();

++t1;

t1.print_time();

cout<<"Decrement time "<<endl;

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;

void operator +(string st)

strcat(str," ");

strcat(str,st.str);

cout<<"Enter string is :"<<str;

};

void main()

clrscr();

string s1("Hello all!");

string s2("how are you");

s1+s2;

76
Instagram mr_faisal_607 Telegram : GUBCA
getch();

6 Create a class distance which contains feet and inch as a datamember.


Overhead = =, operator for the same class. Create necessary functions and
constructors too.

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

cout<<"\nboth distance are same ";

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)

cout<<"\nd1 is less ";

else

cout<<"\nd2 less or equal ";

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 operator +(matrix);

void operator -(matrix);

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

cout<<"\nElemetn ["<<i<<"] ["<<j<<"] ";

cin>>arr[i][j];

void matrix::operator +(matrix m)

int i,j;

for(i=0;i<2;i++)

for(j=0;j<2;j++)

cout<<"\nAddition of element ["<<i<<"] ["<<j<<"] is :


"<<arr[i][j]+m.arr[i][j];

82
Instagram mr_faisal_607 Telegram : GUBCA
}

void matrix::operator -(matrix m)

int i,j;

for(i=0;i<2;i++)

for(j=0;j<2;j++)

cout<<"\nSubtractoin of element ["<<i<<"] ["<<j<<"] is :


"<<arr[i][j]-m.arr[i][j];

void main()

matrix m1,m2;

int i;

clrscr();

cout<<"\nEnter matrix data: ";

m1.get_matrix_data();

cout<<"\nEnter another matrix data: ";

m2.get_matrix_data();

83
Instagram mr_faisal_607 Telegram : GUBCA
m1+m2;

m1-m2;

getch();

8 Define a class Coord, which has x and y coordinates as itsdata members.


Overload ++ and – operators for the Coordclass. Create both its prefix and postfix
forms

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

void operator ++()

x=x+1;

y=y+1;

void operator ++(int)

x=x+1;

y=y+1;

void operator -()

x=-x;

y=-y;

void print_xy()

cout<<"\nx: "<<x<<"\ty: "<<y<<endl;

};

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:

void set_paise(int val)

amount=val;

void printData()

cout<<"\n Amount in paise is: "<<amount;

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();

10 Create two classes Celsius and Fahrenheit to store temperaturein terms of


Celsius and Fahrenheit respectively. Includenecessary functions to read and
display the values. Defineconversion mechanism to convert Celsius object to
Fahrenheitobject and vice versa. Show both types of conversions in mainfunction.

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

cout<<"\nEnter celsius : ";

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

cout<<"\nEnter fahrenhiet : ";

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();

//conversion from celsius to fahrenhit

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

You might also like