Univ

Published on December 2016 | Categories: Documents | Downloads: 59 | Comments: 0 | Views: 508
of 27
Download PDF   Embed   Report

Comments

Content

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; }

complex(int r, int i) { real = r; imag = i; } complex(double r, double i) { real = r; imag = i; } friend istream& operator>>(istream &, complex &); friend ostream& operator<<(ostream &, complex &); complex operator+(complex); complex operator-(complex); complex operator*(complex); complex operator/(complex); friend double condou(complex t); }; double condou(complex t) { return t.real; } istream& operator >>(istream &in, complex &c) { cout<<"Part:"; in>>c.real; cout<<"Imag Part:"; in>>c.imag; return in; } ostream& operator<<(ostream &out, complex &c) { if (c.imag<0) out<<c.real<<c.imag<<"i"; else out<<c.real<<"+"<<c.imag<<"i"; return out; } complex complex::operator+(complex c) { complex temp; temp.real = real+c.real; temp.imag = imag+c.imag; return temp; } complex complex::operator-(complex c) { complex temp; temp.real = real-c.real;

temp.imag = imag-c.imag; return temp; } complex complex::operator*(complex c) { complex temp; temp.real = real*c.real-imag*c.imag; temp.imag = real*c.imag+imag*c.real; return temp; } complex complex::operator/(complex c) { complex temp; float qt; qt = c.real*c.real+c.imag*c.imag; temp.real = (real*c.real+imag*c.imag)/qt; temp.imag = (imag*c.real-real*c.imag)/qt; return temp; } void main() { complex c1, c2, c3,c4(4,9),c5(3.23004,4.666304444); double t; clrscr(); t=condou(c5); cout<<"complex number 1: "; cin>>c1; cout<<"complex number 2: "; cin>>c2; cout<<"complex numbers are:"; cout<<"1: "<<c1; cout<<"2: "<<c2; c3=c1+c2; cout<<"of addition is:"<<c3; c3=c1-c2; cout<<"of subtraction is:"<<c3; c3=c1*c2; cout<<"of multiplication is:"<<c3; c3=c1/c2; cout<<"of division is:"<<c3; cout<<"->complex:"<<c4; cout<<"->complex:"<<c5; cout<<"to double"<<t; getch(); } 3. Implement Matrix class with dynamic memory allocation and necessary methods. Give proper constructor, destructor, copy constructor, and overloading of assignment operator. #include<iostream.h>

#include<conio.h> class matrix { int **m; int row, col; public: matrix() { row=col=0; m=NULL; } matrix(int r ,int c); ~matrix(); void getmatrix(); void showmatrix(); matrix(matrix &m2); //copy constructor matrix& operator=(matrix &m2); }; matrix::~matrix() { for(int i=0;i<row;i++) delete m[i]; delete m; } matrix::matrix(int r ,int c) { row = r; col = c; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; } matrix::matrix(matrix &m2) { cout<<"constructor invoked..."; row = m2.row; col = m2.col; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++) for(int j=0;j<row;j++) m[i][j]=m2.m[i][j]; } matrix& matrix::operator=(matrix &m2) { cout<<"Operator Overloading..."; row = m2.row;

col = m2.col; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++) for(int j=0;j<row;j++) m[i][j]=m2.m[i][j]; return *this; } void matrix::getmatrix() { for(int i=0;i<row;i++) for(int j=0; j<col; j++) cin>>m[i][j]; } void matrix::showmatrix() { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) cout<<""<<m[i][j]; cout<<""; } } void main() { int r,c; clrscr(); cout<<"rows and cols of the matrix..."; cin>>r>>c; matrix m1(r,c); cout<<"the matrix elements one by one..."; m1.getmatrix(); cout<<"matrix is..."; m1.showmatrix(); //invoking copy constructor matrix m2=m1; cout<<"of copy constructor is..."; m2.showmatrix(); matrix m3; m3=m1; cout<<"of assignment operator overloading..."; m3.showmatrix(); getch(); }

4. Overload the new and delete operators to provide custom dynamic allocation of memory. #include<iostream.h>

#include<conio.h> #include<stdlib.h> class vector { private:int *array; public: void *operator new(size_t size) { void *v; cout<<"new invoked..."; v=malloc(size); if(!v) { cout<<"Unable to allocate memory"; exit(0); }return v; } void operator delete(void* v) { cout<<"delete invoked..."; free (v); } void read(int); int max(int); int sum(int); }; void vector::read(int s) { for(int i=0; i<s; i++) { cout<<"element "<<i+1<<":"; cin>>array[i]; } } int vector::sum(int s) { int tot=0; for(int i=0; i<s; i++) tot+=array[i]; return tot; } int vector::max(int s) { int max=0; for(int i=0;i<s;i++) if(array[i]>max) max = array[i]; return max; } void main()

{ int s; clrscr(); cout<<"how many elements..."; cin >> s; vector *vec = new vector; cout<<"Enter vector data..."; vec->read(s); cout<<"max is..."<<vec->max(s); cout<<"sum is..."<<vec->sum(s); delete vec; getch(); } 5. Develop a template of linked-list class and its methods. #include<iostream.h> #include<conio.h> #include<stdlib.h> template <class type> struct node { type data; node* next; }; template<class type> class list { public: list(); int length(void) const; void makeempty(void); void insert(void); void remove(void); void display(void); private:node<type>* linklist; int count; }; template <class type> void list<type>::display(void) { node<type>* cur = linklist; cout<<"linked list is..."; while(cur!=NULL) { cout<<cur->data<<"->"; cur=cur->next; }cout<<"NULL"; }

template<class type> list<type>::list() { count=0; linklist=NULL; } template<class type> int list<type>::length(void) const { return count; } template <class type> void list<type>::makeempty(void) { node<type>* temp; while(linklist !=NULL) { temp=linklist; linklist=linklist->next; delete temp; }count=0; cout<<"List is empty..."; } template <class type> void list<type>::insert(void) { node<type>* temp; type item; cout<<"the item to insert. ."; cin>>item; temp=new node<type>; temp->data = item; temp->next = linklist; linklist=temp; count++; } template<class type> void list<type>::remove(void) { node<type>* cur = linklist; type item; cout<<"the item to remove..."; cin>>item; node<type>* temp; if(item==linklist->data) { temp = cur; linklist=linklist->next;

} else { while(!(item==(cur->next->data))) cur=cur->next; temp = cur->next; cur->next = (cur->next)->next; } delete temp; count--; } void main() { int ch; list<int> list1; clrscr(); while(1) { cout<<"Single Linked List - Menu"; cout<<"1.Insert .Delete3.Empty4.Exit"; cout<<"your Choice... "; cin>>ch; switch(ch) { case 1:list1.insert(); list1.display(); break; case 2:if(list1.length()>0) { list1.remove(); list1.display(); }else cout<<"Empty"; break; case 3:list1.makeempty(); break; case 4:exit(0); default:cout<<"Choice"; } } } 6. Develop templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort, and quick sort. Merge Sort #include<iostream.h> #include<conio.h> #include<iomanip.h> template <class t>

class sort { t a[10]; public: void get(int); void merge(int,int); void mergesort(int,int,int); void display(int); }; template <class t> void sort <t>::get(int n) { int i; cout<<"Enter the array elements:"; for(i=1;i<=n;i++) cin>>a[i]; } template <class t> void sort <t>::display(int n) { int i; cout<<"The sorted array is"; for(i=1;i<=n;i++) cout<<a[i]<<setw(5); } template <class t> void sort <t>::merge(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; merge(low,mid); merge(mid+1,high); mergesort(low,mid,high); } } template <class t> void sort<t>::mergesort(int low,int mid,int high) { t b[10]; int h,i,j,k; h=low; i=low; j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) {

b[i]=a[h]; h=h+1; }else { b[i]=a[j]; j=j+1; }i=i+1; }if(h>mid) { for(k=j;k<=high;k++) { b[i]=a[k]; i=i+1; } }else { for(k=h;k<=mid;k++) { b[i]=a[k]; i=i+1; } }for(k=low;k<=high;k++) a[k]=b[k]; } void main() { int n; clrscr(); cout<<"MERGE SORT USING TEMPLATES"; cout<<"\n"; sort<int>n1; sort<float>n2; cout<<"Enter the array size:"; cin>>n; n1.get(n); n1.merge(1,n); n1.display(n); n2.get(n); n2.merge(1,n); n2.display(n); getch(); } 7.Quick Sort #include<iostream.h> #include<conio.h> template <class w> class quick {

w a[50]; int n; public: void get(); void sort(int,int); int partition(int,int); void put(); }; template <class w> void quick <w>::get() { int i; cout<<"Enter the no of terms:"; cin>>n; cout<<"Enter the values:"; for(i=1;i<=n;i++) cin>>a[i]; sort(1,n); } template <class w> void quick <w>::sort(int p,int q) { int j; if(p<q) { j=partition(p,q+1); sort(p,j-1); sort(j+1,q); } } template <class w> int quick <w>: partition(int m,int p) { int i,j,t; w v; v=a[m]; i=m;j=p; do { do i++; while(a[i]<v); do j--; while(a[j]>v); if(i<j) { t=a[i]; a[i]=a[j];

a[j]=t; } }while(i<j); a[m]=a[j]; a[j]=v; return j; } template <class w> void quick<w>::put() { int i; for(i=1;i<=n;i++) cout<<a[i]<<" "; }void main() { clrscr(); quick<int>q1; quick<float>q2; cout<<"QUICK SORT USING TEMPLATES"; cout<<"\n"; q1.get(); cout<<"Sorted array of integer values:-"; q1.put(); q2.get(); cout<<"Sorted array of floating values:-"; q2.put(); getch(); } 8.Bubble Sort #include<iostream.h> #include<iomanip.h> #include<conio.h> template <class t> class bubble { t a[25]; public: void get(int); void sort(int); void display(int); }; template <class t> void bubble <t>::get(int n) { int i; cout<<"the array elements:"; for(i=0; i<n;i++) cin>>a[i]; }

template <class t> void bubble <t>::display(int n) { int i; cout<<"The sorted array is..."; for(i=0;i<n;i++) cout<<a[i]<<setw(10); } template <class t> void bubble <t>::sort(int n) { int i,j; t temp; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } } void main() { int n; bubble<int> b1; bubble<float> b2; clrscr(); cout<<"Bubble Sort on Integer Values..."; cout<<"Enter the size of array:"; cin>>n; b1.get(n); b1.sort(n); b1.display(n); cout<<"Bubble Sort on Float Values..."; cout<<"Enter the size of array:"; cin>>n; b2.get(n); b2.sort(n); b2.display(n); getch(); } 9.Insertion Sort #include<iostream.h>

#include<iomanip.h> #include<conio.h> template <class t> class insertion { t a[25]; public: void get(int); void sort(int); void display(int); }; template <class t> void insertion<t>::get(int n) { int i; cout<<"the array elements:"; for(i=0; i<n;i++) cin>>a[i]; } template <class t> void insertion <t>::display(int n) { int i; cout<<"The sorted array is..."; for(i=0;i<n;i++) cout<<a[i]<<setw(10); } template <class t> void insertion <t>::sort(int n) { int i,j; t temp; for(i=1;i<n;i++) { j=i; while(j>=1) { if(a[j]<a[j-1]) { temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; }j--; } } } void main() { int n;

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;

node() {} node(int a) { no=a; } }; class arc { public: int adj; int weight; arc(){} arc(int a) { adj = a; } }; class graph { public: node nodes[MAX]; arc arcs[MAX][MAX]; graph(int n) { for(int i=1;i<=n;i++) { nodes[i].no=0; for(int j=1;j<=n;j++) arcs[i][j].adj=FALSE; } } void join(node n1, node n2, int w) { arcs[n1.no][n2.no].adj = w; arcs[n2.no][n1.no].adj=w; } void displayadj(int n) { cout<<"adjacency matrix..."; for(int i=1;i<=n;i++) { for(int j=1; j<=n; j++) cout<<""<<arcs[i][j].adj; cout<<endl; } cout<<endl; } void shortpath(int n) { int lcost[20];

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=&rect;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) {

real = r; imag = i; }complex() { real=imag=0; } void display(void); }; void complex::display(void) { cout<<real<<((imag<0)?"-i":"+i")<<imag<<""; } void main() { clrscr(); ofstream ocom("Complex.txt"); float real, imag; time_t ti; srand((unsigned) time(&ti)); real = rand()%100; imag = rand()%100; ocom<<"("<<real<<((imag<0)?"-i":"+i")<<imag<<")"<<"+"; real = rand()%100; imag = rand()%100; ocom<<"("<<real<<((imag<0)?"-i":"+i")<<imag<<")"<<""; ocom.close(); ifstream icom("Complex.txt"); char no,t, ch,op; icom>>no; icom>>real; icom>>ch; icom>>no; icom>>imag; imag=(ch=='+')?imag:-imag; icom>>no; icom>>op; complex a(real,imag); icom>>no; icom>>real; icom>>ch; icom>>no; icom>>imag; imag=(ch=='+')?imag:-imag; complex b(real,imag); //complex b is created complex c; switch(op) { case '+':c.real=a.real+b.real; c.imag=a.imag+b.imag;

break; case '-':c.real=a.real-b.real; c.imag=a.imag-b.imag; break; case '*':c.real = (a.real*b.real)-(a.imag*b.imag); c.imag = (a.real*b.imag)+(a.imag*b.real); break; case '/':float qt; qt = b.real*b.real+b.imag*b.imag; c.real = (a.real*b.real+a.imag*b.imag)/qt; c.imag = (a.imag*b.real-a.real*b.imag)/qt; break; default:cout<<"Operator"; } cout<<"1:"; a.display(); cout<<"2:"; b.display(); cout<<"Complex:"; c.display(); ofstream out("result.txt"); out<<"("<<c.real<<((c.imag<0)?"-i":"+i")<<c.imag<<")"; out.close(); }

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close