Sie sind auf Seite 1von 129

Problem Solving Through ‘C++’ Harkirat Kalsi

Roll no.1901

Practical File of
Computer Programming
And
Problem Solving Through
’C++’

Submitted To: Submitted By:


Mrs Satwant Kaur Harkirat Kalsi
BCA-Ist
Roll No.1901

Guru Nanak Khalsa Collage For Women,


Gujarkhan Campus,Model Town,
Ludhiana
1|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901

INDEX

Sr.No. Topic Page Number

1. Selection Control 3-6


Program

2. Control Structure Program 7-17

3. Data Input And Output 18-21

4. Functions 22-32

5. Objects And Classes 33-53

6 Constructer And 54-65


Destructor
7. Inheritance 66-65

8. Operator overloading 83-98

9. Virtual functions 100-103


10. C++ Streams and files 104-124

11. Exception handling 125-128

2|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901

Selection Control Program

1. /*Program to display hello*/


#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
cout<<"Hello World!";
return 0;
}
Output:
Hello World!

2. /*Program to add 2 numbers */


#include<iostream.h>
#include<conio.h>
int main()
{
int a,b,sum;
clrscr();
cout<<"Enter number a=";
cin>>a;
cout<<"Enter number b=";
cin>>b;
sum=a+b;
cout<<"Sum is"<<sum;
getch();
return 0;
}
Output:
Enter number a=30
Enter number b=40

3|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Sum is70

3. Program to swap 2 numbers without third variable.


#include<iostream.h>

#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<<"\nAfter swapping numbers are:";

cout<<a<<" "<<b;

return 0;

Output:
Enter 2 numbers: 66

After swapping numbers are:56 66

4. Program to calculate simple interest.


#include<iostream.h>

#include<conio.h>

int main()

4|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{

float p,r,t,si;

clrscr();

cout<<"Enter principal,rate and time=\n";

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

5. Program to add, subtract, multiple and divide 2 numbers


#include<iostream.h>

#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

Control Structures Programs


1. Program to check*/- whether the number is even or odd.
#include<iostream.h>

#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

2. Program to print first 10 natural numbers.


#include<iostream.h>

#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

3. Program to find largest of 3 numbers.


#include<iostream.h>

#include<conio.h>

8|Page
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
int main()

clrscr();

int a,b,c;

cout<<"Enter three numbers:";

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

4.Program to print pattern.


#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int i,j,n;

cout<<"Enter how many rows you want";

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
**

***

****

*****

******

5. Program to print a table.


#include<iostream.h>

#include<conio.h>

int main()

int i,no,table=1;

clrscr();

cout<<"Enter the number:";

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

6. Program to calculate factorial of a number.


#include<iostream.h>

12 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>

int main()

long int num;

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

7. Program to find greatest of three numbers using ternary


operator.
#include<iostream.h>

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

cout<<"Enter three numbers a,b,c:"<<endl;

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

Greatest number= 103

8. Program to show use of arithmetic operator.


#include<iostream.h>

#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

9. Program to show use of scope resolution operator.


#include<iostream.h>

#include<conio.h>

int x=20;

int main()

int x=10;

cout<<"Local variable x="<<x<<'\n';

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

Global variable x=20

10. Program to show the range of integer data type in C++.


#include<iostream.h>

#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

11.Program to calculate percentage of students in 5 subjects.


#include<iostream.h>

#include<conio.h>

int main()

clrscr();

float sub1,sub2,sub3,sub4,sub5,percentage,total;

cout<<"enter the marks obtained in 5 subjects:";

cin>>sub1>>sub2>>sub3>>sub4>>sub5;

total=sub1+sub2+sub3+sub4+sub5;

percentage=(total/500)*100;

cout<<"\nPercentage marks are:"<<percentage<<"%";

return 0;

Output:
Enter the marks obtained in 5 subjects: 89 67 88 93 77

Percentage marks are:82.800003%

17 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901

Data Input And Output


1.Program to show use of endl manipulator.
#include<conio.h>

#include<iostream.h>

int main()

cout<<"Enter name"<<endl;

cout<<"My name is Harkirat Kalsi";

getch();

return 0;

Output:
Enter name

My name is Harkirat Kalsi

2. Program to show use of decimal, octal, hexadecimal


manipulator.
#include<iostream.h>

#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

3. Program to show use of setw (w) manipulator.


#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

int main()

{ clrscr();

int age=19,rollno=1901;

cout<<setw(12)<<"My Rollno is"<<setw(8)<<rollno<<endl;

cout<<setw(12)<<"My age is"<<setw(8)<<age;

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

4. Program to show use of setfill (c) manipulator.


#include<iostream.h>

#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

5. Program to show use of set precision (n) manipulator.


#include<iostream.h>

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

cout<<”Enter how many terms=”;

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

Enter how many terms = 5

Sum is =341

2.Program to calculate the factorial of a number.


#include<iostream.h>

#include<conio.h>

int main()

long int num;

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

3.Program to show the use of pass by value.


#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int a,b;

void swap(int,int);

cout<<"enter two number=";

cin>>a>>b;

cout<<"\nBefore calling a="<<a<<" b="<<b;

swap(a,b);

cout<<"\nAfter calling a="<<a<<" b="<<b;

getch();

return 0;

void swap(int x,int y)

int z;

z=x;

x=y;

y=z;

cout<<"\nAfter modification x="<<x<<"y="<<y;

24 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Output:

Enter two number=45 65

Before calling a=45 b=65

After modification x=65y=45

After calling a=45 b=65

4.Program to show how Pass by Address works?

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int a,b;

void swap(int*,int*);

a=10;b=25;

cout<<"Before calling a="<<a<<"b="<<b;

swap(&a,&b);

cout<<"After calling a="<<a<<"b="<<b;

getch();

return 0;

void swap(int *x,int *y)

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

After modification=25 y=10 After calling=25 b=10

5.Program to show pass by reference.


#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int a,b;

void swap(int &,int &);

cout<<"Enter two numbers=";

cin>>a>>b;

cout<<"\nBefore calling a="<<a<<" b="<<b;

swap(a,b);

cout<<"\nAfter calling a="<<a<<" b="<<b;

getch();

return 0;

void swap(int &x,int &y)

26 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
int z;

z=x;

x=y;

y=z;

cout<<"\nafter modification x="<<x<<" y="<<y;

Output:
Enter two numbers= 45 56

Before calling a=45 b=56

after modification x=56 y=45

After calling a=56 b=45

6. Program to calculate area and perimeter of rectangle by making


function that return more than one value.
#include<iostream.h>

#include<conio.h>

clrscr();

void cal_rect(int,int &,int &);

int l,b,area,perimeter;

cout<<"Enter length and breadth of rectangle:";

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;

void cal_rect(int x,int y,int &ar,int &pr)

ar=x*y;

pr=2*(x+y);

Output:
Enter length and breadth of rectangle= 2 4

Area of rectangle=8

Perimeter of rectangle=12

7.Program to show function returning reference.


#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int x=10;

int &modify(int &);

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

8. Program to show the concept of function overloading to


calculate area where same name functions differs in number of
parameters.
#include<math.h>

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

void area(int x,int y)

cout<<"\nArea of rectangle="<<x*y;

void area(int x,int y,int z)

float s=(x+y+z)/2;

float ar;

ar=sqrt(s*(s-x)*(s-y)*(s-z));

cout<<"\nArea of triangle using hero's formula="<<ar;

Output:
Area of square=100

Area of rectangle=30

Area of triangle using hero's formula=6.480741

Area of triangle using hero's formula=6.480741

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>

inline int max(int x,int y)

return(x>y?x:y);

int main()

clrscr();

int m=10,n=25;

int a,b;

a=max(6,8);

cout<<"Greatest of 6 and 8="<<a;

b=max(m,n);

cout<<"\nGreatest of m="<<m<<" and n="<<n<<" is "<<b;

getch();

return 0;

Ouput:
Greatest of 6 and 8=8

Greatest of m=10 and n=25 is 25

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>

float SI(float p,float r=5.5,float t=1.0);

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

Objects And Classes


1.Program to calculate area of rectangle using objects and classes.
#include<iostream.h>

#include<conio.h>

class rectangle

private:

int a,b;

public:

void setdata(int x,int y)

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

Area of rectangle =200

2.Program to calculate the area of rectangle by defining member


function outside the class.
#include<iostream.h>

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

};

void rectangle: :setdata( int x , int y)

a=x;

b=y;

void rectangle : :area()

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

Area of rectangle = 200

3.Program to calculate area and perimeter of a circle using objects


and classes.
#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

Class circle

private:

double r;

public:

void input();

void area();

void circumference();

};

void circle : :input()

36 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cout<<”Enter radius of circles=”;

cin>>r;

void circle : :area()

double ar=3.1415*r*r;

cout<<”Area of circle =”<<setprecision(2)<<ar;

void circle : :circumference()

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

Circumference of circle = 21.99

4.Program to demonstrate an array of bank account.


#include<iostream.h>

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

cout<<"\nEnter account number,name,balance=";

cin>>acc_no>>name>>balance;

void account::display_data()

cout<<"\nAccount number=\t"<<acc_no;

cout<<"\nAccount holder name=\t"<<name;

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;

cout<<"\nEnter number of acccount holders=";

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:

Enter number of acccount holders= 2

Enter account number,name,balance= 1901 amit 500

39 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901

Enter account number,name,balance= 1902 kapil 2000

Information of account=1

Account number= 1901

Account holder name= amit

Account balance= 500

Information of account=2

Account number= 1902

Account holder name= kapil

Account balance= 2000

5.Program to demonstrate passing an object by value.


#include<iostream.h>

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

void time::add_time(time tt1,time tt2)

int min=tt1.minutes +tt2.minutes;

int hrs=min/60;

minutes=min%60;

hours=hrs+tt1.hours+tt2.hours;

void time::show_time()

cout<<"Time obtained on adding=";

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

Enter time in hours and minutes: 5 50

Time obtained on adding =11:20

6.Program to demonstrate passing of an object by reference


#include<iostream.h>

#include<conio.h>

class acc_info

private:

int acctno;

float balance;

public:

void read();

void show();

void transfer_money(acc_info &,double);

};

void acc_info::read()

cout<<"\nEnter the account number = ";

cin>>acctno;

cout<<"\nEnter the balance = ";

cin>>balance;

42 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
void acc_info::show()

cout<<"\nAccount number = "<<acctno;

cout<<"\nBalance is = "<<balance;

void acc_info::transfer_money(acc_info &acct,double amt)

balance=balance-amt;

acct.balance=acct.balance+amt;

void main()

clrscr();

acc_info acct1, acct2;

cout<<"\nEnter the account information of acct1..\n";

acct1.read();

cout<<"\nEnter the account information of acct2..\n";

acct2.read();

cout<<"\nDisplaying account information before transfer..\n";

acct1.show();

acct2.show();

double money_trans;

cout<<"\nEnter amount to transfer from acct1 to acct2 = ";

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..

Enter the account number =101

Enter the balance =5000

Enter account information of acct 2..

Enter the account number =102

Enter the balance =2000

Display accout information before transfer..

Account number is =101

Balance is =5000

Account number is =102

Balance is =2000

Enter amount to transfer from acct1 to acct2 =500

Displaying account information after transfer..

Account number is =101

Balance is =4500

Account number is=102

Balance is =2500

7.Program to show static data members.


#include<iostream.h>

44 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>

class account

private:

int acc_no; double balance;

static double rate;

public:

void read()

cout<<"\nEnter account number and balance = ";

cin>>acc_no>>balance;

void show()

cout<<"\nAccount number = "<<acc_no<<'\t';

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;

cout<<"\nEnter customer 1 information.....\n";

a1.read();

cout<<"\nEnter customer 2 information.....\n";

a2.read();

a1.qtr_rate_cal();

a2.qtr_rate_cal();

a1.show();

a2.show();

getch();

return 0;

Output:
Enter customer 1 information….

Enter account no. and balance = 201 1000

Enter customer 2 information…

Enter account no. and balance = 202 2000

Account no. =201 Interest = 0.05 Balance = 1012.5

Account no. =202 Interest = 0.05 Balance = 2025

8.Program to show static member functions.


#include<iostream.h>

46 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>

class account

private:

int acc_no; double balance;

static double rate;

public:

void read()

cout<<"\nEnter account number and balance->";

cin>>acc_no>>balance;

void show()

cout<<"\nAccount number = "<<acc_no;

cout<<"\nInterst = "<<rate;

cout<<"\nBalance = "<<balance;

void qtr_rate_cal()

double interest=(balance*rate*0.25)/100;

balance=balance+interest;

static void modify_rate(double incr)

47 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
rate=rate+incr;

cout<<"\nModified rate of interest = "<<rate;

};

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

Enter account number and balance-> 1901

2000

48 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Modified rate of interest = 0.06

Account number = 1901

Interst = 0.06

Balance = 500.075

Account number = 1901

Interst = 0.06

Balance = 2000.3

9.Program to multiply two matrices by returning an object from a


function.
#include<iostream.h>

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

cout<<"Enter order of m x n matrix=";

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;

matrix matrix::mul(matrix mm2)

if(n!=mm2.m)

cout<<"matrix multiplication not possible";

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

cout<<"On multiplication we get\n";

m3.show();

getch();

return 0;

Output:
Enter order of m x n matrix= 2 2

Enter elements : 5 6 2 1

Enter order of m x n matrix = 2 2

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

10.Program to calculate average percentage of marks in a particular


subject by n students of a class using static members.
#include<iostream.h>

#include<conio.h>

class student

private:

int rollno,marks;

static int tot_marks;

public:

void read()

cout<<"Enter rollno and marks:";

cin>>rollno>>marks;

void cal()

tot_marks+=marks;

static void avg_per_marks(int n1)

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;

cout<<"Enter numbers of student:";

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

Enter rollno and marks: 1901 77

Enter rollno and marks:1902 56

average percentage of marks is=66.5

53 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901

Constructor And Destructor


1. Program to show the use of constructor member function to initialize an
object during its creation.
#include<iostream.h>
#include<conio.h>
class rectangle
{
private:
int length,breadth;
public:
rectangle()
{
length=5;
breadth=6;
}
int area()
{
return(length*breadth);
}
};
int main()
{
clrscr();
rectangle r1;
cout<<"Area of rectangle="<<r1.area();
getch();
return 0;
}
Output:
Area of rectangle=30

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

3. Program to display the area of multiple rectangle using array of


objects in which object is initialized using constructor.
#include<iostream.h>
#include<conio.h>
class rectangle
{
private:

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

4. Program to show constructor with default arguments.


#include<iomanip.h>
#include<conio.h>
#include<iostream.h>
class complex
{
private:
double real;
double imag;
public:
complex(double=0.0,double=0.0);
void add(complex,complex);

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

5 .Program to show dynamic initialization using constructor.


#include<iostream.h>
#include<conio.h>
class height
{
private:
int feet;

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

6.Program to trace the flow of execution of destructor in a class.


#include<iostream.h>
#include<conio.h>
class counter
{
int id;
public:
counter(int i)
{
id=i;
cout<<"\nConsturctor of object with id"<<id<<"runs";
}
~counter()
{
cout<<"\nobject with id"<<id<<"destroyed";
}
};
int main()
{
clrscr();
counter c1(1);
counter c2(2);
counter c3(3);

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

8.Program to show use of constructor and destructor for


performing dynamic operations.
#include<string.h>
#include<iostream.h>
#include<conio.h>
class string
{
char *name;
int len;
public:
string(char *s)
{
len=strlen(s);
name=new char[len+1];
strcpy(name,s);
}
void compare(string &ss2)
{
int k;
k=strcmp(name,ss2.name);
if(k==0)
cout<<"\nboth object have same name";
else
cout<<"\n both objects have diffirent name";
}
void display()
{
cout<<"\nname of person="<<name;

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

9.Program to show how dynamic objects can be created and


destroyed using ‘new’ and ‘delete’ operators.
#include<iostream.h>
#include<conio.h>
class rectangle
{
private:
int l,b;
public:
rectangle()
{
cout<<"constructor with no parameter invoked";
}
void read()
{
cout<<"\nenter length and breadth";
cin>>l>>b;

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

enter length and breadth 5 7

area of rectangle=35
destroying object
destructor invoked
end of program

10.Program to create copy constructor.


#include<iostream.h>
class yyy;
class xxx
{
private:
int x;

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

11.Program to show nested classes.


#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
char name[20];
public:
class date

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

12.Program to show how a friend function acts as a bridge


between two classes.

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

cout<<"\Enter number num1=";

cin>>num1;

void base_show()

cout<<"num1="<<num1;

};

class derived_class:public base_class

66 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
private:

int num2;

public:

void derived_read()

cout<<"\nEnter number num2=";

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

Enter number num2= 20

num1=10

num2=20Enter number num1=5

num1=5

2.Program to show the use of public inheritance.


#include<iostream.h>

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

cout<<"Value of base's prive data member="<<x;

prot_base=20;

cout<<"\nValue of base's protected data member="<<prot_base;

};

int main()

clrscr();

derive d;

int y;

d.f1();

y=d.get_base_priv();

cout<<"\nValue of base private data member accessed indirectly="<<y;

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

Value of base's protected data member=20

3.Program to calculate the result of the student using private


inheritance.
#include<iostream.h>

#include<conio.h>

class stud_basic

private:

int rollno;

char name[20];

public:

void read()

cout<<"\nEnter rollno and name: ";

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

class result:private stud_basic

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

4.Program to show protected inherited members.


#include<iostream.h>

#include<conio.h>

class num

protected:

int x,y;

public:

void read()

cout<<"\nEnter two numbers=";

cin>>x>>y;

void showxy()

cout<<"x= "<<x<<"\t y= "<<y;

};

class result:public num

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

5.Program to show multilevel inheritance.


#include<iostream.h>

#include<conio.h>

class person

private:

char name[20];

long int phno;

public:

void read()

cout<<"Enter name and phno=";

cin>>name>>phno;

void show()

cout<<"\nName="<<name;

cout<<"\nPhone number="<<phno;

};

class student:public person

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

cout<<"Enter rollno and course=";

cin>>rollno>>course;

void show()

person::show();

cout<<"\nRollno="<<rollno;

cout<<"\nCourse="<<course;

};

class exam:public student

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 rollno and course=1901 bca

Enter marks: 60 71 89 78 91

Name=harkirat

Phone number=01614661232

Rollno=1901

Course=bcaMarks:60 71 89 78

Percentage=74.5

6.Program no constructor with no parameter in base class and


parametrized constructor in derived class.
#include<iostream.h>

#include<conio.h>

class base

78 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
public:

base()

{ cout<<"base's no parameter constructor"; }

};

class derv:public base

int a,b;

public:

derv(int x,int y)

a=x;

b=y;

cout<<"\nDerive's constructor with two parameter";

};

int main()

clrscr();

derv d(10,15);

getch();

return 0;

Output:
Base’s no parameter constructor

Derive’s constructor with two parameter

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;

cout<<"base's single parameter constructor";

void show()

{ cout<<"\na="<<a; }

};

class derv:public base

int b;

public:

derv(int bb,int aa):base(aa)

b=bb;

cout<<"\nDerived's two patameter constructor";

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

Derived’s two parameter constructor

a=20 b=10

8.Program on constructor with no parameter in multiple


inheritance.
#include<iostream.h>

#include<conio.h>

class base1

81 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
public:

base1()

{ cout<<"\nBase1 no parameter constructor"; }

};

class base2

public:

base2()

{ cout<<"\nBase2 no parameter constructor"; }

};

class derv:public base1,base2

public:

derv()

{ cout<<"Dev no parameter constructor"; }

};

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

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

cout<<"\n initial value of s1 object: "<<s1.show();

cout<<"\n initial value of s2 object: "<<s2.show();

++s1;

++s1;

++s2;

cout<<"\nFinal value of s1 object= "<<s1.show();

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

Initial value of s2 object=0

Final value of object s1=2

Final value of object s2=1

2.Program in which the object of operator returns a value

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

cout<<"\n initial value of s1 object: "<<s1.show();

cout<<"\n initial value of s2 object: "<<s2.show();

++s1;

s2=++s1;

cout<<"\nFinal value of s1 object= "<<s1.show();

cout<<"\nFinal value of s2 object= "<<s2.show();

getch()

Output:-

Initial value of s1 object=0

Initial value of s2 object=0

Final value of s1 object=2

Final value of s2 object=2

3.program to showing the overloading of prefix and postfix

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;

cout<<"\n initial value of s1 object: "<<s1.show();

cout<<"\n initial value of s2 object: "<<s2.show();

s2=++s1;

cout<<"\ns1= "<<s1.show();

cout<<"\ns2 after prefix operation= "<<s2.show();

s2= s1++;

cout<<"\ns1= "<<s1.show();

cout<<"\ns2 after postfix operation= "<<s2.show();

87 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
getch()

Output:-

Initial value of s1 object= 0

Initial value of s2 object=0

S1=1

S2 after prefix operation=1

S1=2

S2 after postfix operation=1

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

cout<<"\nEnter real and imaginary part: ";

cin>>real>>img;

complex operator+(complex cc2)

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

cout<<"\n Enter complex number c1: ";

c1.read();

cout<<"\n Enter complex number c2: ";

c2.read();

c3=c1+c2;

c3.show();

getch()

Output:-

Enter complex number c1:

89 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
Enter real and imaginary part: 3.1 5.2

Enter complex number

Enter real and imaginary part: 2.1 6.3

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

int dob:: int operator==(dob dd2)

{ if((yyyy=dd2.yyyy) &&(mm==dd2.mm) &&(dd=dd2.dd) )

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)

cout<<"\n"d1 and d2 are same";

else

cout<<"\nd1 and d2 are not same";

getch()

Output:-

Dob=10-12-1976

Dob=10-12-1976

D1 and d2 are same

6.program to concatenate two strings of objects by overloading the “+” operator

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

void show()::operator==(dob dd2)

{ cout<<"st; }

};

string string::operator+(string ss2)

{ 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

s1= happy s2= diwali s3=

after concatenation

s3=happy diwali

7.program to add two time objects by overloading += operator

#include<iostream.h>

#include<conio.h>

Class time

{ private:

int hours,mins,secs;

public:

time(int hh,int mm,int ss)

{ hours=hh,min=mm;secs=ss; }

void show()

{ cout<<"hours<<":"<<mins<<":"<secs; }

void operator+=(time);

};

void time::operator+=(time tt2)

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

cout<<"time t1 before operation: ";

t1.show();

t1+=t2;

cout<<"time t1 after overloading += operation = ";

t1.show();

getch();

output:-

time t1 before operation: 4:58:55

time t1 after oberloading += opertaion = 8:19:05

8.program to perform conversions from user-defined data type to basic type

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

time(int hr,int mm)

{ hours=hr,min=mm; }

void show()

{ cout<<"time in mins and hours is: "; }

opertor int()

{ int mins;

mins=hours*60+mins;

return(mins);

};

void main()

{ clrscr();

time t1(3,15);

t1.show();

minutes=t1;

cout<<"time in minutes = "<<minutes;

getch();

output:-

time in hours and minutes= 3:15

time in minutes= 195

9.program to convert the given amount in dollars (assume 1$=rs 44.20)

#include<iostream.h>

95 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
#include<conio.h>

#define dol_val 44.20

Class rupee

{ private:

double rs;

public:

rupee(double rs1)

{ rs=rs1; }

void show()

{ cout<<"money in Rs= "<<rs; }

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

{cout<<"\nmoney in dollars = "<<do1;}

};

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

10.program to understand the concept of overloading fuction call operator

#include<iostream.h>

#include<conio.h>

Class xyz

public:

int operator()(int num)

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

cout<<"\nCube of "<<a<<" is ="<<res;

getch();

output:-

cube of 5 is=125

11.program to show the overloading subscript operator[]

#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

cout<<"index out of range";

};

array::array()

for (int i=0;i<MAX_SITE;i++)

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

cout<<"displaying 3rd element =""<<t;

a1[3]=10;

cout<<Fourth element a1[3]= "<<a1[3];

a[7]=20;

getch();

output:-

displaying 3rd element= 2

fourth element a1[3]=10

index out of range

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:

virtual void display()

{ cout<<"Base cllead display function"; }

};

class derv1: public base

public:

void display()

{ cout<<"derv1's display called\n"; }

};

class derv2:public base

public:

void display()

{ cout<<"Derv2's display called\n"; }

};

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

Derv2's display called

2.Program on area of rectangle and area of triangle.


#include<iostream.h>

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

virtual void cal_area()

{ cout<<"Virtual fuction providing single interface"; }

};

class rectangle:public shape

public:

void cal_area()

double area=a*b;

cout<<"Area of rectangle="<<area;

};

class triangle:public shape

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;

cout<<"Enter length and breadth of rectangle:";

r1.read();

ptr=&r1;

ptr->cal_area();

triangle t1;

cout<<"\nEnter base and perpendicular of triangle:";

t1.read();

ptr=&t1;

ptr->cal_area();

getch();

return 0;

Output:
Enter length and breadth of rectangle: 10 20

Area of rectangle=200

Enter base and perpendicular of triangle: 5 20

Area of triangle=50

103 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901

C++ Streams And Files


1.Program to read multi-word string using get function.
#include<iostream.h>

#include<conio.h>

int main()

clrscr();

const int MAX=80;

char str[MAX];

cout<<"\nEnter a string:";

cin.get(str,MAX);

cout<<"string is="<<str;

getch();

return 0;

Output:
Enter a string: hello

string is= hello

2.Program to read multiple lines string using get function.


#include<iostream.h>

#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

buy ipci at current price*

String is =

share index is growing

buy ipci at current price

3Program to input string using ‘read’ function.


#include<iostream.h>

#include<conio.h>

int main()

clrscr();

const int MAx=11;

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

String is:stock index

4.Program to count the number of characters inputted by the user.


#include<iostream.h>

#include<conio.h>

int main()

clrscr();

char str[20];

cout<<"\Enter a string:";

cin.get(str,20);

int count=cin.gcount();

cout<<"Number of character extracted are="<<count;

getch();

return 0;

Output:

Enter a string:stock

Number of character extracted are=6

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;

cout<<”Enter your first and last name”;

fname=cin.get();

cin.ignore(80,’ ‘);

lname=cin.get();

cout<<”Your initials are:”<<fname<<lname;

getch();

return 0;

Output:
Enter first and last name=harkirat kalsi

Your initials ar: hk

6.Program to show the use of peek().


#include<iostream.h>

#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

Enter a text:aaa bb ccc

abc

^Z

7.Program to show the use of putback(ch).


#include<iostream.h>

#include<conio.h>

#include<ctype.h>

clrscr()

char ch;

cout<<”Press ctrl+z or F6 to exit\n”;

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

8.Program to demonstrate use of write() function.


#include<iostream.h>

#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

9Program to show the use of fill(ch)


#include<iostream.h>

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

10.Program to show the use of prwcision(p).


#include<iomanip.h>

#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

11.Program to show the use of setf() and unsetf().


#include<iomanip.h>

#include<iostream.h>

int main()

cout.width(40);

cout.setf(ios::left);

cout<<"Text is left just justified"<<endl;

cout.unsetf(ios::left);

cout.width(40);

cout.setf(ios::right);

cout<<"Text is right justified";

return 0;

Output:
Text is left justified Text is right justified.

12.Program to work with single file.


#include<iostream.h>

#include<fstream.h>

int main()

ofstream outf("ITEM");

cout<<"\nEnter item name:";

char name[30];

112 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
cin>>name;

outf<<name<<"\n";

cout<<"Enter item cost";

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

Enter item cost:2500

Item name:CD-ROM

Item cost:2500

2.Program on working with multiple files.


#include<iostream.h>

#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 States of America\n";

fout<<"United Kingdom\n";

fout<<"South Korea\n";

fout.close();

fout.open("capital");

fout<<"Washington\n";

fout<<"London\n";

fout<<"Seoul\n";

fout.close();

const int n=80;

char line[n];

ifstream fin;

fin.open("country");

cout<<"contents of country file\n";

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:

Contents of country file

United States of America

United Kingdom

South Korea

Contents of capital file

Washington

London

Seoul

3.Program to read from two files.

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

cout<<"Exit from country\n";

exit(1);

fin1.getline(line,SIZE);

cout<<"Capital of"<<line;

if(fin2.eof()!=0)

cout<<"Exit from capital\n";

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

Capital of United Kingdom

London

Capital of South Korea

Seoul

4.Program to perfrom input output operations on characters.

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

5.Program on input output Operation on binary files

#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.write((char*) & height,sizeof(height));

outfile.close();

for(int i=0;i<4;i++)

height[i]=0;

ifstream infile;

infile.open(filename);

infile.read((char *) & height,sizeof(height));

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:

175.50 153.00 167.25

6.Program to read and write class objects.

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

cout<<"Enter details for three items\n";

for(int i=0;i<3;i++)

item[i].readdata();

file.write((char *) & item[i],sizeof(item[i]));

file.seekg(0);

cout<<"\nOutput\n\n";

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

file.read((char*) & item[i],sizeof(item[i]));

item[i].writedata();

120 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
file.close();

return 0;

Output:

ENTER DETAILS FOR THREE ITEMS

Enter name: C++

Enter code: 101

Enter cost: 175

Enter name: Fortran

Enter code:102

Enter cost:150

Enter name:Java

Enter code:103

Enter cost:225

Output

C++ 101 175

Fortran 102 150

Java 103 225

7.Program on file updating and random access.

#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.open("STOCK.DAT",ios:: ate | ios:: in | ios:: out | ios::bimary);

inoutfile.seekg(0,ios::beg);

cout<<"current contents of stock"<<"\n";

while(inoutfile.read((char 8) & item,sizeof item))

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

inoutfill.write((char*) & item,sizeof item);

intoutfile.seekg(0);

cout<<"CONTENTS OF APPENDED FILE\n";

while(inout.read((char *) &item,sizeof item))

item.putdata();

int last=inoutfile.tellg();

int n=last/sizeof(item);

cout<<"Number of objects="<<n<<"\n";

cout<<"Total bytes in the file="<<last<<"\n";

cout<<"Enter object number to be updated\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);

cout<<"Enter new values of the object\n";

item.getdata();

cin.get(ch);

inoutfile.write((char*) & item,sizeof item)<<flush;

inoutfile.seekg(0);

cout<<"CONTENTS OF UPDATED FILE\n";

while(inoutfile.read((char *) & item,sizeof item))

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;

cout<<"Enter numerator (x) and denominator (y)=";

cin>>x>>y;

try

if(y=0)

throw 10;

cout<<"x/y="<<x/y;

catch (int i)

cout<<"Exception : Division by 0 is not allowed ";

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;

cout<<"Enter a number: ";

cin>>num;

try

if(num>oxffff)

throw 10;

if(num<0);

throw 'E';

cout<<"Square root of "<<num<<"is="<<sqrt(num);

catch(int)

cout<<"Exception : out of range\n";

catch(char)

126 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
{

cout<<"Exception :Square root of negative number doesn't exist";

return 0;

3. Program to check whether a person is eligible for voting or not

#include<iostream.h>

#include<conio.h>

int main()

int age;

cout<<"Enter age for voting(18 to 120): ";

cin>>age;

try

if(age>0 $$age<18)

throw 0;

else if(age>120)

throw 'v';

else if(age<0)

throw 2.8;

cout<<"Eligible for voting";

}
127 | P a g e
Problem Solving Through ‘C++’ Harkirat Kalsi
Roll no.1901
catch(int i)

cout<<"Exception: Valid age but not eligible for voting";

catch(…)

cout<<"Exception : Invalid age for voting";

4. Program to demonstrate exception specification

#include<iostream.h>

#include<conio.h>

void funct_test(int val) throw(int,char)

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)

(cout<<"int-type exception catched";)

catch(char)

{cout<<"char-type exception catched";)

return 0;

129 | P a g e

Das könnte Ihnen auch gefallen