0% found this document useful (0 votes)
89 views29 pages

EXCEPTIOANL HANDLING / Program Code:: Sample Input and Output

The document contains code snippets and sample outputs for various C++ programs involving object-oriented concepts like inheritance, operator overloading, exception handling, templates etc. These include programs for bank account details using inheritance, complex number arithmetic using operator overloading, quicksort using templates, string manipulation functions etc.

Uploaded by

Selva Ganesh
Copyright
© Attribution Non-Commercial (BY-NC)
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)
89 views29 pages

EXCEPTIOANL HANDLING / Program Code:: Sample Input and Output

The document contains code snippets and sample outputs for various C++ programs involving object-oriented concepts like inheritance, operator overloading, exception handling, templates etc. These include programs for bank account details using inheritance, complex number arithmetic using operator overloading, quicksort using templates, string manipulation functions etc.

Uploaded by

Selva Ganesh
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 29

/****EXCEPTIOANL HANDLING****/ PROGRAM CODE:

#include <iostream> using namespace std; int main() { int a,b; cout<<"Enter value of a and b:"; cin>>a>>b; int x=a-b; try { if(x!=0) { cout<<"Result(a/x)="<<a/x<<endl; } else { throw(x); } } catch(int x) { cout<<"Exception caught x="<<x<<endl; } cout<<"END"; return 0; }

SAMPLE INPUT AND OUTPUT

Enter the value of a and b: 20 15 Result(a/x)=4 End Enter the value of a and b: 10 10 Exception caught:X=0 End

/****BANK DEITALS USING SINGLE INHERITANCE****/ PROGRAM CODE: #include <iostream> #include <cstdlib> using namespace std; class account { public: char name[10],add[100]; int no; void getdata() { cout<<"\nEnter name:"; cin>>name; cout<<"\nEnter account no:"; cin>>no; cout<<"\nEnter address:"; cin>>add; } void display() { cout<<"\n Name:"<<name; cout<<"\nAccount number:"<<no; cout<<"\nAddress:"<<add; }}; class saving_acct:public account { private: double balance; public: void getdata() { account::getdata(); cout<<"\nEnter the initial amount"; cin>>balance; } void deposit() { double amount; cout<<"\nEnter the amount to be deposited:"; cin>>amount; balance+=amount; } void withdraw() { double amount; cout<<"\nEnter the amount to be withdrawn:"; cin>>amount; balance-=amount; } int display()

{ account::display(); cout<<"\nBalance:"<<balance; } }; int main() { int n,i=-1,amo; saving_acct sbi[10]; char ch; do { cout<<"\nRead the following instructions"; cout<<"\n1.Create an account"; cout<<"\n2.Deposit an amount"; cout<<"\n3.Withdraw an amount"; cout<<"\n4.Display details"; cout<<"\n5.EXIT "; cout<<"\nEnter your choice:"; cin>>n; switch(n) { case 1: i++; sbi[i].getdata(); sbi[i].deposit(); break; case 2: sbi[i].deposit(); break; case 3: sbi[i].withdraw(); break; case 4: sbi[i].display(); break; case 5: exit(0); default: cout<<"\n Enter the correct choice "; } cout<<"\nRepeat the preocess(y/n)"; cin>>ch; } while(ch='y'); return 0; }

SAMPLE INPUT AND OUTPUT:


Read the following instructions 1.Create an account 2.Deposit an amount 3.Withdraw an amount 4.Display details 5.EXIT Enter your choice:1 Enter name:SELVAGANESH Enter account no:6211 Enter address:PUDUCHERRY Enter the initial amount500 Enter the amount to be deposited:1500 Repeat the preocess(y/n)Y Read the following instructions 1.Create an account 2.Deposit an amount 3.Withdraw an amount 4.Display details 5.EXIT Enter your choice:3 Enter the amount to be withdrawn:1000 Repeat the preocess(y/n)Y Read the following instructions 1.Create an account 2.Deposit an amount 3.Withdraw an amount 4.Display details 5.EXIT Enter your choice:4 Name:SELVAGANESH Account number:6211 Address:PUDUCHERRY Balance:1000 Repeat the preocess(y/n)Y Read the following instructions 1.Create an account 2.Deposit an amount 3.Withdraw an amount 4.Display details 5.EXIT Enter your choice:4 Name:SELVAGANESH Account number:6211 Address:PUDUCHERRY Balance:1000 Repeat the preocess(y/n)Y Read the following instructions 1.Create an account 2.Deposit an amount 3.Withdraw an amount 4.Display details 5.EXIT Enter your choice:5

/****QUICK SORT USING TEMPLATES****/ PROGRAM CODE: #include <iostream> using namespace std; template<class T> void print(T *a,int n) { cout<<a[0]; } template<class T> void quick(T *a,int first,int last) { int i,j,pivot; if(first<last) { pivot=a[first]; i=first; j=last; while(i<j) { while(a[i]<=pivot&&i<last) i++; while(a[j]>=pivot&&j>first) j--; if(i<j) swap(a,i,j); } swap(a,first,j); quick(a,first,j-1); quick(a,j+1,last); } } template<class T> void swap(T *a,int i,int j) { T temp=a[i]; a[i]=a[j]; a[j]=temp; } int main(){ int a[100]; int n; cout<<"\n Number of elements "; cin>>n; cout<<"\n Enter the numbers to be Sort"; for(int i=0;i<n;i++) cin>>a[i]; cout<<"\n Before Sorting\n"; for(int i=0;i<n;i++)

cout<<a[i]<<"\n"; quick(a,0,n-1); cout<<"\n After Sorting\n"; for(int i=0;i<n;i++) cout<<a[i]<<"\n"; char ch[100]; cout<<"\n Enter characters to be sort "; for(int i=0;i<n;i++) cin>>ch[i]; cout<<"\n Before Sorting\n"; for(int i=0;i<n;i++) cout<<ch[i]<<"\n"; quick(ch,0,n-1); cout<<"\n After Sorting\n"; for(int i=0;i<n;i++) cout<<ch[i]<<"\n"; return 0; }

SAMPLE INPUT AND OUTPUT: Number of elements :- 5 Enter the numbers to be Sort :9 87 5 6 2 Before Sorting :9 87 5 6 2 After Sorting :2 5 6 9 87 Enter characters to be sort :S E L V A Before Sorting :S E L V A After Sorting :A E L S V

/****COMPLEX ARITHEMETIC OPERATION USING OPERATOR OVERLOADING****/ PROGRAM CODE: #include <iostream> using namespace std; class complex { float x,y; public: complex(){} complex operator+(complex); complex operator-(complex); complex operator*(complex); complex(float real,float image) { x=real; y=image; } void display(); }; complex complex::operator+(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return temp; } complex complex::operator-(complex c) { complex temp; temp.x=x-c.x; temp.y=y-c.y; return temp; } complex complex::operator*(complex c) { complex temp; temp.x=((x*(c.x))-(y*(c.y))); temp.y=((x*(c.y))+(y*(c.x))); return temp; } void complex::display() { cout<<"("<<x<<")+j("<<y<<")"; }

int main() { complex c1,c2,c3; float a,b,c,d; cout<<"\nEnter the complex no1:"; cin>>a>>b; cout<<"\nEnter the complex no2:"; cin>>c>>d; c1=complex(a,b); c2=complex(c,d); cout<<"\nComplex no1:"; c1.display(); cout<<"\nComplex no2:"; c2.display(); cout<<"\nComplex no 1 and no 2 addition result:"; c3=c1+c2; c3.display(); cout<<"\nComplex no 1 and no 2 subtraction result:"; c3=c1-c2; c3.display(); cout<<"\nComplex no1 and no2 multiplication result:"; c3=c1*c2; c3.display(); return 0; }

SAMPLE INPUT AND OUTPUT: Enter the complex no1:6 3 Enter the complex no2:3 2 Complex no1:(6)+j(3) Complex no2:(3)+j(2) Complex no 1 and no 2 addition result:(9)+j(5) Complex no 1 and no 2 subtraction result:(3)+j(1) Complex no1 and no2 multiplication result:(12)+j(21) .

/****STUDENT DETAILS USING INHERITANCE****/ PROGRAM CODE: #include<iostream.h> using namespace std; class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"; cin>>m1>>m2; } }; class sports { protected: int sm; // sm = Sports mark public: void getsm() { cout<<"\nEnter the sports mark :"; cin>>sm; } }; class statement:public student,public sports { int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal cout<<"\n\tAverage : "<<avg; } }; int main() { statement obj; obj.get(); obj.getsm(); obj.display(); return 0; }

: "<<tot;

SAMPLE INPUT AND OUTPUT: Enter the Roll no : 6211 Enter the two marks : 99 96 Enter the sports mark : 72

Roll No : 6211 Total : 267 Average : 89

/****SHOOPING LIST****/ PROGRAM CODE: #include<iostream> using namespace std; class item { char name[20][20]; int codeno[20],i,n; float price[15],amount[15],total; int quantity[100]; public: void read(); void cal(); void print(); }; void item::read() { cout<<"\n Enter the No. of items "; cin>>n; for(i=1;i<=n;i++) { cout<<"\n Enter the CodeNo.:"; cin>>codeno[i]; cout<<"\n Enter the Name "; cin>>name[i]; cout<<"\n Enter the price"; cin>>price[i]; cout<<"\n Enter the Quantity "; cin>>quantity[i]; } } void item::cal() { total=0; for(i=1;i<=n;i++) { amount[i]=(price[i]*quantity[i]); total+=amount[i]; } } void item::print() { cout<<"\n SHOOPING LIST "; cout<<"\n"; cout<<"Code No."<<"\tName:"<<"\t\tPrice :"<<"\t\tQuantity:"<<"\tAmount:\n"; for(i=1;i<=n;i++) { cout<<codeno[i]<<"\t\t"<<name[i]<<"\t\t"<<price[i]<<"\t"<<quantity[i]<<"\t\t"<<amount[i]; cout<<"\n"; } cout<<"\n TOTAL ="<<total; }

int main() { item s; s.read(); s.cal(); s.print(); return 0; } SAMPLE INPUT AND OUTPUT: Enter the No. of items 4 Enter the CodeNo.:1001 Enter the Name c++ Enter the price 175 Enter the Quantity 6 Enter the CodeNo.:1002 Enter the Name java Enter the price425 Enter the Quantity 6 Enter the CodeNo.:1003 Enter the Name alc Enter the price146 Enter the Quantity 2 Enter the CodeNo.:1004 Enter the Name coa Enter the price123 Enter the Quantity 9 SHOOPING LIST Code No. Name: 1001 c++ 1002 java 1003 alc 1004 coa TOTAL =4999

Price : 175 425 146 123

Quantity: 6 6 2 9

Amount: 1050 2550 292 1107

/****STRING MANIPULATION USING OPERATOR OVERLOADING****/ PROGRAM CODE: #include <iostream> using namespace std; int main() { int x; char ch; string s1,s2; do { cout<<"\n STRING MANIPULATION "; cout<<"\n 1-> STRING COPY\n2->FINDING SUB STRING\n3->SWAPPING\n4->PRINTING INDIDUAL CHARACHTERS\n5->CONCATENATION OF TWO STRINGS"; cout<<"\n Enter Your Choice: "; cin>>x; switch(x) { case 1: { cout<<"\n Enter a String S1 :"; cin>>s1; s2=s1; cout<<"\n Now s2= "<<s2; break; } case 2: { cout<<"\n Enter the string "; cin>>s1; cout<<"\n Enter sub-string to be found\n"; cin>>s2; int x=s1.find(s2); cout<<"\n Found at :"<<x; break; } case 3: { cout<<"\n Swapping of two strings "; cout<<"\n Enter the two Strings "; cin>>s1; cin>>s2; s1.swap(s2); cout<<"\n"<<s1<<"\n"<<s2; break; } case 4: { cout<<"\n Enter the string :"; cin>>s1;

for(int i=0;i<s1.length();i++) cout<<"\n"<<s1.at(i); break; } case 5: { cout<<"\n Enter the first String"; cin>>s1; cout<<"\n Enter the Second String"; cin>>s2; s1+=s2; cout<<"\n Concatented String is :\n"; cout<<s1<<"\n"; break; } default : { cout<<"\n Enter the Correct choice"; break; } } cout<<"\n DO YOU WANT TO CONTINUE ???? (y/n)"; cin>>ch; } while(ch=='y'||ch=='Y'); return 0; }

SAMPLE INPUT AND OUTPUT:

STRING MANIPULATION 1-> STRING COPY 2->FINDING SUB STRING 3->SWAPPING 4->PRINTING INDIDUAL CHARACHTERS 5->CONCATENATION OF TWO STRINGS Enter Your Choice: 1 Enter a String S1 :SELVAGANESH Now s2= SELVAGANESH DO YOU WANT TO CONTINUE ???? (y/n)Y STRING MANIPULATION 1-> STRING COPY 2->FINDING SUB STRING 3->SWAPPING 4->PRINTING INDIDUAL CHARACHTERS

5->CONCATENATION OF TWO STRINGS Enter Your Choice: 2 Enter the string SELVAGANESH Enter sub-string to be found GANESH Found at :5 DO YOU WANT TO CONTINUE ???? (y/n)Y STRING MANIPULATION 1-> STRING COPY 2->FINDING SUB STRING 3->SWAPPING 4->PRINTING INDIDUAL CHARACHTERS 5->CONCATENATION OF TWO STRINGS Enter Your Choice: 3 Swapping of two strings Enter the two Strings SELVA GANESH GANESH SELVA DO YOU WANT TO CONTINUE ???? (y/n)Y STRING MANIPULATION 1-> STRING COPY 2->FINDING SUB STRING 3->SWAPPING 4->PRINTING INDIDUAL CHARACHTERS 5->CONCATENATION OF TWO STRINGS Enter Your Choice: 4 Enter the string :SARATH S A R A T H DO YOU WANT TO CONTINUE ???? (y/n)Y STRING MANIPULATION 1-> STRING COPY 2->FINDING SUB STRING 3->SWAPPING 4->PRINTING INDIDUAL CHARACHTERS 5->CONCATENATION OF TWO STRINGS Enter Your Choice: 5 Enter the first String SARATH Enter the Second StringKUMAR Concatented String is : SARATHKUMAR DO YOU WANT TO CONTINUE ???? (y/n)N

/****SWAPING OF TWO NUMBERS USING FUNCTION OVERLOADING****/ PROGRAM CODE: #include <iostream> using namespace std; class swap1 { public: void interchange(int a,int b) { int t; t=a; a=b; b=t; cout<<"\nthe value of x:"<<a; cout<<"\nthe value of y:"<<b; } void interchange(float c,float d) { float s; s=c; c=d; d=s; cout<<"\nThe value of m="<<c; cout<<"\nThe value of n="<<d; cout<<endl; } }; int main() { int x,y; float m,n; swap1 r; cout<<"\nEnter the value of x of integer type:"; cin>>x; cout<<"\nEnter the value of y of integer type:"; cin>>y; cout<<"\nenter the value of m of float type:"; cin>>m; cout<<"\nenter the value of n of float type:"; cin>>n; r.interchange(x,y); r.interchange(m,n); return 0; }

SAMPLE INPUT AND OUTPUT: Enter the value of x of integer type:6 Enter the value of y of integer type:9 enter the value of m of float type:6.6 enter the value of n of float type:9.9 the value of x:9 the value of y:6 The value of m=9.9 The value of n=6.6

/****SUM OF TWO NUMBERS USING CONSTRUCTOR****/ PROGRAM CODE: #include <iostream> using namespace std; class details { private: int a,b,c; public: details() { cout<<"\nEnter the value of a:"; cin>>a; cout<<"\nEnter the value of b:"; cin>>b; } void add() { c=a+b; cout<<"\nThe sum is:"<<c; } }; int main() { details od; od.add(); return 0; }

SAMPLE INPUT AND OUTPUT: Enter the value of a:62 Enter the value of b:11 The sum is:73

/****GREATEST OF THREE NOS USING FRIEND FUNCTION****/ PROGRAM CODE: #include <iostream> using namespace std; class third; class second; class first { float x; public: void getdata() { cout<<"\n Enter The first Value "; cin>>x; } friend void greatest(first,second,third); }; class second { float y; public: void getdata() { cout<<"\n Enter the second value"; cin>>y; } friend void greatest(first,second,third); }; class third { float z; public: void getdata() { cout<<"\n Enter the Third value "; cin>>z; } friend void greatest(first,second,third); }; void greatest(first a,second b,third c) { if((a.x>b.y)&&(a.x>c.z)) cout<<"\n Greatest Number ="<<a.x; else if((b.y>a.x)&&(b.y>c.z)) cout<<"\n Greatest Number ="<<b.y; else cout<<"\n Greatest Number ="<<c.z;

} int main() { first s1; second s2; third s3; s1.getdata(); s2.getdata(); s3.getdata(); greatest(s1,s2,s3); return 0; }

SAMPLE INPUT AND OUTPUT: Enter The first Value 5 Enter the second value9 Enter the Third value 10 Greatest Number =10

/****FACTORIAL OF NUMBER USING CLASS &OBJECTS****/ PROGRAM CODE: #include <iostream> using namespace std; class fact { private: int i,n,f; public: void get() { cout<<"\n Enter the value "; cin>>n; } void cal() { f=1; for(i=1;i<=n;i++) f=f*i; } void put() { cout<<"\n Factorial of "<<n<<" is "<< f; cout<<"\n"; } }; int main() { fact num; num.get(); num.cal(); num.put(); return 0; } SAMPLE INPUT AND OUTPUT:

Enter the value 6 Factorial of 6 is 720

/****FIBONACCI SERIES USING CLASSES AND OBJECTS****/ PROGRAM CODE: #include <iostream> #include <cstdlib> using namespace std; class fibo { private: int i,n,a,b,c; public: void get() { cout<<"\n Enter the value "; cin>>n; } void get1() { a=-1; b=1; for(i=1;i<=n;i++) { c=a+b; cout<<"\n The Fibonacci Series is "; cout<<c; a=b; b=c; } } }; int main() { fibo num; num.get(); num.get1(); cout<<"\n"; return 0; } SAMPLE INPUT AND OUTPUT: Enter the value 6 The Fibonacci Series is 0 The Fibonacci Series is 1 The Fibonacci Series is 1 The Fibonacci Series is 2 The Fibonacci Series is 3 The Fibonacci Series is 5

/****DESTRUCTOR FUNCTION****/ PROGRAM CODE: #include <iostream> int c=0; using namespace std; class a { public: a() { c++; cout<<"\nNo of object created:"<<c; } ~a() { cout<<"\nNo of object destroyed:"<<c; c--; } }; int main() { { cout<<"\nEnter main:"; a b1,b2,b3,b4; { cout<<"\nEnter block 1:"; a b5; } { cout<<"\nEnter bloack 2:"; a b6; } cout<<"\nRenter main:"; } return 0; }

SAMPLE INPUT AND OUTPUT:

Enter main: No of object created:1 No of object created:2 No of object created:3 No of object created:4 Enter block 1: No of object created:5 No of object destroyed:5 Enter bloack 2: No of object created:5 No of object destroyed:5 Renter main: No of object destroyed:4 No of object destroyed:3 No of object destroyed:2 No of object destroyed:1

/****SIMPLE BANKING USING CLASS AND OBJECTS****/ PROGRAM CODE: #include<iostream> #include<conio.h> #include<process.h> #include<stdlib.h> using namespace std; class bank { int accno; char name[20],a1[20],a2[20],a3[20]; float bal,d,w,amt; public: void dispaly(); void input() { cout<<"enter the account no:"; cin>>accno; cout<<"Enter your Name: "; cin>>name; cout<<"\n Enter you address "; cin>>a1>>a2>>a3; cout<<"\n Deposite minimum of Rs.500/- "; cout<<"\n Enter amout "; cin>>amt; if(amt>=500) { bal=amt; display(); } else cout<<"\n You have to deposite of minimum Rs.500/- "; } void deposite() { cout<<"\n Enter the Amount to be deposite "; cin>>d; bal=bal+d; display(); } void withdraw() { cout<<"\n Enter the amout to be With draw: "; cin>>w; if(bal<500) { cout<<"\n there is no sufficient balance";

} else bal=bal-w; display(); } void display() { cout<<"\n ACCOUNT NUMBER:"<<accno; cout<<"\nNAME OR THE CUSTOMER:"<<name; cout<<"\n ADDRESS "<<a1<<a2<<a3; cout<<"\n CURRENT BALANCE:"<<bal; }}; int main() { bank a; int j; float amt; int ch, search; char ans; do { cout<<"\n\t\t\t***************"; cout<<"\n\t\t\t\t*BANKING*"; cout<<"\n\t\t\t***************"; cout<<"\n MENU\n1.CREATE\n2.DEPOSITE\n3.WITHDRAW\n4.BALANCE\n5.EXIT"; cout<<"\n enter your choice"; cin>>ch; switch(ch) {case 1: a.input(); break; case 2: a.deposite(); break; case 3: a.withdraw(); break; case 4: a.display(); break; case 5: exit(0); default: cout<<"\n WRONG CHOICE"; break; } cout<<"\n ENTER 'Y' TO CONTINUE"; cin>>ans; } while(ans=='y'||ans=='y'); return 0; }

SAMPLE INPUT AND OUTPUT:

*BANKING*
MENU 1.CREATE 2.DEPOSIT 3.WITHDRAW 4.BALANCE 5.EXIT Enter your choice :1 Enter account no.:111 Enter the name:rajee Enter the initial balance:20000 Enter y to continue MENU 1.CREATE 2.DEPOSIT 3.WITHDRAW 4.BALANCE 5.EXIT Enter your choice :1 Enter account no.:777 Enter the initial balance:15000 Enter y to continue MENU 1.CREATE 2.DEPOSIT 3.WITHDRAW 4.BALANCE 5.EXIT Enter your choice :2 Enter account no.:111 Enter th amount to be deposited:2000 Enter y to continue MENU 1.CREATE 2.DEPOSIT 3.WITHDRAW 4.BALANCE 5.EXIT Enter your choice :4 Enter account no.:111 Account number:111 Name of the customer:rajee Current balance:22000 Enter y to continue MENU 1.CREATE 2.DEPOSIT 3.WITHDRAW 4.BALANCE 5.EXIT

Enter your choice :3 Enter account no.:777 Enter th amount to be withdraw:2000 Enter y to continue MENU 1.CREATE 2.DEPOSIT 3.WITHDRAW 4.BALANCE 5.EXIT Enter your choice :4 Enter account no.:777 Account number:777 Name of the customer:manju Current balance:13000 Enter y to continue MENU 1.CREATE 2.DEPOSIT 3.WITHDRAW 4.BALANCE 5.EXIT Enter your choice :5

You might also like