You are on page 1of 46

Lab Manual C++

February 2

2013
Aseem Jain

This lab manual contains 38 programs and coding in C++ Programming language. It helps students as well as instructor to perform their programs.

Ritika Thalyari

School of Computer Science & Engineering

Lab Manual C++

2013

Acknowledgment
We would like to express our gratitude to many people who provided support, talked things over, read, wrote, offered comments, allowed us to quote their remarks and assisted in the editing, proofreading and design.

We express our thanks to our Vice Chancellor Dr. S. K. Bansal for enabling us to write this manual.

Our deepest thanks to Dr. Rajan Vohra and Dr. Sonia Vatta for encouraging and guiding us with attention and care.

We would also like to thank our institution for providing us this valuable opportunity.

Authors

Page 1 of 45

Lab Manual C++

2013

1. Program to display a message on screen using cout command.

int main ( ) { cout <<"Hello World!"; cout<< "I Am a C++ program"; return 0; }

2. Program for declaring and initializing variables, calculate sum of these variables and then print the result on screen.

#include <iostream> #include<conio.h> int main ( ) { int a, b; int result; a = 5; b = 2; a = a + 1; result = a - b; cout << result; return 0; } // print out the result: // terminate the program: // declaring variables: // process:

Page 2 of 45

Lab Manual C++

3. Program to enter user name, percentage and phone number using cin command. Print the details on screen at last.

2013

#include <iostream> int main ( ) { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

4. Program to create two boxes, called as box1 and box 2 as objects of class furniture. Enter the length, breadth and height for these objects, calculate the volume for both.

#include <iostream> #include<conio.h>

class Box { public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box };

Page 3 of 45

Lab Manual C++

2013

int main( ) { Box Box1; Box Box2; double volume = 0.0; // box 1 specification Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // box 2 specification Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // volume of box 1 volume = Box1.height * Box1.length * Box1.breadth; cout << "Volume of Box1 : " << volume <<endl; // volume of box 2 volume = Box2.height * Box2.length * Box2.breadth; cout << "Volume of Box2 : " << volume <<endl; return 0; } // Declare Box1 of type Box // Declare Box2 of type Box // Store the volume of a box here

Page 4 of 45

Lab Manual C++

5. Program to create a member function (called as interchange) for interchanging values between two variables. Call member function through object.
#include<iostream> class interchange { private: int var1, var2, temp; public: void swap( ) { var1=5; var2=10; temp=var1; var1=var2; var2=temp; } void display( ) { cout<<"The new value of variable1 is:"<<var1<<endl; cout<<"The new value of variable2 is:"<<var2<<endl; } };

2013

void main( ) { interchange I1; I1.swap( ); I1.display( ); getch( ); }

Page 5 of 45

Lab Manual C++

6. Program to enter customer details like mobile number, name, date of birth, billing address, city and phoneno.by using member function.
#include <iostream> class Customer { private: char mobileNo[12]; char name[25]; char dateOfBirth[10]; char billingAdd[50]; char city[25]; char phoneNo[13]; float amountOutstanding; public:

2013

void print( ) { cout << endl << "Mobile phone number: "; cout << mobileNo << endl; cout << "Name: "; cout << name << endl; cout << "Date of Birth: "; cout << dateOfBirth << endl; cout << "Billing Address: "; cout << billingAdd << endl; cout << "City: "; cout << city << endl; cout << "Residence phone number: "; cout << phoneNo << endl; cout << "Amount due: "; cout << amountOutstanding << endl; }
Page 6 of 45

Lab Manual C++

2013

void get( ) { cout << "Mobile phone number: "; cin >> mobileNo; cout << endl << "Name: "; cin >> name; cout << endl << "Date of Birth: "; cin >> dateOfBirth; cout << endl << "Billing Address: "; cin >> billingAdd; cout << endl << "City: "; cin >> city; cout << endl << "Residence phone number: "; cin >> phoneNo; cout << endl << "Amount due: "; cin >> amountOutstanding; } };

int main( ) { Customer object; object.get( ); object.print( ); return 0; }

Page 7 of 45

Lab Manual C++

2013

7.

Program to find the multiplication values and cubic values using inline

function.
#include<iostream.h> #include<conio.h> class line { public: inline float mul(float x,float y) { return(x*y); } inline float cube(float x) { return(x*x*x); } }; void main( ) { line obj; float val1,val2; clrscr(); cout<<"Enter two values:"; cin>>val1>>val2; cout<<"\nMultiplication value is:"<<obj.mul(val1,val2); cout<<"\n\nCube value is "<<obj.cube(val1)<<"\t"<<obj.cube(val2); getch( ); }

Page 8 of 45

Lab Manual C++

8. Program to use nested member functions for entering roll no and two subjects marks, calculate and print average for this.
#include<iostream.h> #include<conio.h> class data { private: int rollno, maths, science; int avg ( ); public: void getdata ( ); void putdata ( ); };

2013

void main( ) { clrscr( ); data stud[5]; for(int i=0;i<5;i++) stud[i].getdata( ); getch( ); } void data::getdata( ) { cout<<"Please enter rollno:"; cin>>rollno; cout<<"Please enter maths marks:"; cin>>maths; cout<<"Please enter science marks:"; cin>>science; putdata( ); }
Page 9 of 45

Lab Manual C++

2013

int data::avg ( ) { int a; a=(maths+science)/2; return a; }

void data::putdata ( ) { cout<<"Average is :"<<avg ( )<<endl; }

9. Program to implement this pointer.


#include<iostream> class Test { private: int x; member's name */ public: void setX (int x) { // The 'this' pointer is used to retrieve the object's x // hidden by the local variable 'x' this->x = x; } void print( ) { cout << "x = " << x << endl; } }; /* local variable is same as a

Page 10 of 45

Lab Manual C++

2013

int main( ) { Test obj; int x = 20; obj.setX(x); obj.print( ); return 0; }

10. Program to implement sizeof operator.


#include <iostream> using namespace std; size_t getPtrSize ( char *ptr ) { return sizeof (ptr ); }

int main ( ) { char szHello[ ] = "Hello, world!"; cout << "The size of a char is: "<< sizeof( char ); cout << "\nThe length of " << szHello << " is: " cout << sizeof szHello; cout << "\nThe size of the pointer is "; cout << getPtrSize( szHello ) << endl; return 0; }

Page 11 of 45

Lab Manual C++

11. Program to implement switch construct for calculating number of days in a month.
#include<iostream> #inlclude<conio.h> int main( ) { int month; cout<<"Tell me a month number."; cout<<"I will tell you the number of days in that month."; cin>>month; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: cout<<"Month has 31 days"; break; case 4: case 6: case 9: case 11: cout<<"Month has 30 days"; break; case 2: cout<<"Month has either 28 or 29(if leap year) days"<<endl; break; default: cout<<"There are only 12 months silly"<<endl; } getch( ); return 0; }

Page 12 of 45

2013

Lab Manual C++

2013

12. Program to merge two 2-D matrices, print the final matrix at last.

#include<iostream> class matrix { private: int matrix1[3][3]; int matrix2[3][3]; int finalmatrix[3][3]; public: void acceptmatrix1( ) { cout<<"Enter the values in Matrix1 "<<endl; cout<<"___________________________"<<endl; for(int r=0;r<3;r++) { cout<<"Enter the value in Row No. "<<r+1<<endl; for(int c=0;c<3;c++) { cin>>matrix1[r][c]; } } } void acceptmatrix2( ) { cout<<"Enter the values in Matrix2 "<<endl; cout<<"___________________________"<<endl; for(int r=0;r<3;r++) { cout<<"Enter the value in Row No. "<<r+1<<endl; for(int c=0;c<3;c++) { cin>>matrix2[r][c]; } } }

Page 13 of 45

Lab Manual C++

2013

void merge( ) { for(int r=0;r<3;r++) { for(int c=0;c<3;c++) { finalmatrix[r][c]=matrix1[r][c]+matrix2[r][c]; } } } void displaymergedmatrix( ) { cout<<endl<<"Display Final Matrix"<<endl; cout<<endl<<"____________________"<<endl; for(int r=0;r<3;r++) { cout<<endl; for(int c=0;c<3;c++) { cout<<finalmatrix[r][c]<<" "; } } } }; void main( ) { matrix m: m.acceptmartrix1( ); m.acceptmartrix2( ); m.merge( ); m.displaymargedmatrix( ); getch( ); }

Page 14 of 45

Lab Manual C++

2013

13. A C++ program to reverse a string.

#include <iostream> class strings { char source[101], dest[101]; int pos=0 , j; public: void reverseString( ) { pos=j=0; cout << "Enter string to be reversed"<< "(please enter a maximum of 100 characters): " << endl ; cin >> source; while(source[pos]!= '\0') { pos = pos + 1; } for(--pos; pos >= 0 ;dest[j++] = source[pos--]) { dest[j] = '\0'; cout << endl << "The reversed string is: " << endl << dest << endl; } };

int main( ) { strings S1; S1.reverseString( ); return 0; }


Page 15 of 45

Lab Manual C++

2013

14. Program to enter 20 books details by using structure array.

#include<iostream.h> #include<stdio.h> struct books { char name[20],author[20]; }a[50]; int main( ) { int i,n; cout<<"No Of Books[less than 50]:"; cin>>n; cout<<"Enter the book details\n"; cout<<"----------------------\n"; for(i=0;i<n;i++) { cout<<"Details of Book No "<<i+1<<"\n"; cout<<"Book Name :"; cin>>a[i].name; cout<<"Book Author :"; cin>>a[i].author; cout<<"----------------------\n"; } cout<<"================================================\n"; cout<<" S.No\t| Book Name\t|author\n"; cout<<"================================================\n"; for(i=0;i<n;i++) { cout<<"\n "<<i+1<<"\t|"<<a[i].name<<"\t| "<<a[i].author; } cout<<"\n==============================================="; return 0; }

Page 16 of 45

Lab Manual C++

2013

15.

Program to implement Pointer for an array.

#include <iostream> using namespace std; int main ( ) { int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) cout << numbers[n] << ", "; return 0; } 16. Program to implement Array of Pointer.

#include <iostream> using namespace std; const int MAX = 3; int main ( ) { int var[MAX] = {10, 100, 200}; int *ptr[MAX]; for (int i = 0; i < MAX; i++) { ptr[i] = &var[i]; // assign the address of integer. } for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << *ptr[i] << endl; } return 0; }

Page 17 of 45

Lab Manual C++

17. Program to implement array of objects. Enter feet and inches for 20 objects and display the details for 20 objects on screen then #include<iostream.h> #include<conio.h> class distance { private: int feet; float inches; public: void getdistance( ); void showdistance( ); };

2013

void distance::getdistance( ) { cout<<"\t Enter feet = "; cin>>feet; cout<<"\t Enter inches = "; cin>>inches; } void distance::showdistance() { cout<<"\n\t Total distance is "<<feet<<"\' "<<inches<<'"'<<endl; }

int main( ) { clrscr( ); distance obj[25]; char ans; int n=0;


Page 18 of 45

Lab Manual C++

2013

do { cout<<"\n Enter the distance number = "<<n+1<<endl; obj[n++].getdistance( ); cout<<"\n Enter another (y/n)?"; cin>>ans; } while(ans!='n'); for(int i=0;i<n;i++) { cout<<"\n Distance number "<<i+1<<" is :"; obj[i].showdistance(); } getch( ); return 0; }

18. Program to implement Friend Function.

#include <iostream> using namespace std; class Box { double width; public: friend void printWidth( Box box ); void setWidth( double wid ); }; // Member function definition void Box::setWidth( double wid ) { width = wid; }

Page 19 of 45

Lab Manual C++

2013

// Note: printWidth( ) is not a member function of any class. void printWidth( Box box ) { /* Because setWidth() is a friend of Box, it can directly access any member of this class */ cout << "Width of box : " << box.width <<endl; } // Main function for the program int main( ) { Box box; // set box width without member function box.setWidth(10.0); // Use friend function to print the wdith. printWidth( box ); return 0; }

19. Program to implement Friend Class.


#include<iostream.h> #include<conio.h> class readint { float a,b; public: void read( ) { cout<<"\n\nEnter the First Number : "; cin>>a; cout<<"\n\nEnter the Second Number : "; cin>>b; } friend class sum; }; class sum { public: float c;

Page 20 of 45

Lab Manual C++

2013

void add(readint rd) { c=rd.a+rd.b; cout<<"\n\nSum="<<c; } }; void main( ) { int cont; readint rd; sum s; clrscr(); do { clrscr( ); rd.read( ); s.add(rd); cout<<"\n\nDo you want to continue?(1-YES,0-NO)"; cin>>cont; }while(cont==1); getch( ); }
20.

20. Program to implement Constructor of a class.

#include <iostream> using namespace std; class Line { private: double length; public: void setLength( double len ); double getLength( void ); Line( ); // This is the constructor };

Page 21 of 45

Lab Manual C++

2013

Line::Line(void) {

// Member functions definitions including constructor

cout << "Object is being created" << endl; }

void Line::setLength( double len ) { length = len; }

double Line::getLength( void ) { return length; } int main( ) { Line line; // set line length line.setLength(6.0); cout << "Length of line : " << line.getLength( ) <<endl; return 0; } // Main function for the program

Page 22 of 45

Lab Manual C++

21. Program to implement parameterized constructor of a class, copy constructor and destructor.
#include<iostream.h> #include<conio.h> class student //specify a class { private : int rollno; //class data members float marks; public: student( ) //default constructor { rollno=0; marks=0.0; } student(int r, int m) //parameterized constructor { rollno=r; marks=m; } student(student &t) //copy constructor { rollno=t.rollno; marks=t.marks; } void getdata( ) //member function to get data from user { cout<<"Enter Roll Number : "; cin>>rollno; cout<<"Enter Marks : "; cin>>marks; }
Page 23 of 45

2013

Lab Manual C++

2013

void showdata() // member function to show data { cout<<"\nRoll number: "<<rollno<<"\nMarks: "<<marks; } ~student( ) //destructor { // Empty body// } };

int main( ) { clrscr(); student st1; student st2(5,78); student st3(st2); st1.showdata( ); st1 st2.showdata( ); st2 st3.showdata( ); st3 return 0; } //display data members of object //display data members of object //defalut constructor invoked //parmeterized constructor invoked //copy constructor invoked //display data members of object

Page 24 of 45

Lab Manual C++

2013

22. Program to implement new and delete operator in class.


#include <iostream> #include <new> using namespace std; int main ( ) { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) { cout << p[n] << ", "; } delete[ ] p; } return 0; }

Page 25 of 45

Lab Manual C++

2013

23. Program to implement Nested Classes concept.

#include <iostream.h> class Nest { public: class Display { private: int s; public: void sum( int a, int b) { s =a+b; } void show( ) { cout << "\nSum of a and b is:: " << s;} }; };

void main( ) { clrscr( ); Nest::Display x; x.sum(12, 10); x.show( ); getch( ); }

Page 26 of 45

Lab Manual C++

2013

24. Program to implement Local Variable.

#include <iostream> using namespace std; int main ( ) { int a, b, c; // actual initialization a = 10; b = 20; c = a + b; cout << c; return 0; } // Local variable declaration:

25. Program to implement Global Variable. #include <iostream> using namespace std; int g; int main ( ) { int a, b; // actual initialization a = 10; b = 20; g = a + b; cout << g; return 0; } // Local variable declaration: // Global variable declaration:

Page 27 of 45

Lab Manual C++

2013

26. Program to implement Single Level Inheritance. #include<iostream.h> #include<conio.h> class abc { private: int rl; char name[20]; public: void read( ) { cout<<"Enter Roll No and name "<<endl; cin>>rl>>name; } void display() { cout<<"roll no "<<rl<<endl; cout<<"name : "<<name <<endl; } }; class deepak :public abc { private: int m1; int m2; int t; public: void read1( ) { cout<<"enter m1 , m2"<<endl; cin>>m1>>m2; t= m1+m2; }
Page 28 of 45

Lab Manual C++

2013

void display1( ) { cout<<"m1 "<<m1<<endl; cout<<"m2 "<<m2 <<endl; cout<<"total "<<t<<endl; } }; void main ( ) { deepak ob; clrscr( ); ob.read( ); ob.read1( ); ob.display( ); ob.display1( ); getch( ); }

Page 29 of 45

Lab Manual C++

2013

27. Program to implement Multilevel Inheritance.


#include<iostream.h> #include<conio.h> class A { int a1,a2; public : void getdata( ) { cout<<"\n Enter value of a1 and a2"; cin>>a1>>a2; } void putdata( ) { cout<<"\n value of a1 is" <<a1"and a2 is"<<a2; } };

class B: public A {

//class B is publicly derived by class A

int b1,b2; public : void indata( ) { cout<<"\n Enter the value of b1 nad b2"; cin>>b1>>b2; } void outdata( ) { cout<<"\n the value of b1 is" <<b1 "and b2 is:<<b2; } };
Page 30 of 45

Lab Manual C++

2013

class C: public B { int c1, c2; public void input() { cout<<"\n enter the value of c1 and c2"; cin>>c1>>c2; } void output ( ) { cout<<"\nvalue of c1 is"<<c1"and c2 is"<<c2; } }; void main( ) { C obj obj.getdata ( ); obj.indata ( ); obj.input( ); obj.putdata ( ); obj.outdata( ); obj.output ( ); } //member function of class A //member function of class B //member function of class C //member function of class A //member function of class B //member function of class C

Page 31 of 45

Lab Manual C++

2013

28. Program to implement Multiple Inheritance. #include<iostream.h> #include<conio.h> 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; 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; } }; // sm = Sports mark

: "<<tot;

Page 32 of 45

Lab Manual C++

2013

void main( ) { clrscr( ); statement obj; obj.get( ); obj.getsm( ); obj.display( ); getch( ); }

29. Program to implement Function Overloading.

#include<iosteram.h> long add(long, long); float add(float, float); int main( ) { long a, b, x; float c, d, y;

/* Function arguments are of different data type */

cout << "Enter two integers\n"; cin >> a >> b; x = add(a, b); cout << "Sum of integers: " << x << endl; cout << "Enter two floating point numbers\n"; cin >> c >> d; y = add(c, d); cout << "Sum of floats: " << y << endl; return 0; } long add(long x, long y) { long sum; sum = x + y; return sum; }
Page 33 of 45

Lab Manual C++

2013

float add(float x, float y) { float sum; sum = x + y; return sum; } 30. Program to implement Binary Operator Overloading. #include <iostream> using namespace std; class Box { double length; double breadth; double height; public: // Length of a box // Breadth of a box // Height of a box

double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; }
Page 34 of 45

Lab Manual C++

2013

// Overload + operator to add two Box objects. Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } };

int main( ) { Box Box1; Box Box2; Box Box3; double volume = 0.0; Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);

// Main function for the program

// Declare Box1 of type Box // Declare Box2 of type Box // Declare Box3 of type Box // Store the volume of a box here // box 1 specification

// box 2 specification

volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; Box3 = Box1 + Box2; follows: volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; }

// volume of box 1

// volume of box 2

// Add two object as

// volume of box 3

Page 35 of 45

Lab Manual C++

2013

31. Program to implement Unary Operator Overloading.

#include<iostream.h>

#include<conio.h> class complex { int a,b,c; public: complex( ) { } void getvalue( ) { cout<<"Enter the Two Numbers:"; cin>>a>>b; } void operator++( ) { a= ++a; b= ++b; } void operator- -( ) { a= - -a; b= - -b; } void display( ) { cout<<a<<"+\t"<<b<<"i"<<endl; } };

Page 36 of 45

Lab Manual C++

2013

void main( ) { clrscr( ); complex obj; obj.getvalue( ); obj + +; cout<<"Increment Complex Number\n"; obj.display( ); obj - -; cout<<"Decrement Complex Number\n"; obj.display( ); getch( ); }

32. Program to implement Virtual Function.

#include<iostream.h> #include<conio.h>

class base { public: virtual void show( ) { cout<<"\n Base class show:"; } void display() { cout<<"\n Base class display:" ; } };

Page 37 of 45

Lab Manual C++

2013

class drive:public base { public: void display( ) { cout<<"\n Drive class display:"; } void show( ) { cout<<"\n Drive class show:"; } }; void main( ) { clrscr( ); base obj1; base *p; cout<<"\n\t P points to base:\n" ; p=&obj1; p->display( ); p->show( ); cout<<"\n\n\t P points to drive:\n"; drive obj2; p=&obj2; p->display( ); p->show( ); getch( ); }

Page 38 of 45

Lab Manual C++

2013

33. Program to implement Abstract Class.

#include <iostream.h> using namespace std; class area { double dim1, dim2; public: void setarea(double d1, double d2) { dim1 = d1; dim2 = d2; } void getdim(double &d1, double &d2) { d1 = dim1; d2 = dim2; } virtual double getarea( ) = 0; // pure virtual function };

class rectangle : public area { public: double getarea( ) { double d1, d2; getdim(d1, d2); return d1 * d2; } };

Page 39 of 45

Lab Manual C++

2013

class triangle : public area { public: double getarea( ) { double d1, d2; getdim(d1, d2); return 0.5 * d1 * d2; } };

int main( ) { area *p; rectangle r; triangle t; r.setarea(3.3, 4.5); t.setarea(4.0, 5.0); p = &r; cout << "Rectangle has area: " << p->getarea() << '\n'; p = &t; cout << "Triangle has area: " << p->getarea() << '\n'; return 0; }

Page 40 of 45

Lab Manual C++

2013

34. Program to implement Function Template.

#include <iostream> template <class T> T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); }

int main ( ) { int i=5, j=6, k; long l=10, m=5, n; k=GetMax<int>(i,j); n=GetMax<long>(l,m); cout << k << endl; cout << n << endl; return 0; }

Page 41 of 45

Lab Manual C++

2013

35. Program to implement Class Template.

#include <iostream> using namespace std; template <class T> class mypair { T a, b;

public: mypair (T first, T second) { a=first; b=second; } T getmax (); };

template <class T> T mypair<T>::getmax () { T retval; retval = a>b? a : b; return retval; }

int main ( ) { mypair <int> myobject (100, 75); cout << myobject.getmax(); return 0; }
Page 42 of 45

Lab Manual C++

36. A simple Program to implement Exception Handling concept using try and catch statement.

2013

#include <iostream> using namespace std; int main ( ) { try { throw 20; } catch (int e) { cout << "An exception occurred. Exception Nr. " << e << endl; } return 0; }

37. Program to implement exception handling for divide by zero Exception. #include<iostream.h> #include<conio.h> void main( ) { int a,b,c; float d; clrscr( ); cout<<"Enter the value of a:"; cin>>a; cout<<"Enter the value of b:"; cin>>b; cout<<"Enter the value of c:"; cin>>c; try {
Page 43 of 45

Lab Manual C++

2013

if((a-b)!=0) { d=c/(a-b); cout<<"Result is:"<<d; } else { throw(a-b); } } catch(int i) { cout<<"Answer is infinite because a-b is:"<<i; } getch( ); }

38. Program to implement Rethrowing of exception by using throw command. #include <iostream> using namespace std; void MyHandler( ) { try { throw hello; } catch (const char*) { cout <<Caught exception inside MyHandler\n; throw; //rethrow char* out of function } }

Page 44 of 45

Lab Manual C++

2013

int main( ) { cout<< Main start; try { MyHandler( ); } catch(const char*) { cout <<Caught exception inside Main\n; } cout << Main end; return 0; }

39)

Case study for project.

40)

Project based on : Inheritance. Polymorphism. Abstract Classes. Function/Class Templates. Exception Handling.

Page 45 of 45

You might also like