1. Design C++ classes with static members, methods with default arguments, friend functions. (For example, design matrix and vector classes with static allocation, and a friend function to do matrixvector multiplication) #include<iostream.h> #include<conio.h> class vector; //class declaration – needed, because this class referred before it is defined in the friend function. class matrix { int m[3][3]; public: void getmatrix(void); void dismatrix(void); friend void multiply(matrix &, vector &); }; class vector { int v[10]; public: //default argument is 3 void getvector(int n=3); void disvector(void); friend void multiply(matrix &, vector &); }; void vector::getvector(int n) { int i; cout<<"elements for vector one by one..."; for(i=0;i<n;i++) cin>>v[i]; } void vector::disvector() { int i; cout<<"vector elements are..."; for(i=0;i<3;i++) cout<<v[i]<<""; } void matrix::getmatrix() { int i,j; cout<<"the matrix..."; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>m[i][j]; } void matrix::dismatrix() { int i, j;
cout<<"matrix is..."; for(i=0;i<3;i++) { for(j=0;j<3;j++) cout<<m[i][j]<<""; cout<<""; } } void multiply(matrix &m1, vector &v1) { int ans[3], i, j; cout<<"resultant matrix. ."; for(i=0;i<3;i++) { ans[i]=0; for(j=0;j<3;j++) ans[i]+=m1.m[i][j] * v1.v[j]; cout<<ans[i]<<""; } } void main() { matrix m1; vector v1; clrscr(); m1.getmatrix(); m1.dismatrix(); v1.getvector(); //no argument, default value wil be taken v1.disvector(); multiply(m1,v1); getch(); } 2. Implement complex number class with necessary operator overloadings and type conversions such as integer to complex, double to complex, complex to double etc. #include<iostream.h> #include<conio.h> #include<iomanip.h> class complex { private: float real; float imag; public: complex() { real=imag=0.0; }
insertion<int> i1; insertion<float> i2; clrscr(); cout<<"Insertion Sort on Integer Values..."; cout<<"Enter the size of array:"; cin>>n; i1.get(n); i1.sort(n); i1.display(n); cout<<"Insertion Sort on Float Values..."; cout<<"Enter the size of array:"; cin>>n; i2.get(n); i2.sort(n); i2.display(n); getch(); } 10. Design stack classes with necessary exception handling #include<iostream.h> #include<conio.h> class stk { int x,top,stack[10]; public: stk() { top=0; } void push(); void pop(); void display(); }; void stk::push() { if(top==10) cout<<"\nSTACK FULL"; else top=top+1; cout<<"\nENTER THE NUMBER TO BE PUSHED:"; cin>>x; stack[top]=x; cout<<"\nTHE VALUE IS PUSHED INTO THE STACK IS:"<<x; } void stk::pop() { if(top==0) cout<<"\nSTACK EMPTY\n:"; else
{ x=stack[top]; top=top-1; cout<<"\nTHE VALUE RETREIVED FROM STACK IS:"<<x; } } void stk::display() { cout<<"\nCONTENTS OF STACK:"; for(int i=top;i>0;i--) { cout<<"\n"<<stack[i]<<"\t"; } } void main() { clrscr(); stk s; int a; cout<<"\n\t\t\t*****\n"; cout<<"\n\t\t\tSTACK\n"; cout<<"\n\t\t\t*****\n"; do { cout<<"\n"<<"1.PUSH"<<"\n"<<"2.POP"<<"\n"<<"3.DISPLAY" <<"\n"<<"4.EXIT"; cout<<"\n\t\tENTER THE CHOICE:"; cin>>a; switch(a) { case 1: s.push(); break; case 2: s.pop(); break; case 3: s.display(); break; default: cout<<"\nINVALID CHOICE"; } } while(a<4); getch(); } 11. Design queue classes with necessary exception handling #include<iostream.h> #include<conio.h>
class queue { public: int q[5],front,rear,x,result; void enq(); void dque(); void disp(); queue() { front=0; rear=0; } }; void queue::enq() { if(rear>=5) cout<<"\nQueue overflow!!\n"; else { cout<<"\nEnter the number to be inserted: "; cin>>x; rear++; q[rear]=x; cout<<"\nNumber pushed in the queue:"<<q[rear]; } } void queue::dque() { if(rear==0) cout<<"\nQueue underflow!!\n"; else { if(front==rear) { front=0; rear=0; } else front++; } cout<<"\nDeleted element is:"; result=q[front]; cout<<result; } void queue::disp() { if(rear==0) cout<<"\nQueue underflow!!\n";
else cout<<"\nContents of queue is:"; for(int i=front+1;i<=rear;i++) cout<<q[i]<<"\t"; } void main() { int c; queue qu; clrscr(); cout<<"\n\t\t\t*****\n"; cout<<"\n\t\t\tQUEUE\n"; cout<<"\n\t\t\t*****\n"; do { cout<<"\n\n\n\t\t\t1.Insertion\n\t\t\t2.Deletion\n\t\t \t3.Display\n"; cout<<"\n\t\tEnter your choice:"; cin>>c; switch(c) { case 1: qu.enq(); break; case 2: qu.dque(); break; case 3: qu.disp(); break; default: cout<<"\nInvalid choice!!\n"; } } while(c<4); getch(); } 12. Define Point class and an Arc class. Define a Graph class which represents graph as a collection of Point objects and Arc objects. Write a method to find a minimum cost spanning tree in a graph. #include<iostream> using namespace std; #define MAX 50 #define TRUE 1 #define FALSE 0 #define MAXINT 250 class node { public: int no;
int clost[20],i,j,k,min; for(i=2;i<=n;i++) { lcost[i]=arcs[1][i].adj; clost[i]=1; } cout<<"Minimum cost spanning tree edges are:"; for(i=2;i<=n;++i) { min=lcost[2]; k=2; for(j=3;j<=n;++j) if(lcost[j]<min) { min=lcost[j]; k=j; } cout<<""<<k<<"<->"<<clost[k]; lcost[k]=MAXINT; for(j=2;j<=n;++j) if((arcs[k][j].adj<lcost[j])&&(lcost[j]<MAXINT)) { lcost[j]=arcs[k][j].adj; clost[j]=k; } } } }; int main() { int n; cout<<"total number of nodes..."; cin>>n; graph g(n); cout<<"number for each node..."; for (int i=1; i<=n; i++) g.nodes[i].no = i; char ch='y'; int w; do { node a, b; cout<<"Create path between the nodes.."; cout<<"the source node..."; cin>> a.no; cout<<"the destination node..."; cin>>b.no; cout<<"the weight"; cin>>w;
g.join(a,b,w); cout<<"to continue... [y]es [n]o"; cin>>ch; } while(ch=='y'); g.displayadj(n); g.shortpath(n); cin>>n; return 0; } 13.. Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square,Circle, Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic polymorphism and RTTI. #include<iostream.h> #include<conio.h> #include<graphics.h> class Point { public: int x; int y; Point(){} Point(int tempX, int tempY) { x=tempX; y=tempY; } int GetX() { return x; } int GetY() { return y; } friend ostream & operator<<(ostream & tempout, Point & tempPoint) { tempout<<"("<<tempPoint.GetX()<<","<<tempPoint.GetY()<<")"; return tempout; } }; class Shape { Point Position; 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 drwan at"<<LeftBottom<<"and with length as"<<Length<<"\n"; setcolor(14); rectangle(LeftBottom.GetX(),LeftBottom.GetY(),LeftBottom.GetX()+Length,Lef tBottom.GetY()+Length); } }; class Rectangles : public Shape { Point LeftBottom, LeftTop, RightBottom, RightTop; public: Rectangles(){} Rectangles(Point tLeftBottom, Point tLeftTop, Point tRightBottom, Point tRightTop) { LeftBottom=tLeftBottom; LeftTop=tLeftTop; RightBottom=tRightBottom; RightTop=tRightTop; } void draw() { cout<<"Rectangle is drwan at("<<LeftBottom<<","<<RightBottom<<")"<<"and"<<")"<<LeftTop<<","<<RightTop< <")\n"; setcolor(4); rectangle(LeftBottom.GetX(),LeftBottom.GetY(),RightTop.GetX(),RightTop.Get Y()); } }; class Circle : public Shape { Point Center;
int Radius; public: Circle(){} Circle(Point tCenter, int tRadius) { Center=tCenter; Radius=tRadius; } void draw() { cout<<"Circle is drawn at"<<" "<<Center<<" "<<"and the radius is"<<Radius<<"\n"; setcolor(5); circle(Center.GetX(),Center.GetY(),Radius); } }; int main() { clrscr(); int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "D:/Tc/BGI"); Point p1(100,200); Point p2(50,50); Point p3(100,300); Square sq(p1,50); Rectangles rect(p1,p2,p1,p2); Circle c(p3,50); Shape*s; s=&sq; s->draw();getch(); s=▭s->draw();getch(); s=&c; s->draw();getch(); return 0; } 14. Write a C++ program that randomly generates complex numbers and writes them two per line in a file along with an operator (+, -, *, or /). The numbers are written to file in the format (a + ib). Write another program to read one line at a time from this file, perform the corresponding operation on the two complex numbers read, and write the result to another file (one per line). #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdlib.h> #include<time.h> class complex { public: int real; int imag; complex(int r,int i) {