Oops Lab Manual
Oops Lab Manual
Algorithm: Step 1: Start the program. Step 2: Define the class with test. Step 3: Declare the code and count. Step 4: Declare and define the function set code, show code and show count. Step 5: Declare the object t1,t2. Step 6: Call the function and print the result. Step 7: Stop the program. Program: #include<iostream.h> #include<conio.h> class test { int code; static int count; public: void setcode() { code=++count; } void showcode() { cout<<"object number:"<<code<<"\n"; } static void showcount() { cout<<"count:"<<count<<"\n"; } }; int test::count; //void test:showcount() int main() { test t1,t2; clrscr(); t1.setcode();
t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); return(0); } Out put: COUNT :2 COUNT :3 OBJECT NUMBER :1 OBJECT NUMBER :2 OBJECT NUMBER :3
Result: Thus the c++ program for static member function was executed and the output is verified.
DEFAULT ARUGMENT Ex.No:1(b) Date : Aim: To write a c++ program using default arguments. Algorithm: Step 1:Start the program. Step 2:Declare and initialize the variable amount. Step 3:declare and function value and print line(). Step 4:Define the function. Step 5:print the result. Step 6:Stop the program. Program: #include<iostream.h> #include<conio.h> int main() { Float amount; float value(float p, int n,float r=0.15); void printline(char ch='*',int ten=15); printline(); amount=value(5000.00,5); cout<<amount; cout<<"\n"; printline('='); return(0); } float value(float p,int n,float r) { int year=1; float sum=p; while(year<=n) { sum=sum*(1+r); year=year+1; } return sum; } void printline(char ch,int ten) {
for(int i=1;i<=ten;i++) cout<<ch; cout<<ch; cout<<"\n"; getch(); } Output: ************************************ 10056.786133 ================================
Result: Thus the c++ program for default argument was executed and then output is verified,
MATRIX VECTOR MULTPLICATION Ex.no:1(c) Date : Aim: To write a c++ program for matrix vector multiplication using friend function. Algorithm: Step 1: start the program. Step 2: Declare the class with class name vector. Step 3: Declare the variable a [3]. Step 4: Declare and define the function get () and put(). Step 5: Declare and define the friend function Step 6: Declare the class with class name matrix. Step 7: Declare the variable m [3] [3] Step 8: declare the define function get,put and friend function. Step 9:declare the object v1,v2 vector and m1 for matrix. Step 9: call the function and print the result. Step 10:stop the program. Program: #include<iostream.h> #include<conio.h> class matrix; class vector { float a[3]; public: void get(); void put(); friend vector mult(vector &,matrix &); }; class matrix { float m[3][3]; public: void get(); void put(); friend vector mult(vector &,matrix &); }; void vector::get() {
cout<<"enter the elements"<<endl; for(int i=0;i<3;i++) cin>>a[i]; } void vector::put() { cout<<"the vector is:["<<a[0]<<","<<a[1]<<","<<a[2]<<"]"<<endl; } void matrix::get() { cout<<"enter the elements m[3][3] element"<<endl; for(int i=0;i<3;i++) for(int j=0;j<3;j++) cin>>m[i][j]; } void matrix::put() { cout<<"the matrix is"<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) cout<<m[i][j]<<"\t"; cout<<endl; } } vector mult(vector &x,matrix &y) { vector temp; for(int i=0;i<3;i++) for(int j=0;j<3;j++) { temp.a[i]=0; for(int i=0;i<3;i++) temp.a[i]+=(x.a[i]*y.m[i][j]); } return temp; } void main() { clrscr(); vector v1,v2; matrix m1; v1.get(); m1.get(); cout<<"before multiplication:"<<endl; v1.put();
m1.put(); v2=mult(v1,m1); cout<<"after multiplication:"<<endl; v2.put(); getch(); } Output: Enter the elements: 123 Enter the matrix m[3][3] elements 1 2 3 4 5 6 7 8 9 Before multiplication: The vector is:[1,2,3] The matrix is 1 2 3 4 5 6 7 8 9 After multiplication: The vector is :[30,36,42] Result: Thus the c++ program matrix vector multiplication using friend function was executed and the output was verified.
IMPLEMENTATION COMPLEX NUMBER CLASS WITH NECCESSARY OPERATOR OVERLOADING Ex.No:2 Date: Aim: To implement the c++ program for complex number class with necessary operator overloading. Algorithm: Step 1: Start the Program Execution Step 2: Create the Classes and declare all the Variables and Member functions. Step 3: Use the template to perform operator overloaded with complex class. Step 4: Create the class name as COMPLEX. Step 5: Declare the data member and member function. Step 6: Declare the template for handling various operator overloaded Operation. Step 7: Display the result. Step 8: Stop the Program Execution. Program: #include<iostream.h> #include<conio.h> class complex { float real; float image; public: complex() { real=image=0; } operator int() { return(real); } void getadata() { cout<<"real part"; cin>>real; cout<<"imag part"; cin>>image; } void outdata(char msg)
{ cout<<endl<<msg; cout<<"("<<real; cout<<","<<image<<")"; }; complex operator+(complex c2); complex operator-(complex c2); complex operator*(complex c2); complex operator/(complex c3); } complex complex::operator+(complex c2) { complex temp; temp.real=real+c2.real; temp.imag=imag+c2.imag; return(temp); } complex complex::operator-(complex c2) { complex temp; temp.real=real-c2.real; temp.imag=imag-c2.imag; return(temp); } complex complex::operator*(complex c2) { complex temp; temp.real+real*c2.real-imag*c2.imag; temp.imag=real*c2.imag+imag*c2.real; return(temp); } complex complex::operator/(complex c2) { complex temp; float qt; qt=c2.real*c2.real+c2imag*c2.imag; temp.real=(real*c2.real+imag*c2imag)/qt; temp.imag=(imag*c2.real-real*c2.imag)/qt; return(temp); } void main() { complex c1,c2,c3; cout<<"enter c1.."<<endl; c1.get data(); cout<<"enter c2.."<<endl;
c2.get data(); cout<<"enter complex are.."; c1.outdata("c1="); c2.outdata("c2="); cout<<endl<<"results are..."; c3=c1+c2; c3.outdata("c3=c1+c2:"); c3=c1-c2; c3.outdata("c3=c1-c2:"); c3=c1*c2; c3.outdata("c3=c1*c2:"); c3=c1/c2; c3.outdata("c3=c1/c2:"); complex c4; int number; c4.outdata("type conversion"); number=c4; cout<<endl<<"type conversion is"<<number; getch(); } Output: Enter c1: Real part 8 Imaginary part 9 Enter c2: Real part 6 Imaginary complex are: C1=(8,9) C2=(6,5) Results are: C3=c1+c2;(14,14) C3=c1-c2;(2,4) C3=c1*c2;(3,94) C3=c1/c2;(1,52459,229508) real part. Result: Thus the c++ program implementation of complex number has been executed successfully and the output was verified.
10
11
int i,j; cout<<"\n copy constructor invoked...\n"; row=m2.row; col=m2.col; m=new int*[row]; for(i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<m2.row;i++) m[i]=new int[col]; for(i=0;i<m2.row;i++) for(j=0;j<m2.col;j++) m[i][j]=m2.m[i][j]; } void matrix::operator=(matrix &m2) { cout<<"\n assignment operator overloading..\n"; for(int i=0;i<m2.row;i++) for(int j=0;j<m2.col;j++) m[i][j]=m2.m[i][j]; } matrix::matrix(int row1,int col1) { row=row1; col=col1; m=new int *[row]; for(int i=0;i<row;i++) m[i]=new int[col]; } matrix::~matrix() { for(int i=0;i<row;i++) delete m[i]; delete m; } void matrix::add(matrix &a,matrix &b) { int i,j; row=a.row; col=b.col; if(a.row!=b.row||a.col!=b.col) { cout<<"error..."; } for(i=0;i<row;i++) for(j=0;j<col;j++) m[i][j]=a.m[i][j]+b.m[i][j];
12
} void matrix::read() { int i,j; for(i=0;i<row;i++) for(j=0;j<col;j++) { cout<<"matrix["<<i<<","<<j<<"]"; cin>>m[i][j]; }} void matrix::show() { int i,j; for(i=0;i<row;i++) { cout<<endl; for(int j=0;j<col;j++) cout<<m[i][j]<<"\t"; cout<<endl; }} void main() { int m,n,p,q; clrscr(); cout<<"\n enter no of row elements for matrix a...."; cin>>m; cout<<"\n enter no of column elements for matrix a..."; cin>>n; matrix a(m,n); a.read(); cout<<"\n enter no of rows in matrix b......"; cin>>p; cout<<"\n enter no of column in matrix b....."; cin>>q; matrix b(p,q); b.read(); cout<<"a matrix"; a.show(); cout<<"b matrix"; b.show(); matrix c(m,n); c.add(a,b); cout<<" addition of two matrix"; c.show(); matrix d=c;
13
Output: Enter the no. of row elements for matrix A.2 Enter the no. of column elements for matrix A.2 Matrix[0,0]1 Matrix[0,1]2 Matrix[1,0]3 Matrix[1,1]4 Enter the no. of row elements for matrix b.2 Enter the no. of column elements for matrix b.2 Matrix[0,0]1 Matrix[0,1]2 Matrix[1,0]3 Matrix[1,1]4 A matrix 1 2 3 4 B matrix 1 2 3 4 Addition of two matrix 2 4 6 8 Copy constuctor 2 4 6 8 Assignment operator is invoked. 1 2 3 4 Result: Thus the c++ program dynamic memory allocation has been executed successfully and the output was verified.
14
OVERLOAD THE NEW AND DELETE OPERATORS TO PROVIDE CUSTOM DYNAMIC ALLOCATION OF MEMORY.
Ex.No:4 Date: Aim: To implement class with dynamic memory allocation using new and delete operator to overload. Algorithm: Step 1: Start the Program Execution Step 2: Create the Classes and declare all the Variables and Member functions. Step 3: Using the classes with dynamic memory alloction. Step 4: New and delete operator is overloaded along with dynamic Memory allocation. Step 5: Create the class. Step 6: Declare the new and delete operator. Step 7: Perform the overload operation Step 8: Display the result. Step 9: Stop the Program Execution. Program: #include<iostream.h> #include<conio.h> #include<alloc.h> class vector { int *array; public: vector() { cout<<"constructor is called\n"; } ~vector() { cout<<"\n destructor iscalled\n"; } void *operator new(size_t size); void operator delete(void *vec); void read(); int sum(); 15
}; void *vector::operator new(size_t size) { vector *myvector; myvector=::new vector; myvector->array=new int[10]; return myvector; } void vector::operator delete (void *vec) { vector *myvect; myvect=(vector *)vec; delete(int *)myvect->array; ::delete vec; } void vector::read() { for(int i=0;i<10;i++) { cout<<"vector ["<<i<<"]="; cin>>array[i]; } } int vector::sum() { int sum=0; for(int i=0;i<10;i++) sum+=array[i]; return sum; } void main() { clrscr(); vector *myvector=new vector; cout<<" enter vector data...."<<endl; myvector->read(); cout<<"sum of vector="<<myvector->sum(); delete myvector; getch(); }
16
Output: Constructor is called Constructor is called Enter vector data Vector [0]=1 Vector [1]=2 Vector [2]=3 Vector [3]=4 Vector [4]=5 Vector [5]=6 Vector [6]=7 Vector [7]=8 Vector [8]=9 Vector [9]=1 Sum of vector =46 Destructor is called
Result: Thus the c++ program has overload new and delete operator been executed successfully and the output was verified
17
DEVELOP A TEMPLATE OF LINKED LIST CLASS AND ITS METHODS Ex.No: 5 Date: Aim: To implement the linked list to perform insert, delete and display the data by using c++. Algorithm: Step 1: Start the Program Execution Step 2: Create the Classes and declare all the Variables and Member functions. Step 3: Create the class with LIST.. Step 4: Create a member function for constructor and to insert the element in the linked list. Step 5: Use the pointer to refer the next list. Step 6: Display the list value. Step 7: Stop the Program Execution. Program: #include<iostream.h> #include<conio.h> #include<process.h> #define null 0 template<class t> class list { int data; list *next; public: list() { data=0.0; next=null; } list (int dat) { data=dat; next=null; } void insert(list *node); void display(list*); }; template<class t>
18
void list<t>::insert(list<t>*node) { list*last=this; while(last->next) last=last->next; last->next=node; } template<class t> void list<t>::display(list<t>*first) { list*traverse; for(traverse=first;traverse;traverse=traverse=traverse->next) cout<<traverse->data<<"->"; cout<<"null"; cout<<endl; } void main() { int choicel,data; clrscr(); list<int>*first=null; list<int>*node; while(1) { cout<<"\n linked list"; cout<<"\n.1 inserted"; cout<<"\n.2display"; cout<<"\n.3 exit"; cout<<"\n choice[i-3]\t"; cin>>choicel; switch(choicel) { case 1:cout<<"\n enter the data\t"; cin>>data; node=new list<int >(data); if(first==null) first=node; else first->insert(node); break; case 2: first->display(first); break; case 3:exit(1); } }getch(); }
19
Enter the data 1 Linked list 1.inserated 2.display 3.exit Choice[1-3] 1 Enter the data 4 Linked list 1.inserated 2.display 3.exit Choice[1-3] 1 Enter the data 8 Linked list 1.inserated 2.display 3.exit Choice[1-3] 2 1->4->8->NULL Linked list 1.inserated 2.display 3.exi Choice[1-3] Result: Thus the c++ program template of link list has been executed successfully and the output was verified
20
Ex.No:6(a) Date: Aim: To implement the sorting using bubble sort by using c++ Algorithm: Step 1: start the program execution. Step 2: create the classes and declare all the variables and member functions Step 3: create the class with list Step 4: create a member function for constructor and to insert the element in the Linked list Step 5: use the pointer to refer the next list Step 6: display the list value Step 7: stop the program execution. Program: #include<iostream.h> #include<conio.h> template<class t> void print(t*a,int n) { cout<<a[0]; for(int i=1;i<n;i++) { cout<<","<<a[i]; } } template<class t> void sort(t*a,int n) { for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) {
21
if(a[j-1]>a[j]) { t temp=a[j-1]; a[j-1]=a[j]; a[j]=temp; } } } } void main() { int a[]={12,11,15,13,17,14,16,19,18}; clrscr(); cout<<"\n before sorting"; print(a,9); sort(a,9); cout<<"\n after sorting"; print(a,9); char ch[]={'b','d','a','f','g','j','r','u','s'}; cout<<"\n before sorting\n"; print(ch,9); sort(ch,9); cout<<"\n after sorting\n"; print(ch,9); getch(); } Output: Before sorting 12,11,15,13,17,14,16,19,18 After sorting 11,12,13,14,15,16,17,18, Before sorting b,d,a,f,g,j,r,u,s After sorting a,b,d,f,g,j,r,s,u
Result: Thus the above program template for using bubble has been executed successfully and output was verified
22
DEVELOP TEMPLATES FOR USING INSERTION SORT Ex.No:6(b) Date: Aim: To implement the sorting using insertion sort by using c++ Algorithm: Step 1: start the program execution Step 2: create the classes and declare all the variables and member function Step 3: create the class with list Step 4: create a member function for constructor and to insert the element in The linked list Step 5: use the pointer to refer the next list Step 6: display the list value Step 7: stop the program execution. Program: #include<iostream.h> #include<conio.h> template<class T> void insert(T a[],int n) { T temp; for(int i=1;i<n-1;i++) { temp=a[i]; for(int j=1;j>=1;j--) { if(temp<a[j-1]) a[j]=a[j-1]; else break; } a[j]=temp; }} void main() { int i,j,n; int x[5]={10,80,50,60,74};
23
float y[5]={1.1,2.2,9.7,6.7,5.9}; clrscr(); cout<<"unsorted x array\t"; for(i=0;i<5;i++) cout<<x[i]<<" "; cout<<endl; cout<<"unsorted y array\t"; for(j=0;j<5;j++) cout<<y[j]<<" "; cout<<endl; insert(x,5); insert(y,5); cout<<"sorted x array\t"; for(i=0;i<5;i++); cout<<x[i]<<" "; cout<<endl; cout<<"sorted y array\t"; for(j=0;j<5;j++); cout<<y[j]<<" "; cout<<endl; getch(); } Output: Unsorted x array 10 80 50 60 74 Unsorted y array 1.2 2.2 9.7 6.7 5.9 Sorted x array 10 50 60 74 80 Sorted y array 1.1 2.2 Result: Thus the above program template for using insertion sort has been executed successfully and output was verified.
24
DEVELOP TEMPLATES FOR USING MERGE SORT Ex.No:6(c) Date: Aim: To implement the sorting using merge sort by using c++. Algorithm: Step 1: start the program execution Step 2: create the classes and declare all the variables and member function Step 3: create the class with list Step 4: create a member function for constructor and to insert the element in the linked list Step 5: use the pointer to refer the next list Step 6: display the list value Step 7: stop the program execution. Program: #include<iostream.h> #include<conio.h> template<class t> void print(t*a,int n) { cout<<a[0]; for(int i=1;i<n;i++) { cout<<","<<a[i]; } } template<class t> void merge(t*a,int n1,int n2) { t*temp=new t[n1+n2]; int i=0,j1=0,j2=0; while(j1<n1&&j2<n2) temp[i++]=(a[j1]<=a[n1+j2]?a[j1++]:a[n1+j2++]); while(j1<n1) temp[i++]=a[j1++]; while(j2<n2) temp[i++]=(a+n1)[j2++]; for (i=0;i<n1+n2;i++)
25
a[i]=temp[i]; delete[]temp; } template<class t> void sort(t*a,int n) { if(n>1) { int n1=n/2; int n2=n-n1; sort(a,n1); sort(a+n1,n2); merge(a,n1,n2); } } void main() { int a[]={12,11,15,13,17,14,16,19,18}; clrscr(); cout<<"\n before sorting"; print(a,9); sort(a,9); cout<<"\n after soring\n"; print(a,9); char ch[]={'b','d','a','f','g','j','r','u','s'}; cout<<"\n before sorting"; print(ch,9); sort(ch,9); cout<<"\n after sorting"; print(ch,9); getch(); } Output: Before sorting After sorting Before sorting After sorting Result: Thus the above program template for using merge was executed and the output is verified. 12,11,15,13,17,14,16,19,18 11,12,13,14,15,16,17,18,19 b,d,a,f,g,j,r,u,s a,b,d,f,g,j,r,s,u
26
DESIGN STACK AND QUEUE CLASSES WITH NECESSARY Ex.No:7 Date: Aim: To implement stack and queue using exception handling c++. Algorithm: Step 1: To perform LIFO and FIFO operation Step 2: create the class for stack Step 3: declare the member function for push and pop operation Step 4: push operation to insert the element Step 5: pop operation to delete the element Step 6: create a class for queue Step 7: declare the member function for rear and front operation Step 8: provide necessary exception handling for stack and queue Step 9: display the result. Program: #include<iostream.h> #include<conio.h> #include<process.h> #define max 3 class except1{}; class except2{}; class except3{}; class except4{}; class stack { public: int top; int push(int[],int); int pop(int[],int*); void point(int[]); void displaymenu(); stack() {
27
top=-1; } }; void stack::displaymenu() { cout<<"\n 1.push"; cout<<"\n 2.pop"; cout<<"\n 3.view"; cout<<"\n 4.exit"; } int stack::push(int stk[],int element) { if(top<(max-1)) { stk[++top]=element; return 0; } else return -1; } int stack::pop(int stk[],int*element) { if(top>=0) { *element=stk[top--]; return 0; } else return -1; } void stack::print(inkstk[]) { if(top==-1) throw except1(); else { cout<<"\n the content of the stack is ... top"; for(i=top;i>=0;i--) cout<<"-->"<<stk[i]; if(top==(max-1)) throw except2(); } } catch(except1)
28
{ cout<<"\n stack is empty"; } catch(except2) { cout<<"\n stack is full:"; } void main() { int data,status,choice,stk[max];stack s; s.displaymenu(); while(1) { try { cout<<"\n choice[1-4]?"; cin>>choice; switch(choice) { case 1: cout<<"enter the element:"; cin>>data; status=s.push(stk,data); if(ststus==-1) throw except 3(); else s.print(stk); break; case 2: status=s.pop(stk,&data); if(status==-1) throw except4(); else { cout<<"the popped value is"<<data; s.print(stk); } break; case 3: s.print(stk); break 4; exit(1); } } catch(except3)
29
{ cout<<"stack overflow"; } catch(except 4) { cout<<"stack underflow"; } } } Output: 1. Push 2. Pop 3. View 4. Exit Choice[1-4]?1 Enter the element :3 The content of the stack is ..top->3 Choice[1-4]?1 Enter the element :5 The content of the stack is..top->5->3 Choice[1-4]?1 Enter the element:7 The content of the stack is..top->7->5->3stack is full Choice[1-4]?1 Enter the element:8 Stack overflow Choice[1-4]?2 The content of the stack is ..top->5->3 Choice[1-4]?2 The popped value is 5 The content of the stack is ..top->3 Choice[1-4]?2 The popped value is 3 Stack is empty Choice[1-4]?2 Stack underflow Stack is empty Choice[1-4]? Result: Thus the above program stack and queue with exception handling has been executed successfully and output was verified.
30
IMPLEMENTATION OF GRAPH Ex.No:8 Date: Aim: To implement the graph and to obtain a minimum cost spanning tree Algorithm: Step 1:start the program execution Step 2: to know about the concept of graph along with minimum spanning tree Step 3: create two classes as point and arc Step 4: define a graph class which represents the collection of point and arc object Step 5: write a method to find a minimum cost spanning tree in a graph Step 6: display the result Step 7: stop the program execution. Program: #include<iostream.h> #include<conio.h> class point { public: int n; }; class arc { public: int a[10][10]; }; class graph:public point,public arc { public: graph(); void krush(); }; void graph::graph()
31
{ int i,j; cout<<"enter the number of nodes"; cin>>n; cout<<"enter the weightmatrix:"; for(i=0;i<=n;i++) { for(j=1;j<=n;j++) { cin>>a[i][j]; } } } void graph::krush() { int i,j,min,imin,jmin,flg=0,v[10],sum=0; for(i=1;i<=1;i++) { v[i]=0; } while(flg==0) { min=1000; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if((a[i][j]<min)&&(a[i][j]!=0)) { min=a[i][j]; imin=i; jmin=j; } } } sum=sum+a[imin][jmin]; a[imin][jmin]=0; a[jmin][imin]=0; v[imin]=v[jmin]=1; cout<<"/n("<<imin<<","<<jmin<<")"; flg=1; for(i=1;i<=n;i++) { if(v[i]==0)
32
flg=0; } } cout<<"/n minimal cost of spanning tree="<<sum; } void main() { clrscr(); cout<<"/n kruskals algorithm:"; graph g; g.krush(); getch(); } Output: Enter the number of nodes 4 Enter the weight matrix: 0123 1045 2406 3560 (1,2) (1,3) (1,4) Minimum cost of spanning tree=6 Result: Thus the c++ program implementation of graph has been was executed successfully and the output was verified.
33
POLYMORPHISM AND RTTI. Ex.No:9 Date: Aim: To write a c++ program to design a simple test application to demonstrate dynamic polymorphism and RTTI. Algorithm: Step 1:start the program execution Step 2: create the classes and declare all the variables and member function Step 3: create the classes with RTTI Step 4: create a member function and operate to perform RTTI operation and Dynamic polymorphism Step 5: display the result Step 6: stop the program execution.
Program: #include<iostream.h> #include<conio.h> #include<type info.h> class point { public: int x,y; point(){} point(int tempx,int tempy) { x=tempx; y=tempy; } int getx() { return x; } int gety()
34
{ return y; } friend ostream &operator<<(ostream &tempout.point &temppoint) { tempout<<"("<<temppoint.getx()<<","<<temppoint.gety()<<")"; return tempout; } }; class shape { pointposition; public: shape(){} virtual void draw() { cout<<"shape is drawn"; } }; class square:public shape { point leftbottom; int length; public: square(){} square(point tleftbottom,int tlength) { leftbottom=tleftbottom; length=tlength; } void draw() { cout<<"square is drawn at"<<leftbottom<<"and with length as"<<length<<"\n"; } }; class rectangles:public shape { point leftbottom,lefttop,rightbottom,righttop; public: rectangles(){} rectangles(point tleftbottom,point tlefttop,point trightbottom,right trighttop) { leftbottom=tleftbottom; lefttop=tlefttop; rightbottom=trightbottom;
35
righttop=trighttop; } void draw() { cout<<"rectangle is drawn at("<<leftbottom<<","<<rightbottom<<" and"<<"("<<lefttop<<","<<righttop<<")"; } }; class triangle:public shape { point avertex,bvertex,cvertex; public: triangle(){} triangle(point tavertex,point tbvertex,point tcvertex) { avertex=tavertex; bvertex=tbvertex; cvertex=tcvertex; } void draw() { cout<<"triangle is drawn at "<<avertex<<""<<bvertex<<""<<cvertex<<"\n"; } }; class circle:public shape { point center; int radius; public: circle(point tcenter,int tradius) { center=tcenter; radius=tradius; } void draw() { cout<<"circle is drawn at"<<"" ""<<center<<"" ""<<"and the radius is"<< radius<<"\n"; } }; class ellipse:public shape { point center; int radius; int angle; public:
36
ellipse(){} ellipse(point tcenter,int tradius,int tangle) { center=tcenter; radius=tradius; angle=tangle; } void draw() { cout<<"ellipse is drawn at"<<center<<"and the radius is"<<radius<<"with an angle"<<angle<<"/n"; } }; void main() { point p1(10,20); pointp2(3,2); square sq(p1,5); sq.draw(); rectangle rect(p1,p2,p1,p2); rect.draw(); circle c(p1,50); c.draw(); ellipse e(p2,34,23); e.draw(); triangle t(p1,p2,p1); t.draw(); shape *s; s=&sq; s->draw(); s=▭ s->draw(); s=&t; s->draw(); cout<<"object sq is"<<typeid(sq).name()<<"\n"; cout<<"object c is"<<typeid(c).name()<<"\n"; cout<<"object e is"<<typeid(e).name()<<"\n"; cout<<"object t is"<<typeid(t).name()<<"\n"; getch(); }
37
Output: Square is drawn at (10,20) an with length as 5 Rectangle is drawn at(10,20) (10,20) and (3,2) (3,2) circle is drawn at (10,20) and the radius is 50. Ellipse is drawn at (3,2) and the radius is 34with an angle is 23 Triangle is drawn at (10,20) (3,2) (10,20) Square is drawn at(10,20) and with length as 5 Rectangle is drawn at (10,20),(10,20)and (3,2) (3,2) Triangle is drawn at (10,20) (3,2) (10,20) Object sq is class square Object c is class circle Object e is class ellipse
Result: Thus the c++ program dynamic polymorphism and RTTI has been was executed successfully and the output was verified.
38
RANDOMLY GENERATES COMPLEX NUMBERS Ex.No:10 Date: Aim: A c++ program that randomly generates complex numbers. Algorithm: Step 1:start the program execution Step 2: create a two file Step 3: one file to read and another file to write Step 4: do the operation in one file and write the result in another file Step 5: display the result Step 6: stop the program execution. Program: #include<iostream.h> #include<conio.h> #include<fstream.h> class complex { public: float real,imag; complex() { real=imag=0,0; } void write(ofstream &os) { os.write((char*)&real,sizeof(real)); os.write((char*)&imag,sizeof(imag)); } int read(ifstream &is) { is.read((char*)&real,sizeof(real)); is.read((char*)&imag,sizeof(imag)); return is.good(); }
39
friend ostream &operator<<(ostream &os,complex &c); friend istream &operator<<(istream &is,complex &c); friend ofstream &operator<<(ofstream &fos,complex &c) { c.write(fos); return fos; } friend ifstream &operator>>(ifstream &fos,complex &c) { c.read(fos); return fos; } friend void add(complex,complex); }; istream &operator>>(istream &is,complex&c) { cout<<"real?"; is>>ws>>c.real; cout<<"imag?"; is>>ws>>c.imag; return is; } ostream &operator<<(ostream &os,complex &c) { os<<c.real<<"+i"<<c.imag<<"\t"; return os; } void add(complex c1,complex c2) { cout<<"\n result is"<<endl; cout<<(c1.real+c2.real)<<"+i"<<(c1.imag+c2.imag); } void main() { complex p_obj; clrscr(); ofstream ofile("complex.txt:",ios::out); char ch; cout<<"\n enter two complex numbers"<<endl; int i=0; do { cin>>p_obj; ofile<<p_obj; i++; }
40
while(i!=2); ofile.close(); ifstream ifile("complex.txt",ios::in); cout<<"\n objects wruttten to the file were.."<<endl; complex c1,c2; i=0; while(1) { ifile>>p_obj; if(i==0) c1=p_obj; else c2=p_obj; i++; if(ifile.fail()) break; cout<<p_obj; } add(c1,c2); getch(); } Output: Enter 2 complex nos Real?1 Real?2 Real?3 Image?4 Objects written to the file were 1+i2 3+i4 Result is 4+i6 Result: Thus the c++ program randomly generates complex has been was executed successfully and the output was verified.
41