CPP
CPP
CPP
Roll no.1901
Practical File of
Computer Programming
And
Problem Solving Through
’C++’
INDEX
4. Functions 22-32
2|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
3|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Sum is70
#include<conio.h>
int main()
int a,b;
clrscr();
cout<<"\nEnter 2 numbers:";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<a<<" "<<b;
return 0;
Output:
Enter 2 numbers: 66
#include<conio.h>
int main()
4|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
float p,r,t,si;
clrscr();
cin>>p>>r>>t;
si=(p*r*t)/100;
cout<<"Simple Interest="<<si;
getch();
return 0;
Output:
Enter principal,rate and time=
250 10 2
Simple Interest=50
#include<conio.h>
int main()
{ int a,b,res;
clrscr();
cout<<"Enter 2 numbers:";
cin>>a>>b;
res=a+b;
cout<<"\nAddition="<<res;
res=a-b;
5|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"\nSubtraction="<<res;
res=a*b;
cout<<"\nMultiplication="<<res;
res=a/b;
cout<<"\nDivision="<<res;
getch();
return 0;
Output:
Enter 2 numbers: 999
Addition=1002
Subtraction=996
Multiplication=2997
Division=333
6|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
int main()
int n;
clrscr();
cout<<"Enter an integer:";
cin>>n;
if(n%2==0)
cout<<n<<"is even";
else
cout<<n<<"is odd.";
return 0;
Output:
Enter an integer: 77
#include<conio.h>
7|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void main()
clrscr();
int a=0;
while(a<10)
++a;
cout<<a<<endl;
getch();
Output:
1
10
#include<conio.h>
8|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
int main()
clrscr();
int a,b,c;
cin>>a>>b>>c;
if(a>b)
if(a>c)
cout<<"A="<<a<<"is largest"<<endl;
else
cout<<"C="<<c<<"is largest"<<endl;
else
if(b>c)
cout<<"B="<<b<<"is largest"<<endl;
else
cout<<"C="<<c<<"is largest"<<endl;
getch();
return 0;
Output:
Enter three numbers:67 87 99
9|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
C=99is largest
#include<conio.h>
int main()
clrscr();
int i,j,n;
cin>>n;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
cout<<("*");
cout<<("\n");
getch();
return 0;
Output:
Enter how many rows you want 6
10 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
**
***
****
*****
******
#include<conio.h>
int main()
int i,no,table=1;
clrscr();
cin>>no;
cout<<"Table of"<<no;
for(i=1;i<=10;i++)
table=no*i;
cout<<"\n"<<table;
cout<<"\n";
getch();
return 0;
Output:
11 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Enter the number:3
Table of3
12
15
18
21
24
27
30
12 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
int main()
clrscr();
cout<<"Enter a number:";
cin>>num;
int fact=1;
for(int i=2;i<=num;i++)
fact=fact*i;
cout<<"Factorial of"<<num<<"is="<<fact;
getch();
return 0;
Output:
Enter a number:5
Factorial of 5 is=120
#include<iomanip.h>
#include<conio.h>
int main()
13 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
int a,b,c;
clrscr();
cin>>a>>b>>c;
int d=(a>b)?((a>c)?a:c):((b>c)?b:c);
cout<<"Greatest number="<<setw(5)<<d;
getch();
return 0;
}
Output:
Enter three numbers a,b,c:
56 77 103
#include<conio.h>
int main()
int a,b,res;
clrscr();
cout<<"Enter 2 numbers:";
cin>>a>>b;
res=a+b;
14 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"\nAddition="<<res;
res=a-b;
cout<<"\nSubtraction="<<res;
res=a*b;
cout<<"\nMultiplication="<<res;
res=a/b;
cout<<"\nDivision="<<res;
getch();
return 0;
Output:
Enter 2 numbers: 999
Addition=1002
Subtraction=996
Multiplication=2997
Division=333
#include<conio.h>
int x=20;
int main()
int x=10;
15 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"Global variable x="<<::x;
getch();
return 0;
Output:
Local variable x=10
#include<conio.h>
int main()
int i;
float j;
clrscr();
cout<<"sizeof integer="<<sizeof(i)<<'\n';
cout<<"sizeof integer="<<sizeof(int)<<'\n';
cout<<"sizeof float="<<sizeof(j)<<'\n';
cout<<"sizeof float="<<sizeof(float);
getch();
return 0;
Output:
sizeof integer=2
sizeof integer=2
16 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
sizeof float=4
sizeof float=4
#include<conio.h>
int main()
clrscr();
float sub1,sub2,sub3,sub4,sub5,percentage,total;
cin>>sub1>>sub2>>sub3>>sub4>>sub5;
total=sub1+sub2+sub3+sub4+sub5;
percentage=(total/500)*100;
return 0;
Output:
Enter the marks obtained in 5 subjects: 89 67 88 93 77
17 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<iostream.h>
int main()
cout<<"Enter name"<<endl;
getch();
return 0;
Output:
Enter name
#include<conio.h>
int main()
int i;
clrscr();
18 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"Enter hexadecimal number=";
cin>>hex>>i;
cout<<"hexadecimal value="<<hex<<i<<endl;
cout<<"octal value="<<oct<<i<<endl;
cout<<"decimal value="<<dec<<i<<endl;
getch();
return 0;
Output:
Enter hexadecimal number=f
hexadecimal value=f
octal value=17
decimal value=15
#include<iomanip.h>
#include<conio.h>
int main()
{ clrscr();
int age=19,rollno=1901;
getch();
19 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
return 0;
Output:
My Rollno is 19
My age is 1901
#include<iomanip.h>
#include<conio.h>
int main()
int age=19,rollno=1901;
cout<<setfill('#');
cout<<setw(4)<<age<<setw(6)<<rollno<<endl;
cout<<setw(6)<<age<<setw(8)<<rollno;
getch();
return 0;
Output:
##19##1901
####19####1901
#include<iomanip.h>
20 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
int main()
float a=129.455396;
clrscr();
cout<<setprecision(2)<<a<<endl;
cout<<setprecision(3)<<a;
getch();
return 0;
Output:
129.46
129.455
21 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Functions
1.Program to find the sum of the series.
S=1+x2+x4+x6+………..(n terms)
#include<iostream.h>
#include<conio.h>
#include<math.h>
Int main()
Int n,x,sum=1;
clrscr();
cout<<”Enter x=”;
cin>>x;
cin>>n;
int k=2*n-1;
for(int i=2;i<=k;i=i+2)
Sum=sum+pow(x,1);
cout<<”Sum is =”<<sum;
getch();
return 0;
22 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
}
Output:
Enter x = 2
Sum is =341
#include<conio.h>
int main()
clrscr();
cout<<"Enter a number:";
cin>>num;
int fact=1;
for(int i=2;i<=num;i++)
fact=fact*i;
cout<<"Factorial of"<<num<<"is="<<fact;
getch();
return 0;
Output:
Enter a number:5
23 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Factorial of 5 is=120
#include<conio.h>
int main()
clrscr();
int a,b;
void swap(int,int);
cin>>a>>b;
swap(a,b);
getch();
return 0;
int z;
z=x;
x=y;
y=z;
24 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Output:
#include<iostream.h>
#include<conio.h>
int main()
clrscr();
int a,b;
void swap(int*,int*);
a=10;b=25;
swap(&a,&b);
getch();
return 0;
int z;
z=*x;
25 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
*x=*y;
*y=z;
cout<<"\nAfter modification="<<*x<<"y="<<*y;
Output:
Before calling a=10 b=25
#include<conio.h>
int main()
clrscr();
int a,b;
cin>>a>>b;
swap(a,b);
getch();
return 0;
26 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
int z;
z=x;
x=y;
y=z;
Output:
Enter two numbers= 45 56
#include<conio.h>
clrscr();
int l,b,area,perimeter;
cin>>l>>b;
cal_rect(l,b,area,perimeter);
cout<<"\nArea of rectangle="<<area;
cout<<"\nPerimeter of rectangle="<<perimeter;
27 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
getch();
return 0;
ar=x*y;
pr=2*(x+y);
Output:
Enter length and breadth of rectangle= 2 4
Area of rectangle=8
Perimeter of rectangle=12
#include<conio.h>
int main()
clrscr();
int x=10;
modify(x)=25;
cout<<"\nValue of x="<<x;
getch();
return 0;
28 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
int modify(int &y)
cout<<"\nValue of y="<<y;
return y;
Output:
Value of y=10
Value of x=25
#include<iostream.h>
#include<conio.h>
void area(int);
void area(int,int);
void area(int,int,int);
int main()
clrscr();
int side=10;
int le=5,br=6;
int a=4,b=5,c=6;
area(side);
area(le,br);
29 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
area(a,b,c);
getch();
return 0;
void area(int x)
cout<<"Area of square="<<x*x;
cout<<"\nArea of rectangle="<<x*y;
float s=(x+y+z)/2;
float ar;
ar=sqrt(s*(s-x)*(s-y)*(s-z));
Output:
Area of square=100
Area of rectangle=30
30 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
9.Program to show the use of inline function which calculates the
maximum of two numbers.
#include<iostream.h>
#include<conio.h>
return(x>y?x:y);
int main()
clrscr();
int m=10,n=25;
int a,b;
a=max(6,8);
b=max(m,n);
getch();
return 0;
Ouput:
Greatest of 6 and 8=8
31 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
10. Program to demonstrate the use of default arguments to
calculate the simple interest.
#include<iostream.h>
#include<conio.h>
int main()
clrscr();
float k;
k=SI(10000);
cout<<"Simple interest="<<k<<endl;
k=SI(12000,6.0);
cout<<"Simple interest="<<k<<endl;
k=SI(15000,6.0,0.5);
cout<<"simple interest="<<k;
getch();
return 0;
Output:
Simple interest=550
Simple interest=720
Simple interest=450
32 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
class rectangle
private:
int a,b;
public:
a=x;
b=y;
void area()
Int ar=a*b;
Cout<<”\nArea of rectangle=”<<ar;
33 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
};
int main()
clrscr();
rectangle r1 r2;
r1.setdata(5,10);
cout<<”Rectangle 1”;
r1.area();
r2.setdata(10,20);
cout<<”\nRectangle 2”;
r2.area();
getch();
return 0;
Output:
Rectangle 1
Area of rectangle = 50
Rectangle 2
#include<conio.h>
class rectangle
34 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
private:
int a,b;
public:
void setdata(int,int);
void area();
};
a=x;
b=y;
Int ar=a*b;
cout<<”\nArea of rectangle=”<<ar;
Int main()
clrscr();
rectangle r1,r2;
r1.setdata(3,10);
cout<<”Rectangle r1”;
r1.area();
r2.setdata(10,20);
cout<<”Rectangle r2”;
35 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
r2.area();
getch();
return 0;
Output:
Rectangle r1
Area of rectangle = 30
Rectangle r2
#include<conio.h>
#include<iomanip.h>
Class circle
private:
double r;
public:
void input();
void area();
void circumference();
};
36 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<”Enter radius of circles=”;
cin>>r;
double ar=3.1415*r*r;
double cir=2*3.1415r;
cout<<”\nCircumference of circle=”<<setprecision(2)<<cir;
Int main()
clrscr();
circle c1;
c1.input();
c1.area();
c1.circumference();
getch();
return 0;
Output:
Enter radius of circle = 3.5
37 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Area of circle = 38.48
#include<conio.h>
class account
private:
int acc_no;
char name[15];
double balance;
public:
void input_data();
void display_data();
};
void account::input_data()
cin>>acc_no>>name>>balance;
void account::display_data()
cout<<"\nAccount number=\t"<<acc_no;
cout<<"\nAccount balance=\t"<<balance;
38 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
}
int main()
clrscr();
account acc[10];
int n;
cin>>n;
for(int i=0;i<n;i++)
acc[i].input_data();
for(i=0;i<n;i++)
cout<<"\nInformation of account="<<i+1;
acc[i].display_data();
getch();
return 0;
Output:
39 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Information of account=1
Information of account=2
#include<conio.h>
class time
private:
int hours,minutes;
public:
void read_time();
void add_time(time,time);
void show_time;
};
void time::read_time()
40 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"\nEnter time in hours and minures:";
cin>>hours>>minutes;
int hrs=min/60;
minutes=min%60;
hours=hrs+tt1.hours+tt2.hours;
void time::show_time()
cout<<hours<<minutes;
int main()
clrscr();
time t1,t2,t3;
t1.read_time();
t2.read_time();
t3.add_time(t1,t2);
t3.show_time();
getch();
return 0;
41 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
}
Ouput:
Enter time in hours and minutes: 5 30
#include<conio.h>
class acc_info
private:
int acctno;
float balance;
public:
void read();
void show();
};
void acc_info::read()
cin>>acctno;
cin>>balance;
42 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void acc_info::show()
cout<<"\nBalance is = "<<balance;
balance=balance-amt;
acct.balance=acct.balance+amt;
void main()
clrscr();
acct1.read();
acct2.read();
acct1.show();
acct2.show();
double money_trans;
cin>>money_trans;
acct1.transfer_money(acct2,money_trans);
43 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"\nDisplaying account information aftrer tranfer of money...";
acct1.show();
acct2.show();
getch();
Output:
Enter account information of acct 1..
Balance is =5000
Balance is =2000
Balance is =4500
Balance is =2500
44 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
class account
private:
public:
void read()
cin>>acc_no>>balance;
void show()
cout<<"\nInterest = "<<rate<<'\t';
cout<<"\nBalance = "<<balance;
void qtr_rate_cal()
double interest=(balance*rate*0.25);
balance=balance+interest;
};
45 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
double account::rate=0.05;
int main()
clrscr();
account a1,a2;
a1.read();
a2.read();
a1.qtr_rate_cal();
a2.qtr_rate_cal();
a1.show();
a2.show();
getch();
return 0;
Output:
Enter customer 1 information….
46 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
class account
private:
public:
void read()
cin>>acc_no>>balance;
void show()
cout<<"\nInterst = "<<rate;
cout<<"\nBalance = "<<balance;
void qtr_rate_cal()
double interest=(balance*rate*0.25)/100;
balance=balance+interest;
47 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
rate=rate+incr;
};
double account::rate=0.05;
void main()
clrscr();
account a1,a2;
a1.read();
a2.read();
account::modify_rate(0.01);
a1.qtr_rate_cal();
a2.qtr_rate_cal();
a1.show();
a2.show();
getch();
Output:
Enter account number and balance-> 1901
500
2000
48 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Modified rate of interest = 0.06
Interst = 0.06
Balance = 500.075
Interst = 0.06
Balance = 2000.3
#include<conio.h>
#include<process.h>
class matrix
private:
int a[10][10],m,n;
public:
void read();
matrix mul(matrix);
void show();
};
void matrix::read()
cin>>m>>n;
49 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"Enter Elements:";
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
void matrix::show()
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cout<<a[i]<<'\t';
cout<<endl;
if(n!=mm2.m)
exit(0);
matrix temp;
temp.m=m;
temp.n=mm2.n;
for(int i=0;i<temp.m;i++)
50 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
for(int j=0;j<temp.n;j++)
temp.a[i][j]=0;
for(int k=0;k<n;k++)
temp.a[i][j]+=a[i][k]*mm2.a[k][j];
return(temp);
int main()
matrix m1,m2,m3;
clrscr();
m1.read();
m2.read();
m3=m1.mul(m2);
m3.show();
getch();
return 0;
Output:
Enter order of m x n matrix= 2 2
Enter elements : 5 6 2 1
Enter elements: 4 2 1 3
51 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
On multilplication we get
26 28
9 7
#include<conio.h>
class student
private:
int rollno,marks;
public:
void read()
cin>>rollno>>marks;
void cal()
tot_marks+=marks;
double avg=double(tot_marks)/n1;
52 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"average percentage of marks is="<<avg;
};
int student::tot_marks;
int main()
clrscr();
student s[50];
int n;
cin>>n;
for(int i=0;i<n;i++)
s[i].read();
s[i].cal();
student::avg_per_marks(n);
getch();
return 0;
Output:
Enter numbers of student:2
53 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
54 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
2. Program to show parameterized constructor.
#include<iostream.h>
#include<conio.h>
class rectangle
{
private:
int length;
int breadth;
public:
rectangle(int a,int b)
{
length=a;
breadth=b;
}
int area()
{
return(length*breadth);
}
};
int main()
{
clrscr();
rectangle r1(5,6);
rectangle r2(7,8);
cout<<"\nArea of rectangle 1="<<r1.area();
cout<<"\nArea of rectangle 2="<<r2.area();
getch();
return 0;
}
Output:
Area of rectangle 1= 30
Area of rectangle 2= 56
55 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
int length;
int breadth;
public:
rectangle(int l,int b)
{
length=l;
breadth=b;
}
void showarea()
{
cout<<"\narea of rectangle="<<length*breadth;
}
};
int main()
{
clrscr();
rectangle r[3]={rectangle(5,6),rectangle(7,8),rectangle(5,7)};
cout<<"\nDispaly area of rectangles\n";
for(int i=0;i<3;i++)
r[i].showarea();
return 0;
}
Output:
Displaying area of rectangle
Area of rectangle = 30
Area of rectangle = 56
Area of rectangle = 35
56 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void show();
};
complex::complex(double x,double y)
{
real=x;
imag=y;
}
void complex::add(complex c1,complex c2)
{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
void complex::show()
{
if(imag>0)
cout<<real<<"+"<<imag<<"i"<<endl;
else
cout<<real<<imag<<"i"<<endl;
}
int main()
{
clrscr();
complex c1(6,8);
complex c2(5);
complex c3;
c3.add(c1,c2);
c3.show();
getch();
return 0;
}
Output:
11 + 8 i
57 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
double inches;
public:
height(int f)
{
feet=f;
inches=0.0;
}
height(double f)
{
feet=int(f);
inches=(f-int(f))*12.0;
}
height()
{
feet=inches=0;
}
height(int f,double in)
{
feet=f;
inches=in;
}
void show()
{
cout<<"feet="<<feet<<"inches="<<inches<<endl;
}
};
int main()
{
clrscr();
height h1,h2,h3;
int ht_feet;
cout<<"enter height in terms of feet only:";
cin>>ht_feet;
h1=height(ht_feet);
double ht_fract;
cout<<"enter height in terms of feet in fractional form:";
cin>>ht_fract;
h2=height(ht_fract);
int ft1;
double in1;
58 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"enter height in terms of feet and inches:";
cin>>ft1>>in1;
h3=height(ft1,in1);
h1.show();
h2.show();
h3.show();
getch();
return 0;
}
Output:
Enter height in terms of feet only : 5
Enter height in terms of feet in fractional form : 5.5
Enter height in terms of feet and inches : 5 8
Feet = 5 Inches=0
Feet = 5 Inches =6
Feet =5 Inches =8
59 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"\nend of main";
return 0;
}
Output:
Constructor of object with Id 1 runs
Constructor of object with Id 2 runs
Constructor of object with Id 3 runs
End of main
Object with Id=3 destroyed
Object with Id=2 destroyed
Object with Id=1 destroyed
60 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
}
~string()
{
cout<<"\nrelease memory allocated to "<<name;
delete[] name;
};
int main()
{
clrscr();
string s1("anu");
string s2("Muskan");
s1.display();
s2.display();
s1.compare(s2);
return 0;
}
Output:
Name of person=Anu
Name of person=Muskan
Both objects have different name
Release memory allocated to Muskan
Release memory allocated to Anu
61 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
}
void area()
{
cout<<"area of rectangle="<<l*b;
}
~rectangle()
{
cout<<"\ndestructor invoked";
}
};
int main()
{
clrscr();
rectangle *ptr;
ptr= new rectangle;
ptr->read();
ptr->area();
cout<<"\ndestroying object";
delete ptr;
cout<<"\nend of program";
getch();
return 0;
}
Output:
constructor with no parameter invoked
area of rectangle=35
destroying object
destructor invoked
end of program
62 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
public:
xxx(int xx)
{ x=xx; }
friend void f1(xxx,yyy);
};
class yyy
{
private:
int y;
public:
yyy(int yy)
{ y=yy; }
friend void f1(xxx,yyy);
};
void f1(xxx objx,yyy objy)
{
cout<<"difference="<<objx.x-objy.y;
}
int main()
{
xxx ob1(10);
yyy ob2(5);
f1(ob1,ob2);
return 0;
}
Output:
copy constructor invoked
count=10
count=10
63 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
private:
int dd,mm,yy;
public:
void read();
void show();
}dob;
void read();
void show();
};
int main()
{
clrscr();
student s1;
s1.read();
s1.show();
getch();
return 0;
}
void student::date::read()
{
cin>>dd>>mm>>yy;
}
void student::date::show()
{
cout<<"\nDate="<<dd<<"/"<<mm<<"/"<<yy;
}
void student::show()
{
cout<<"Rollno="<<rollno<<"\tname="<<name;
dob.show();
}
Output:
Enter rollno and name= 312 amit
Enter date of birth = 05 10 2006
Rollno = 312 name = amit
Date =05/10/2006
64 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<iostream.h>
class yyy;
class xxx
{
private:
int x;
public:
xxx(int xx)
{ x=xx; }
friend void f1(xxx,yyy);
};
class yyy
{
private:
int y;
public:
yyy(int yy)
{ y=yy;}
firend void f1(xxx,yyy);
};
void f1(xxx objx,yyy objy)
{
cout<<”Difference=”<<objx.x-objy.y;
}
int main()
{
xxx ob1(10);
yyy obj2(5);
f1(ob1,ob2);
return 0;
}
Output:
Difference = 5
65 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Inheritance
1.Program to show derived class declaration.
#include<iostream.h>
#include<conio.h>
class base_class
private:
int num1;
public:
void base_read()
cin>>num1;
void base_show()
cout<<"num1="<<num1;
};
66 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
private:
int num2;
public:
void derived_read()
cin>>num2;
void derived_show()
cout<<"\nnum2="<<num2;
};
int main()
clrscr();
base_class b1;
derived_class d1;
d1.base_read();
d1.derived_read();
d1.base_show();
d1.derived_show();
b1.base_read();
b1.base_show();
getch();
67 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
return 0;
Output:
Enter number num1= 10
num1=10
num1=5
#include<conio.h>
class base
private:
int priv_base;
protected:
int prot_base;
public:
int get_base_priv()
priv_base=10;
return(priv_base);
};
68 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
class derive:public base
private:
int priv_der;
protected:
int prot_der;
public:
void f1()
int x;
x=get_base_priv();
prot_base=20;
};
int main()
clrscr();
derive d;
int y;
d.f1();
y=d.get_base_priv();
getch();
69 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
return 0;
Output:
Value of base's prive data member=10
#include<conio.h>
class stud_basic
private:
int rollno;
char name[20];
public:
void read()
cin>>rollno>>name;
void show()
cout<<"rollno="<<rollno;
cout<<"\nname="<<name;
70 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
};
private:
int m[4];
double per;
public:
void input();
void cal();
void display();
};
void result::input()
read();
cout<<"\nEnter marks:";
for(int i=0;i<4;i++)
cin>>m[i];
void result::cal()
int tot_marks=0;
for(int i=0;i<4;i++)
tot_marks+=m[i];
per=double(tot_marks)/4;
71 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void result::display()
show();
cout<<"\nMarks ;";
for(int i=0;i<4;i++)
cout<<m[i]<<'\t';
cout<<"\nPercentage ="<<per;
int main()
clrscr();
result r1;
r1.input();
r1.cal();
r1.display();
getch();
return 0;
Output:
Enter rollno and name: 1901 harkirat
Enter marks:56
99
78
72 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
77
rollno=1901
name=harkirat
Marks ;56 99 78 77
Percentage =77.5
#include<conio.h>
class num
protected:
int x,y;
public:
void read()
cin>>x>>y;
void showxy()
};
73 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
private:
int z;
public:
void add()
z=x+y;
void showz()
cout<<"\nZ="<<z;
};
int main()
clrscr();
result r1;
r1.read();
r1.add();
r1.showxy();
r1.showz();
getch();
return 0;
Output:
74 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Enter two numbers= 10 20
x= 10 y= 20
Z=30
#include<conio.h>
class person
private:
char name[20];
public:
void read()
cin>>name>>phno;
void show()
cout<<"\nName="<<name;
cout<<"\nPhone number="<<phno;
};
75 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
private:
int rollno;
char course[20];
public:
void read()
person::read();
cin>>rollno>>course;
void show()
person::show();
cout<<"\nRollno="<<rollno;
cout<<"\nCourse="<<course;
};
private:
int m[4];
double per;
public:
void read();
void cal();
76 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void show();
};
void exam::read()
student::read();
cout<<"Enter marks:";
for(int i=0;i<4;i++)
cin>>m[i];
void exam::cal()
int tot_marks=0;
for(int i=0;i<4;i++)
tot_marks+=m[i];
per=double(tot_marks)/4;
void exam::show()
student::show();
cout<<"\Marks:";
for(int i=0;i<4;i++)
cout<<m[i]<<"\t";
cout<<"\nPercentage="<<per;
int main()
77 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
clrscr();
exam e1;
e1.read();
e1.cal();
e1.show();
getch();
return 0;
Output:
Enter name and phno=harkirat 01614661232
Enter marks: 60 71 89 78 91
Name=harkirat
Phone number=01614661232
Rollno=1901
Course=bcaMarks:60 71 89 78
Percentage=74.5
#include<conio.h>
class base
78 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
public:
base()
};
int a,b;
public:
derv(int x,int y)
a=x;
b=y;
};
int main()
clrscr();
derv d(10,15);
getch();
return 0;
Output:
Base’s no parameter constructor
79 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
7.Program on parameterized constructors in both the base and
derived classes.
#include<iostream.h>
#include<conio.h>
class base
int a;
public:
base(int a1)
a=a1;
void show()
{ cout<<"\na="<<a; }
};
int b;
public:
b=bb;
80 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void show()
base::show();
cout<<"b="<<b;
};
int main()
clrscr();
derv d(10,20);
d.show();
getch();
return 0;
Output:
Base’s single parameter constructor
a=20 b=10
#include<conio.h>
class base1
81 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
public:
base1()
};
class base2
public:
base2()
};
public:
derv()
};
int main()
clrscr();
derv d;
getch();
return 0;
Output:
Base1 no parameter constructor
82 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Base2 no parameter constructor
83 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Operator Overloading
1.Program to overload unary increment operator (++)
#include<iostream.h>
#include<conio.h>
Class score
{ private:
int val;
public:
score()
{ val=0; }
void operator++()
{ val=val }
int show()
{ return(val) }
};
void main()
{ clrscr();
acore s1,s2;
++s1;
++s1;
++s2;
84 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"\nFinal value of s2 object= "<<s2.show();
getch()
Output:-
Initial value of s1 object=0
#include<iostream.h>
#include<conio.h>
Class score
{ private:
int val;
public:
score()
{ val=0; }
void operator++()
{ scorre temp;
val=val+1;
temp.val=val;
return temp;
int show()
{ return(val) }
};
85 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void main()
{ clrscr();
score s1,s2;
++s1;
s2=++s1;
getch()
Output:-
Increment operator
#include<iostream.h>
#include<conio.h>
Class score
{ private:
int val;
public:
score()
{ val=0; }
86 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void operator++()
{ scorre temp;
val=val+1;
temp.val=val;
return temp;
score operator++(int)
{ score temp;
temp.val=val;
val=val+1;
return(temp);
int show()
{ return(val) }
};
void main()
{ clrscr();
score s1,s2;
s2=++s1;
cout<<"\ns1= "<<s1.show();
s2= s1++;
cout<<"\ns1= "<<s1.show();
87 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
getch()
Output:-
S1=1
S1=2
4.program to add two complex numbers in which addition is performed by overloading the
binary operator +
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
Class complex
{ private:
double real,img;
public:
score()
{ real=img=0.0; }
void read()
cin>>real>>img;
{ complex temp;
88 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
temp.real=real+cc2.real;
temp.img=img+cc2.img;
return(temp);
void show()
if(img>0)
cout<<real<<"+"<<img<<"i"<<endl;
else
cout<<real<<img<<"i"<<endl;
};
void main()
{ clrscr();
complex c1,c2,c3;
c1.read();
c2.read();
c3=c1+c2;
c3.show();
getch()
Output:-
89 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Enter real and imaginary part: 3.1 5.2
5.2+11.5i
5.program to compare the date of birth of two persons by overloading the equality operator(==)
#include<iostream.h>
#include<conio.h>
Class dob
{ private:
int dd,mm,yyyy;
public:
dob(int dd,mm,,yyyy)
{ real=img=0.0; }
void show()
cout<<"\ndob= "<<dd<<"-"<<mm<<"-""<<yyyy;
int operator==(dob);
return(1);
else
return(0);
void main()
{ clrscr();
90 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
dob d1(10,12.1976);
dob d2(10,12,1976);
d1.show();
d2,show();
if(d1==d2)
else
getch()
Output:-
Dob=10-12-1976
Dob=10-12-1976
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define SIZE 40
Class string
{ private:
char st[SIZE];
public:
string()
{ strcpy(st,"") }
string(char s[])
91 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
strcpy(st,s);
{ cout<<"st; }
};
{ string temp;
strcpy(temp,st,st);
strcat(temp.st,ss2.st);
return(temp);
void main()
{ clrscr();
string s1="happy";
string s2="diwali";
string s3;
cout<<"before concatenation";
cout<<"s1= ";
s1.show();
cout<<"\nts2 = ";
s2.show();
cout<<""/nts2= ";
s3.show();
s3=s1+s2;
cout<<"\nafter concatenation";
92 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"s3 = ";
getch();
output:-
before concatenation
after concatenation
s3=happy diwali
#include<iostream.h>
#include<conio.h>
Class time
{ private:
int hours,mins,secs;
public:
{ hours=hh,min=mm;secs=ss; }
void show()
{ cout<<"hours<<":"<<mins<<":"<secs; }
void operator+=(time);
};
{ secs+=tt2.secs;
mins+=tt2.mins;
hours+=tt2.hours;
if(secs>59)
93 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{ secs-=60;
mins++;
if(mins>59)
{ mins-=60;
hours++;
void main()
{ clrscr();
time t1(4,58,55);
time t2(3,20,10);
t1.show();
t1+=t2;
t1.show();
getch();
output:-
#include<iostream.h>
#include<conio.h>
Class time
94 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{ private:
int hours,mins;
public:
{ hours=hr,min=mm; }
void show()
opertor int()
{ int mins;
mins=hours*60+mins;
return(mins);
};
void main()
{ clrscr();
time t1(3,15);
t1.show();
minutes=t1;
getch();
output:-
#include<iostream.h>
95 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
Class rupee
{ private:
double rs;
public:
rupee(double rs1)
{ rs=rs1; }
void show()
rupee()
{ rs=0; }
};
class dollar
{ double do1;
public:
dollar(double dol1)
{ dol=dol1; }
operator rupee()
{ double rs1=dol*dol_val;
return(rupee(rs1));
void show()
};
void main()
96 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{ clrscr();
dollar d1(3);
d1.show();
rupee r1;
r1=d1;
r1.show();
getch();
output:-
money in dollars=3
money in Rs=132.6
#include<iostream.h>
#include<conio.h>
Class xyz
public:
{ int cb=num*num*num;
return(cb);
};
void main()
{ clrscr();
xyz cube;
int a=5,res;
97 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
res=cube(a);
getch();
output:-
cube of 5 is=125
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX_SITE 5
Class array
{ int arr[max_size];
public:
int operator[](int n)
{ if ((n>=0)&&(n<MAX_SITE))
return arr[n];
else
};
array::array()
arr[i]=i;
98 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void main()
{ clrscr();
int i;
array a,a1;
t=a1[2];
a1[3]=10;
a[7]=20;
getch();
output:-
99 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Virtual Functions
1.Program on polymorphism using virtual fuction.
#include<iostream.h>
#include<conio.h>
class base
public:
};
public:
void display()
};
public:
void display()
};
int main()
100 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
clrscr();
base *ptr;
derv1 d1;
derv2 d2;
ptr=&d1;
ptr->display();
ptr=&d2;
ptr->display();
getch();
return 0;
Output:
Derv1's display called
#include<conio.h>
class shape
protected:
double a,b;
public:
void read()
101 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cin>>a>>b;
};
public:
void cal_area()
double area=a*b;
cout<<"Area of rectangle="<<area;
};
public:
void cal_area()
double area=(a*b)/2;
cout<<"Area of triangle="<<area;
};
int main()
102 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
clrscr();
shape *ptr;
rectangle r1;
r1.read();
ptr=&r1;
ptr->cal_area();
triangle t1;
t1.read();
ptr=&t1;
ptr->cal_area();
getch();
return 0;
Output:
Enter length and breadth of rectangle: 10 20
Area of rectangle=200
Area of triangle=50
103 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>
int main()
clrscr();
char str[MAX];
cout<<"\nEnter a string:";
cin.get(str,MAX);
cout<<"string is="<<str;
getch();
return 0;
Output:
Enter a string: hello
#include<conio.h>
int main()
clrscr();
104 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
const int MAX=80;
char str[MAX];
cout<<"\Enter a string:";
cin.get(str,MAX,'*');
cout<<"String is =\n"<<str;
getch();
return 0;
Output:
Enter a string:share index is growing
String is =
#include<conio.h>
int main()
clrscr();
char str[MAx];
cout<<"\nEnter a string";
cin.read(str,MAX);
int count=cin.gcount();
105 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
str[count]='\0';
cout<<"string is:"<<str;
getch();
return 0;
Output:
Enter a string:stock index
#include<conio.h>
int main()
clrscr();
char str[20];
cout<<"\Enter a string:";
cin.get(str,20);
int count=cin.gcount();
getch();
return 0;
Output:
Enter a string:stock
106 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
5.Program to demonstrate the use of ignore() member function to
display the first character of your first name and last name.
#include<iostream.h>
#include<conio.h>
int main()
char fname,lname;
fname=cin.get();
cin.ignore(80,’ ‘);
lname=cin.get();
getch();
return 0;
Output:
Enter first and last name=harkirat kalsi
#include<conio.h>
int main()
char ch;
clrscr();
107 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<”Press F6 or ^Z to exit\n”;
cout<<”enter a text:”;
while(cin.get(ch))
cout.put(ch);
while(cin.peek()==ch)
cin.ignore();
return 0;
Output:
Press F6 or ^Z to exit
abc
^Z
#include<conio.h>
#include<ctype.h>
clrscr()
char ch;
while(cin.get(ch))
108 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
cin.putback(toupper(ch));
cin.get(ch);
cout.put(ch);
return 0;
Output:
abcdef
ABCDEF
#include<string.h>
int main()
char str[]="streams";
int len1=strlen(str);
for(int i=1;i<=len1;i++)
cout.write(str,i);
cout<<"\n";
for(i=len1-1;i>0;i--)
109 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
cout.write(str,i);
cout<<"\n";
return 0;
Output:
st
str
stre
strea
stream
streams
stream
strea
stre
str
st
int main()
cout.fill('#');
110 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout.width(6);
cout<<204;
cout.width(4);
cout<<34;
return 0;
Output:
###204##34
#include<iostream.h>
int main()
cout.precision(2);
cout<<3.46<<endl;
cout<<9.0022<<endl;
cout<<7.5056<<endl;
cout.precision(3);
cout<<6.7291<<endl;
return 0;
Output:
3.46
7.51
111 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
6.729
#include<iostream.h>
int main()
cout.width(40);
cout.setf(ios::left);
cout.unsetf(ios::left);
cout.width(40);
cout.setf(ios::right);
return 0;
Output:
Text is left justified Text is right justified.
#include<fstream.h>
int main()
ofstream outf("ITEM");
char name[30];
112 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cin>>name;
outf<<name<<"\n";
float cost;
cin>>cost;
outf<<cost<<"\n";
outf.close();
ifstream inf("ITEM");
inf>>name;
inf>>cost;
cout<<"\n";
cout<<"Item name:"<<name<<"\n";
cout<<"Item cost:"<<cost<<"\n";
inf.close();
return 0;
Output:
Enter item name:CD-ROM
Item name:CD-ROM
Item cost:2500
#include<fstream.h>
113 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
int main()
ofstream fout;
fout.open("country");
fout<<"United Kingdom\n";
fout<<"South Korea\n";
fout.close();
fout.open("capital");
fout<<"Washington\n";
fout<<"London\n";
fout<<"Seoul\n";
fout.close();
char line[n];
ifstream fin;
fin.open("country");
while(fin)
fin.getline(line,n);
cout<<line;
fin.close();
fin.open("capital");
114 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<"\nContents of capital file\n";
while(fin)
fin.getline(line,n);
cout<<line;
fin.close();
return 0;
Output:
United Kingdom
South Korea
Washington
London
Seoul
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int main()
115 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
const int SIZE=80;
char line[SIZE];
ifstream fin1,fin2;
fin1.open("country");
fin2.open("capital");
for(int i=1;i<=10;i++)
if(fin1.eof()!=0)
exit(1);
fin1.getline(line,SIZE);
cout<<"Capital of"<<line;
if(fin2.eof()!=0)
exit(1);
fin2.getline(line,SIZE);
cout<<line<<"\n";
return 0;
Output:
116 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Capital of United States of America
Washington
London
Seoul
Char string[80];
cout<<"Enter a string\n";
cin>>string;
int len=strlen(string);
fstream file;
file.open("TEXT",ios::in|ios::out);
for(int i=0;i<len;i++)
file.put(string[i]);
file.seekg(0);
char ch;
while(file)
file.get(ch);
cout<<ch;
return 0;
Output:
117 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Enter a string
Cobol_Programming input
Cobol_programming output
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
const char*filename="BINARY";
int main()
float height[4]={175.5,153.0,167.25,160.70};
ofstream outfile;
outfile.open(filename);
outfile.close();
for(int i=0;i<4;i++)
height[i]=0;
ifstream infile;
infile.open(filename);
for(i=0;i<4;i++)
cout.setf(ios::showpoint);
cout<<setw(10)<<setprecision(2)<<height[i];
118 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
infile.close();
return 0;
Output:
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
evoid INVENTORY::readdata(void)
cout<<"Enter name:";
cin<<name;
cout<<"Enter code:";
cin<<code;
cout<<"Enter cost:";
119 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cin<<cost;
void INVENTORY::writedata(void)
cout<<setiosflags(ios::left)<<setw(10)<<name<<setiosflags(ios::right)<<setw(10)
<<code<<setprecision(2)<<setw(10)<<cost<<endl;
int main()
INVENTORY item[3];
fstream file;
file.open("stock.dat",ios::in|ios::out);
for(int i=0;i<3;i++)
item[i].readdata();
file.seekg(0);
cout<<"\nOutput\n\n";
for(i=0;i<3;i++)
item[i].writedata();
120 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
file.close();
return 0;
Output:
Enter code:102
Enter cost:150
Enter name:Java
Enter code:103
Enter cost:225
Output
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
121 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
char name[10];
int code;
float cost;
public:
void getdata(void)
cout<<"Enter name";cin>>name;
cout<<"Enter code";cin>>code;
cout<<"Enter cost";cin>>cost;
void putdata(void)
cout<<setw(10)<<name;
cout<<setw(10)<<code<<setprecision(2)<<setw(10)<<cost<<endl;
};
int main()
INVENTORY item;
fstream inoutfile;
inoutfile.seekg(0,ios::beg);
122 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
item.putdata();
inoutfile.clear();
cout<<"\nADD AN ITEM\n";
item.getdata();
char ch;
cin.get(ch);
intoutfile.seekg(0);
item.putdata();
int last=inoutfile.tellg();
int n=last/sizeof(item);
cout<<"Number of objects="<<n<<"\n";
int object;
cin>>object;
cin.get(ch);
int location=(object-1)*sizeof(item);
if(inoutfile.eof())
inoutfile.clear();
123 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
inoutfile.seekp(location);
item.getdata();
cin.get(ch);
inoutfile.seekg(0);
item.putdata();
inoutfile.close();
return 0;
Output:
CURRENT CONTENTS OF STOCK
AA 11 100
BB 22 200
CC 33 300
DD 44 400
XX 99 900
ADD AN ITEM
Enter name:XX
Enter code:99
Enter cost:900
124 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
EXCEPTION HANDLING
1. Program to illustrate how an exception is handled using try-catch
block and throw statement.
#include<iostream.h>
int main()
int x,y;
cin>>x>>y;
try
if(y=0)
throw 10;
cout<<"x/y="<<x/y;
catch (int i)
return 0;
125 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
2. Program to calculate the square root of a given number and handle
all the exceptions
#include<iostream.h>
#include<conio.h>
int main()
int num;
double res;
cin>>num;
try
if(num>oxffff)
throw 10;
if(num<0);
throw 'E';
catch(int)
catch(char)
126 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
return 0;
#include<iostream.h>
#include<conio.h>
int main()
int age;
cin>>age;
try
if(age>0 $$age<18)
throw 0;
else if(age>120)
throw 'v';
else if(age<0)
throw 2.8;
}
127 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
catch(int i)
catch(…)
#include<iostream.h>
#include<conio.h>
if(val==0)
throw 'a';
if(val==1)
throw 10;
int main()
cout<<"start of main";
try
128 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{
func_test(1);
catch(int)
catch(char)
return 0;
129 | P a g e