0% found this document useful (0 votes)
6 views19 pages

Gauri CPP

The document contains multiple C++ programs demonstrating various concepts such as constructors, destructors, operator overloading, inheritance, and exception handling. Each program includes code snippets along with expected outputs, showcasing functionalities like distance calculation, string manipulation, and handling of bank deposits. The examples illustrate both single and multiple inheritance, as well as the use of virtual functions and exception handling mechanisms.

Uploaded by

gaurisawale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views19 pages

Gauri CPP

The document contains multiple C++ programs demonstrating various concepts such as constructors, destructors, operator overloading, inheritance, and exception handling. Each program includes code snippets along with expected outputs, showcasing functionalities like distance calculation, string manipulation, and handling of bank deposits. The examples illustrate both single and multiple inheritance, as well as the use of virtual functions and exception handling mechanisms.

Uploaded by

gaurisawale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1) //program in cpp to demonstrate the use of constructure

#include<iostream.h>
#include<conio.h>
class data
{
int acc_no;
float balance;
public:
data(int a, float b)
{
acc_no=a;
balance=b;
}
void display()
{
cout<<"\naccount number :"<<acc_no;
cout<<"\nBalance :"<<balance;
}
};
void main()
{
clrscr();
data d1(15,17500);
d1.display();
data d2(321,57500);
d2.display();
getch();
}

/* OUTPUT

account number :15


Balance :17500
account number :321
Balance :57500
*/
2)//Program to demonstrate the use of constructor and destructor

#include<iostream.h>
#include<conio.h>
class alpha
{
public:
static int count;
alpha()
{
count++;
cout<<"\nNumber of object created=>"<<count;
}
~alpha()
{
cout<<"\nNumber of object destroyed=>"<<count;
count--;
}
};
int alpha:: count;
void main()
{
clrscr();
cout<<"\n in main block";
alpha A1,A2,A3;
{
cout<<"\n in sub block";
alpha A4,A5,A6;
}
cout<<"out of sub block";
getch();
}

/*OUTPUT

in main block
Number of object created=>1
Number of object created=>2
Number of object created=>3
in sub block
Number of object created=>4
Number of object created=>5
Number of object created=>6
Number of object destroyed=>6
Number of object destroyed=>5
Number of object destroyed=>4out of sub block
Number of object destroyed=>3
Number of object destroyed=>2
Number of object destroyed=>1 */
3)//program to accept distance between 1st and 2nd city and 3rd city
// To calculate the distance between !st and 3rd city
#include<iostream.h>
#include<conio.h>
class road
{
int km,m;
public:
void getdata()
{
cout<<"enter distance in kilometer-=>";
cin>>km;
cout<<"enter distance in meter";
cin>>m;
}
void calculate(road d1, road d2)
{
road d3;
d3.km=d1.km+d2.km;
d3.m=d1.m+d2.m;
if(d3.m>=1000)
{
d3.km=d3.km+1;
d3.m=d3.m-1000;
}
cout<<d3.km<<". "<<d3.m<<"km";
}
};
void main()
{
clrscr();
road d1,d2,d3;
cout<<"enter distance between 1sr and 2nd city :\n";
d1.getdata();
cout<<"enter distance between 2nd and 3rd city :\n";
d2.getdata();
cout<<"distance between 1st and 2nd city :\n";
d3.calculate(d1,d2);
getch();
}

/*OUTPUT
enter distance between 1sr and 2nd city :
enter distance in kilometer-=>155
enter distance in meter400
enter distance between 2nd and 3rd city :
enter distance in kilometer-=>35
enter distance in meter700
distance between 1st and 2nd city :
191. 100km
*/
4(A)/* program 4(i)
Demonstrate the use of operator overloading string manipulation:realational operator
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char str[20];
public:
void read_string()
{
cout<<"\n Input any string=>";
cin>>str;
}
void print_string()
{
cout<<"\nString is=>"<<str;
}
int operator==(string s)
{
if(strcmp(str,s.str)==0)
return 1;
else
return 0;
}
};
void main()
{
clrscr();
string s1,s2;
s1.read_string();
s2.read_string();
if(s1==s2)
cout<<"\nstrings are equal";
else
cout<<"\nstrings are not equal";
//return 0;
getch();
}

/*OUTPUT

Input any string=>Sameer

Input any string=>sameer

strings are not equal


*/
4(B)//program 4(ii)
//Demonstrate the use of operator overloading:concatenation
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct string
{
char str[20];
};
string operator+(string,string);
void main()
{
clrscr();
string str1,str2,str3;
cout<<"Input first string=>";
cin>>str1.str;
cout<<"Input second string=>";
cin>>str2.str;
str3=str1+str2;
cout<<"\n\n\nFirst string=>"<<str1.str;
cout<<"\nsecond string=>"<<str2.str;
cout<<"\n\nAfter concatenation string=>"<<str3.str;
getch();
}
string operator+(string s1,string s2)
{
string s3;
strcpy(s3.str,s1.str);
strcat(s3.str,s2.str);
return s3;
}

/*OUTPUT
Input first string=>Shivaji
Input second string=>Science

First string=>Shivaji
second string=>Science

After concatenation string=>ShivajiScience


*/
5)// in a bank N depositer deposit the amount . Write the program to find total amount
#include<iostream.h>
#include<conio.h>
class deposit
{
int rupee;
int paise;
public:
deposit()
{
rupee=0;
paise=0;
}
void getdata()
{
cout<<"\nEnter rupee=";
cin>>rupee;
cout<<"\nEnter paise=";
cin>>paise;
}
void putdata()
{
if(paise>=100)
{
rupee++;
paise=-(100-paise);
}
cout<<"Rs"<<rupee<<"."<<paise;
}
deposit operator+(deposit d)
{
deposit t;
int a=0;
t.rupee=rupee+d.rupee;
t.paise=paise+d.paise;
if(t.paise>=100)
{
a=paise/100;
t.rupee=t.rupee+a;
t.paise=t.paise-a*100;
}
return t;
}
};
void main()
{
int i,n;
clrscr();
deposit D[15], total;
cout<<"\nenter total number of depositer:";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"\nEnter the depositer amountfor"<<i+1<<"Depositer";
D[i].getdata();
}
for(i=0; i<n; i++)
{
total=total+D[i];
}
cout<<"\nAmount with depositer:";
for(i=0; i<n; i++)
{
cout<<"\n Depositer amount for"<<i+1<<"Depositer:";
D[i].putdata();
}
cout<<"Total amount deposited in the bank=>";
total.putdata();
getch();
}

/*OUTPUT

enter total number of depositer:3

Enter the depositer amount for 1Depositer


Enter rupee=150

Enter paise=110

Enter the depositer amount for 2Depositer


Enter rupee=50

Enter paise=80

Enter the depositer amount for 3Depositer


Enter rupee=350

Enter paise=95

Amount with depositer:


Depositer amount for1Depositer:Rs151.10
Depositer amount for2Depositer:Rs50.80
Depositer amount for3Depositer:Rs350.95
Total amount deposited in the bank=>Rs552.85
*/
6)/* Declare classs event and accept time of first event and second event
and find
i)Use operator *-* overloading
*/
#include<iostream.h>
#include<conio.h>
class event
{
private:
long int sec,min,hr,total;
public:
event()
{
sec=0;
min=0;
hr=0;
}
void getdata();
void display();
event operator-(event e);
};
void event::getdata()
{
cout<<"\nEnter the time of event in hours,minute&sec :";
cin>>hr>>min>>sec;
total=(3600*hr)+(60*min)+sec;
}
void event::display()
{
cout<<"\nHours ="<<hr;
cout<<"\nMinutes ="<<min;
cout<<"\nSecond ="<<sec;
}
event event::operator-(event e)
{
event t;
t.total=e.total-total;
t.hr=t.total/3600;
t.min=(t.total%3600)/60;
t.sec=(t.total%3600)%60;
return(t);
}
void main()
{
clrscr();
event e1,e2,e3;
e1.getdata();
e2.getdata();
cout<<"\nFirst event=";
e1.display();
cout<<"\nSecond event=";
e2.display();
e3=e2-e1;
cout<<"\n DIfference between 1st and 2nd event=";
e3.display();
getch();
}
/*OUTPUT

Enter the time of event in hours,minute&sec :2 30 40

Enter the time of event in hours,minute&sec :1 20 30

First event=
Hours =2
Minutes =30
Second =40
Second event=
Hours =1
Minutes =20
Second =30
DIfference between 1st and 2nd event=
Hours =1
Minutes =10
Second =10
*/
7)/* Program to demonstrate the use of single inheritance
class B,base class,class D,derived class
*/
#include<iostream.h>
#include<conio.h>
class B
{
int a;
public:
int b;
void get_ab()
{
cout<<"\nEnter the value of A and B=>";
cin>>a>>b;
}
int get_a()
{
return a;
}
void show_a()
{
cout<<"\nA=>"<<a;
}
};
class D:public B
{
int c;
public:
void mul()
{
c=b*get_a();
}
void display()
{
cout<<"\nA=>"<<get_a();
cout<<"\nB=>"<<b;
cout<<"\nMultiplication is =>"<<c;
}
};
void main()
{
clrscr();
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
getch();
}
/* OUTPUT
Enter the value of A and B=>5 10

A=>5
A=>5
B=>10
Multiplication is =>50
A=>5
B=>20
Multiplication is =>100
*/
8)/*program to demonstrate use of multiple inheritance declare class M & N and
derive publically class P from M & N
*/
#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get_m()
{
cout<<"\n enter value of m:";
cin>>m;
}
};
class N
{
protected:
int n;
public:
void get_n()
{
cout<<"\n enter value of n:";
cin>>n;
}
};
class P:public M, public N
{
public:
int c;
void display()
{
c=m*n;
cout<<"\n m="<<m;
cout<<"\n n="<<n;
cout<<"\n Multiplication is="<<c;
}
};
void main()
{
clrscr();
P z;
z.get_m();
z.get_n();
z.display();
getch();
}

/*OUTPUT
enter value of m:5
enter value of n:10
m=5
n=10
Multiplication is=50
*/
9)//Program to demonstrate the use of multilevel inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
long roll_number;
public:
void get_number()
{
cout<<"\nEnter the roll number of student:";
cin>>roll_number;
}
void put_number()
{
cout<<"\nRoll No.:"<<roll_number;
}
};
class test:public student
{
protected:
long sub1,sub2;
public:
void get_marks()
{
cout<<"\nEnter the marks os sub1 & sub2=";
cin>>sub1>>sub2;
}
void put_marks()
{
cout<<"\nSub1="<<sub1<<"\nSub2="<<sub2;
}
};
class result:public test
{
long total;
public:
void display()
{
put_number();
put_marks();
total=sub1+sub2;
cout<<"\nTotal marks="<<total;
}
};
void main()
{
clrscr();
result r;
r.get_number();
r.get_marks();
r.display();
getch();
}
/*OUTPUT

Enter the roll number of student:735

Enter the marks os sub1 & sub2=47 48

Roll No.:735
Sub1=47
Sub2=48
Total marks=95
*/
10)//Program to demonstrate the use of heirarchical inheritance
#include<iostream.h>
#include< conio.h>
class side
{
protected:
int L;
public:
int a;
void set_values()
{
cout<<"\n\nEnter the value of a=";
cin>>a;
}
};
class square: public side
{
public:
void sq()
{
int sq=a*a;
cout<<"\nSquare="<<sq;
}
};
class cube:public side
{
public:
void cub()
{
int cub=a*a*a;
cout<<"\nCube="<<cub;
}
};
void main()
{
clrscr();
square s;
s.set_values();
s.sq();
cube c;
c.set_values();
c.cub();
getch();
}

/*OUTPUT

Enter the value of a= 12


Square=144

Enter the value of a=5


Cube=125
*/
11)//Program to demonstrate the use of normal virtual function and pure virtual function with abstract class
#include<iostream.h>
#include<conio.h>
class test
{
public:
virtual void display()=0;//pure virtual function
virtual void show() {};//empty virtual fumction
};
class derived:public test
{
public:
void show()
{
cout<<"\nI am in test class:"<<endl;
}
void display()
{
cout<<"\nThis is pure class virtual function"<<endl;
}
};
void main()
{
test*t;
derived d;
clrscr();
t=&d;
t->show();
t->display();
getch();
}

/*OUTPUT

I am in test class:

This is pure class virtual function


*/
12)//program to determine whether the input is +ve or -ve througth exception
#include<iostream.h>
#include<conio.h>

class test
{
public:
int n,y;
char x;
void insert ()
{
cout<<"Enter the number :";
cin>>n;
}
void test_no()
{
if((n>0)
{
throw"x";
}
else
{
y=n;
throw(y);
}
}
};
void main()
{
test t1;
try
{
t1.insert();
t1.test_no();
}
catch(char x1)
{
cout<<"\nThe no is positive";
}
catch(int y1)
{
cout<<"\nThe no is negative";
}
getch();
}

/*OUTPUT
Enter the number:25
The no is positive

Enter the number:-25


The no is negative
*/
13)//program to aise exception if an attempt divided by zero
#include<iostream.h>
#include<conio.h>
class exceptiontest
{
public:
float a,b;
float c;
char x;
void insert()
{
cout<<"\nEnter value of a:";
cin>>a;
cout<<"\nEnter value of b:";
cin>>b;
}
void divided()
{
try
{
c=a/b;
if(b==0)
{
throw b;
}
else
{
cout<<"\nanswer is:"<<c;
}
}
catch(float y1)
{
cout<"\nDivided by zero exception is there\n";
}
}
};
void main()
{
exceptiontest t;
t.insert();
t.divided();
getch();
}

/*OUTPUT
Enter value of a:100
Enter value of b:20
answer is:5

Enter value of a:50


Enter value of b:0
Divided by zero exception is there
*/

You might also like