Vous êtes sur la page 1sur 82

Exercise 1.

Stack Using Array


#include<iostream.h> #include<conio.h> #include<ctype.h> const int MAX=5; class stack { int stack[10],top; public: stack() { top=-1; } void push(void); void pop(void); void disp(void); }; void stack::push(void) { int p; if(top==MAX-1) cout<<"\nStack is full"; else { cout<<"Enter the Value:"; cin>>p; top=top+1; stack[top]=p; } } void stack::pop(void) { if(top==-1) cout<<"Stack is empty"; else { cout<<"The deleted element is "<<stack[top]; top--; } }

void stack::disp(void) { if(top==-1) cout<<"Stack is empty\n"; else { cout<<"Items in the Stack are:\n"; for(int i=0;i<=top;i++) cout<<stack[i]<<"\t"; } } void main() { int ch; stack s; clrscr(); cout<<"\n\t\tStack using Array\n"; cout<<"\t1.Push\n"; cout<<"\t2.Pop\n"; cout<<"\t3.Display\n"; cout<<"\t4.Exit\n"; do { cout<<"\nEnter Your choice:"; cin>>ch; switch(ch) { case 1: s.push(); break; case 2: s.pop(); break; case 3: s.disp(); getch(); break; } }while(ch!=4); }

Output for Exercise 1:


Stack Using Array 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice : 1 Enter the Value: 10 Enter Your Choice:1 Enter the Value : 20 Enter Your Choice: 3 Items in the Stack are: 10 20 Enter Your Choice: 2 The deleted element is 20 Enter Your Choice: 4

Exercise 2. Stack Using Pointer


#include<iostream.h> #include<conio.h> #include<process.h> class stack { struct node { int data; node *next; }*top; public: stack() { top=NULL; } void push(); void pop(); void disp(); }; void stack::push() { node *tmp=new node; if(tmp==NULL) { cout<<"\nStack is full"; return; } else { cout<<"\n\t\tEnter the element to be inserted:"; cin>>tmp->data; tmp->next=top; top=tmp; } }

void stack::pop() { if(top==NULL) { cout<<"\tStack is empty"; return; } else { node *tmp=top; top=top->next; cout<<"\tThe element poped is:\n"<<tmp->data<<endl; delete tmp; } } void stack::disp() { node *tmp=top; if(top==NULL) { cout<<"\n\t\tStack is Empty\n\t"; return; } else { cout<<"\n\t\tStack Elements are:\n"; while(tmp!=NULL) { cout<<tmp->data<<endl; tmp=tmp->next; } } } void main() { stack s; int ch; clrscr(); cout<<"\n\t\tStack Using Pointers"; cout<<"\n\t\t1.Push"<<"\n\t\t2.Pop"<<"\n\t\t3.Display\n"; do { cout<<"\n\tEnter your choice:"; cin>>ch;

switch(ch) { case 1: s.push(); break; case 2: s.pop(); break; case 3: s.disp(); getch(); break; } }while(ch!=4); }

Output for Exercise 2:


Stack Using Pointers 1.Push 2.Pop 3.Display Enter Your Choice: 1 Enter the element to be inserted: 10 Enter Your Choice: 1 Enter the element to be inserted : 20 Enter Your choice: 3 Stack Elemts are: 20 10 Enter Your Choice: 2 The element poped is : 20 Enter Your Choice : 4

Exercise 3.Queue Using Array


#include<iostream.h> #include<conio.h> const int MAX=3; class queue { int queue[MAX]; int rear,front; public: queue() { rear=front=-1; } void insert(); void del(); void disp(); }; void queue::insert() { int item; if(rear==MAX-1) cout<<"\n queue is overflow"; else { if(front==-1) front=0; cout<<"\nEnter the item:"; cin>>item; rear++; queue[rear]=item; } } void queue::del() { if((front==-1)||(front>rear)) { cout<<"\nqueue underflow"; return; } else

{ cout<<"the deleted item is:"<<queue[front]; front=front+1; } } void queue::disp() { if(front==-1) cout<<"\nqueue is empty"; else { cout<<"\nqueue is:\n"; for(int i=front;i<=rear;i++) cout<<queue[i]<<endl; } } void main() { queue q; int ch; clrscr(); cout<<"\nQueue Using Array"; cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit\n"; do { cout<<"\n Enter Your Choice:"; cin>>ch; switch(ch) { case 1: q.insert(); break; case 2: q.del(); break; case 3: q.disp(); break; } }while(ch!=4); }

Output for Exercise 3:


Queue Using Array 1.Insert 2.Delete 3.Display 4.Exit Enter Your Choice: 1 Enter the item: 10 Enter Your Choice: 1 Enter the item: 20 Enter Your Choice:3 Queue is: 10 20 Enter Your Choice: 2 The deleted item is : 10 Enter Your Choice: 4

Exercise 4.Queue Using Pointer


#include<iostream.h> #include<conio.h> class queuep { struct node { int item; node *next; }*front,*rear; public: queuep() { front=rear=NULL; } void insert(); void del(); void disp(); }; void queuep::insert() { node *tmp=new node; cout<<"\n Input the element for Adding in queue:"; cin>>tmp->item; tmp->next=NULL; if(front==NULL) front=tmp; else rear->next=tmp; rear=tmp; } void queuep::del() { node *tmp; if(front==NULL) cout<<"\nqueue is underflow"; else { tmp=front; cout<<"\nDeleted element is:"<<tmp->item<<endl; front=front->next; delete(tmp);

} } void queuep::disp() { node *tmp =front; if(front==NULL) cout<<"\nQueue is empty"; else { cout<<"Queue elements:\n"; while(tmp!=NULL) { cout<<tmp->item<<endl; tmp=tmp->next; } } } void main() { queuep p; int ch; clrscr(); cout<<"\nQueue using Pointer"; cout<<"\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n"; do { cout<<"\n\nEnter your choice:"; cin>>ch; switch(ch) { case 1: p.insert(); break; case 2: p.del(); break; case 3: p.disp(); break; } }while(ch!=4); }

Output for Exercise 4:


Queue Using Pointer 1.Insert 2.Delete 3.Display 4.Exit Enter Your choice: 1 Input the element for Adding in queue: 10 Enter your choice: 1 Input the element for Adding in queue: 20 Enter Your choice: 3 Queue elements: 10 20 Enter your choice: 2 Deleted element is: 10 Enter your choice: 4

Exercise 5. Infix to Postfix Conversion


#include<iostream.h> #include<conio.h> #include<math.h> #include<ctype.h> class infix { char str[25],stack[25],item; int top; public: infix() { top=0; stack[0]=&; } void input(); void inpost(); int ipriority(char); int spriority(char); }; void infix::inpost() { int I=0; while(str[I]!=#) { item=str[I]; if(isalpha(item)) cout<<item; else if(item==)) { while(stack[top]!=() { cout<<stack[top]; top--; } top--; }

else { while(ipriority(item)<=spriority(stack[top])) { cout<<stack[top]; top--; } top++; stack[top=item; } I++; } while(top!=0) { cout<<stack[top]; top--; } } int infix::ipriority(char e) { char pr; switch(e) { case (: pr=4; break; case /: case *: pr=3; break; case +: case -: pr=2; break; } return(pr); }

int infix::spriority(char e) { char pr; switch(e) { case (: pr=0; break; case /: case *: pr=3; break; case +: case -: pr=2; break; case &: pr=-1; break; } return(pr); } void infix::input() { cout<<Enter a string and terminate it with #\n; cin>>str; } void main() { infix s; clrscr(); cout<<\n\t\tInfix To Postfix Conversion\n; s.input(); s.inpost(); getch(); }

Output for Exercise 5:


Infix To Postfix Conversion Enter a string and terminate it with #: a/b-c+d*e-a*c# Postfix Form : ab/c-dc*+ac*-

Exercise 6. Postfix Evaluation


#include<iostream.h> #include<conio.h> class postfix { char str[25],item; int top,item1,item2,stack[10]; public: postfix() { top=0; } void input(); void post(); int calc(int,int,char); }; void postfix::post() { int I=0,ch; while(str[I]!='#') { item=str[I]; if((item>47)&&(item<58)) { top++; ch=item-48; stack[top]=ch; } else { item1=stack[top]; top--; item2=stack[top]; stack[top]=calc(item2,item1,str[I]); } I++; } cout<<stack[top]; }

int postfix::calc(int x,int y,char op) { int s; switch(op) { case '-': s=x-y; break; case '+': s=x+y; break; case '*': s=x*y; break; case '/': s=x/y; break; } return s; } void postfix::input() { cout<<"\n\t\tEnter the values and terminate it with #\n"; cin>>str; } void main() { postfix p; clrscr(); cout<<"\n\t\tPostfix Evaluation\n"; p.input(); cout<<"\n\tResult of the exprexxion is:"; p.post(); getch(); }

Output for Exercise 6:


Postfix Evaluation Enter the values and terminate it with #: 10 # Result of the expression is: 26899

Exercise 7: POLYNOMIAL ADDITION USING ARRAY


#include<iostream.h> #include<conio.h> class poly { int I,n,coeff[20],expo[20]; public: void getdata(); void display(); poly operator+(poly); }; void poly::getdata() { cin>>n; for(I=1;I<=n;I++) { cout<<"\nenter the coefficient:"; cin>>coeff[I]; cout<<"\nenter the exponet:"; cin>>expo[I]; } } void poly::display() { for(I=0;I<=n;I++) { if(expo[I]!=0) { if(coeff[I]!=0) cout<<coeff[I]<<"X^"<<expo[I]<<"+"; else cout<<coeff[I]<<"X^"<<expo[I]; } else { if(coeff[I]!=0) cout<<coeff[I]; else cout<<coeff[I]; } } }

poly poly::operator+(poly p) { poly p3; I=1; int j=1; int k=0; while(I<=n&&j<=p.n) { if(expo[I]==p.expo[j]) { k++; p3.coeff[k]=coeff[I]+p.coeff[j]; p3.expo[k]=expo[I]; I++; j++; } else if(expo[I]>p.expo[j]) { k++; p3.coeff[k]=coeff[I]; p3.expo[k]=expo[I]; I++; } else { k++; p3.coeff[k]=coeff[j]; p3.expo[k]=expo[j]; j++; } } while(I<=n) { k++; p3.coeff[k]=coeff[I]; p3.expo[k]=expo[I]; I++; }

while(j<=p.n) { k++; p3.coeff[k]=coeff[j]; p3.expo[k]=expo[j]; j++; } p3.n=k; return(p3); } void main() { poly p1,p2,p3; clrscr(); cout<"\n\tpolynomial addition using operator overloading"; cout<<"\nenter the no.of terms of 1st polynomial:"; p1.getdata(); cout<<"\nenter the no.of terms of 2nd polynomial:"; p2.getdata(); p3=p1+p2; cout<<"\nlst polynomial:"; p1.display(); cout<<"\n2nd polynomial:"; p2.display(); cout<<"\nresulant polynomial is:"; p3.display(); getch(); }

Output for Exercise 7:


Polynomial Addition Using Operator Overloading
Enter the 1st Polynomial Enter the Coefficient:3 Enter the Exponent:2 Enter the Coefficient:2 Enter the Exponent:1 Enter the Coefficient:4 Enter the Exponent:0 Enter the 2nd Polynomial Enter the Coefficient:4 Enter the Exponent:1 Enter the Coefficient:5 Enter the Exponent:0 The 1st Polynomial is: 3x^2+2x^1+4 The 2nd Polynomial is: 4x^1+5 The Addition of two Polynomial is: 3x^2+6x^1+9

Exercise 8:POLYNOMIAL ADDITION USING POINTER


#include<iostream.h> #include<conio.h> class poly { struct node { int coeff,exp; struct node*next; }*first,*tmp,*last; public: void getdata(int); void createnode(int,int); poly() { first=NULL; } void putdata(); poly operator+(poly b); }; void poly::createnode(int c,int exp) { if(first==NULL) { first=new node; first->coeff=c; first->exp=exp; first->next=NULL; last=first; } else { tmp=new node; tmp->coeff=c; tmp->exp=exp; tmp->next=NULL; last->next=tmp; last=tmp; } } void poly::getdata(int n) { int c,e,i; for(i=0;i<n;i++) {

cout<<"\n\nEnter coefficient"; cin>>c; cout<<"\nEnter exponent"; cin>>e; createnode(c,e); } } poly poly::operator+(poly p) { poly tem; node*a,*b; int co,d; a=first; b=p.first; while(a!=NULL&&b!=NULL) { if(a->exp==b->exp) { co=a->coeff+b->coeff; d=a->exp; tem.createnode(co,d); a=a->next; b=b->next; } else if(a->exp>b->exp) { co=a->coeff; d=a->exp; tem.createnode(co,d); a=a->next; }

else if(b->exp>a->exp) { co=b->coeff; d=b->exp; } } while(a!=NULL) { co=a->coeff; d=a->exp; tem.createnode(co,d);

a=a->next; } while(b!=NULL) { co=b->coeff; d=b->exp; tem.createnode(co,d); b=b->next; } return(tem); } void poly::putdata() { node*a; for(a=first;a!=NULL;a=a->next) { if(a==first) cout<<a->coeff<<"x^"<<a->exp; else if(a>0) cout<<"+"<<a->coeff<<"x^"<<a->exp; else cout<<a->coeff<<"x^"<<a->exp; } } void main() { clrscr(); poly p1,p2,p3; int m,n; cout<<"\nenter the no of terms of 1st polynomial and 2nd polynomial\n"; cin>>m>>n; cout<<"\nenter the 1st polynomial"; p1.getdata(m); cout<<"\nenter the 2nd polynomial"; p2.getdata(n); p3=p1+p2; cout<<"\nenter the 1st polynomial is:"; p1.putdata(); cout<<"\nenter the 2nd polynomial is:"; p2.putdata(); cout<<"\nthe addition of two polynomial is:"; p3.putdata(); getch(); }

Output for Exercise 8:


Enter the No of terms of 1st Polynomial And 2nd Polynomial 2 2 Enter the 1st Polynomial Enter the Coefficient:2 Enter the Exponent:1 Enter the Coefficient:2 Enter the Exponent:0 Enter the 2nd Polynomial Enter the Coefficient:3 Enter the Exponent:1 Enter the Coefficient:2 Enter the Exponent:0 The 1st Polynomial is: 2x^1+2x^0 The 2nd Polynomial is: 3x^1+2x^0 The Addition of two Polynomial is: 5x^14x^0

Exercise 9.single linked list


#include<iostream.h> #include<conio.h> #include<process.h> class slink { struct node { int info; struct node *link; }*start; public: slink() { start=NULL; } void createlist(); void addatbeg(); void addinbet(); void del(); void disp(); }; void slink::createlist() { node *q,*tmp; tmp=new node; cout<<"\nEnter the element:"; cin>>tmp->info; tmp->link=NULL; if(start==NULL) start=tmp; else { q=start; while(q->link!=NULL) { q=q->link; } q->link=tmp; } }

void slink::addatbeg() { node *tmp=new node; cout<<"\n Enter the element:"; cin>>tmp->info; tmp->link=start; start=tmp; } void slink::addinbet() { node *tmp,*q; int pos,i; cout<<"\n Enter the position after which this element is inserted:"; cin>>pos; q=start; for(i=0;i<pos-1;i++) { q=q->link; if(q==NULL) { cout<<"\n There are less than "<<pos<<"elements"; return; } } tmp=new node; cout<<"\n Enter the element:"; cin>>tmp->info; tmp->link=q->link; q->link=tmp; } void slink::del() { int data; node *tmp,*q; if(start==NULL) { cout<<"\n List is empty"; return; } cout<<"\n Enter the element:"; cin>>data; if(start->info==data) { tmp=start; start=start->link; delete(tmp);

} q=start; while(q->link->link!=NULL) { if(q->link->info==data) { tmp=q->link; q->link=tmp->link; delete(tmp); return; } q=q->link; } if(q->link->info==data) { tmp=q->link; delete(tmp); q->link==NULL; return; } cout<<"\n Element"<<data<<"not found"; } void slink::disp() { node *q; if(start==NULL) { cout<<"\n List is empty"; return; } q=start; cout<<"\nList is:"; while(q!=NULL) { cout<<q->info; q=q->link; } }

void main() { slink l; int ch; clrscr(); cout<<"\n\n*******Singly Linked List********"; cout<<"\n1.CREATE LIST \n2.ADD AT BEGINNING \n3.ADD IN BETWEEN \n4.DELETE \n5.DISP \n6.EXIT"; do { cout<<"\n\nEnter your choice:"; cin>>ch; switch(ch) { case 1: l.createlist(); break; case 2: l.addatbeg(); break; case 3: l.addinbet(); break; case 4: l.del(); break; case 5: l.disp(); break; } } while(ch!=6); }

Output for Exercise 9:


Single Linked List 1.CREATE LIST 2.ADD AT BEGINNING 3,ADD IN BETWEEN 4.DELETE 5.DISP 6.EXIT Enter your choice: 1 Enter the element: 10 Enter your choice: 2 Enter the element: 3 Enter your choice: 5 List is : 310 Enter your choice: 3 Enter the position after which this element is inserted: 2 Enter the element:2 Enter your choice: 5 List is : 3102 Enter your choice: 6

Exercise 10.Double Linked List


#include<iostream.h> #include<conio.h> #include<process.h> class doublelink { struct node { struct node *prev; int info; struct node *next; }*start; public: doublelink() { start=NULL; } void createlist(); void addatbeg(); void addinbet(); void del(); void disp(); }; void doublelink::createlist() { node *q,*tmp; tmp=new node; cout<<"\nEnter the data:"; cin>>tmp->info; tmp->next=NULL; if(start==NULL) { tmp->prev=NULL; start=tmp; } else { q=start; while(q->next!=NULL) { q=q->next; } q->next=tmp; tmp->prev=q; }

} void doublelink::addatbeg() { node *tmp=new node; cout<<"\nEnter the data:"; cin>>tmp->info; tmp->prev=NULL; tmp->next=start; start->prev=tmp; start=tmp; } void doublelink::addinbet() { node *tmp,*q; int i,p; cout<<"\nEnter the Position:"; cin>>p; q=start; for(i=0;i<p-1;i++) { q=q->next; if(q==NULL) { cout<<"\nThere are less than"<<p<<"elements"; return; } } tmp=new node; cout<<"\nEnter the data:"; cin>>tmp->info; q->next->prev=tmp; tmp->next=q->next; tmp->prev=q; q->next=tmp; } void doublelink::del() { node *tmp,*q; int num; cout<<"\nEnter the deleted element:"; cin>>num; if(start->info==num) { tmp=start; start=start->next; start->prev=NULL;

delete(tmp); return; } q=start; while(q->next->next!=NULL) { if(q->next->info==num) { tmp=q->next; q->next=tmp->next; tmp->next->prev=q; delete(tmp); return; } q=q->next; } if(q->next->info==num) { tmp=q->next; delete(tmp); q->next=NULL; return; } cout<<"\Element"<<num<<"not found"; } void doublelink::disp() { struct node *q; if(start==NULL) { cout<<"\nList is Empty"; return; } q=start; cout<<"\nList is:"; while(q!=NULL) { cout<<q->info<<"\t"; q=q->next; } }

void main() { doublelink d; int ch; clrscr(); cout<<"\n******DOUBLE LINKED LIST*****"; cout<<"\n1.CREATELIST \n2.ADDATBEGINNING \n3.ADDINBETWEEN \n4.DELETE \n5.DISPLAY \n6.EXIT"; do { cout<<"\nEnter Your choice:"; cin>>ch; switch(ch) { case 1: d.createlist(); break; case 2: d.addatbeg(); break; case 3: d.addinbet(); break; case 4: d.del(); break; case 5: d.disp(); break; } }while(ch!=6); }

Output for Exercise 10:


Double Linked List 1.CREATE LIST 2.ADD AT BEGINNING 3,ADD IN BETWEEN 4.DELETE 5.DISP 6.EXIT Enter your choice: 1 Enter the data: 10 Enter your choice: 2 Enter the data: 3 Enter your choice: 5 List is : 3 10 Enter your choice: 3 Enter the position:1 Enter the data: 2 Enter your choice: 5 List is : 3 2 10 Enter your choice: 4 Enter the deleted element: 2 Enter your choice: 5 List is : 3 10

Exercise 11. Array of Class Concepts:#include<iostream.h> #include<conio.h> class employee { public: char name[30]; int salary; void getdata(); void putdata(); }; void employee::getdata() { cout<<"Enter the Employee name\n"; cin>>name; cout<<"Enter the Employee Salary\n"; cin>>salary; } void employee::putdata() { cout<<"\nEmployee Name is : "<<name<<endl; cout<<"Employee Salary is : "<<salary<<endl; } const int size=3; void main() { employee emp[size]; int i; clrscr(); cout<<"\n\t\t\t--------------------------"; cout<<"\n\t\t\tEnter the Employee Details"; cout<<"\n\t\t\t--------------------------"; for(i=1;i<=3;i++) { cout<<"\nEnter the "<<i<<" Employee Details\n"; emp[i].getdata(); cout<<endl; }

clrscr(); cout<<"\n\t\t\t--------------------"; cout<<"\n\t\t\tThe Employee Details"; cout<<"\n\t\t\t--------------------"; for(i=1;i<=3;i++) { cout<<"\n"; cout<<i<<" Employee Details are\n"; emp[i].putdata(); cout<<endl; } getch(); }

Output for Exercise 11:


Enter the 1 Employee Details Enter the Employee Name : Krishnan Enter the Employee salary: 1000 Enter the 2 Employee Details Enter the Employee Name: Rahuram Enter the Employee Salary:1000 Enter the 3 Employee Details Enter the Employee Name: Jayanthi Nathan Enter the Employee Salary:1000

Exercise 12. Single Inheritance:#include<iostream.h> #include<conio.h> class base { protected: int rollno,m1,m2,m3,total; char name[20]; float avg; public: void input() { Cout<<\n***Single Inheritance***:; cout<<"\nenter the rollno:"; cin>>rollno; cout<<"enter the name:"; cin>>name; cout<<"enter the mark1:"; cin>>m1; cout<<"enter the mark2:"; cin>>m2; cout<<"enter the mark3:"; cin>>m3; } void output() { total=m1+m2+m3; avg=total/3; cout<<"\nrollno:"<<rollno; cout<<"\nname:"<<name; cout<<\nMark Details are:; cout<<"\nmark1:"<<m1; cout<<"\nmark2:"<<m2; cout<<"\nmark3:"<<m3; cout<<"\ntotal:"<<total; cout<<"\navg:"<<avg; } };

class derived: public base { public: void getdata() { input(); } void putdata() { output(); } }; void main() { clrscr(); derived d; d.getdata(); d.putdata(); getch(); }

Output for Exercise 12:


****Single Inheritance***** Enter the rollno: 101 Enter the Name: Ramachandran Enter the Mark1:100 Enter the Mark2:100 Enter the Mark3:100 Rollno:101 Name:Ramachandran Mark Details are.. Mark1:100 Mark1:100 Mark1:100 Total:300 Avg:100

Exercise 13.Multiple Inheritance


#include<iostream.h> #include<conio.h> class base { public: char name[20]; void ge() { cout<<"\nEnter the Name:"; cin>>name; } }; class base1 { public: int rollno; void get() { cout<<"\nEnter the Rollno:"; cin>>rollno; } }; class base2 { public: int mark1,mark2,mark3; void input() { cout<<"\nEnter the Three Marks are:"; cin>>mark1>>mark2>>mark3; } }; class derived: public base,base1,base2 { public: void output() { int total; float avg; total=mark1+mark2+mark3; avg=total/3; cout<<"\nName is:"<<name; cout<<"\nRollno is:"<<rollno; cout<<"\nMarks Are:"<<mark1<<"\t"<<mark2<<"\t"<<mark3;

cout<<"\nTotal is:"<<total; cout<<"\nAvg is:"<<avg; } void display() { base::ge(); base1::get(); input(); output(); } }; void main() { derived d; clrscr(); d.display(); getch(); }

Output for Exercise 13:


Enter the Name: Krishnan Enter the Roll no: 948 Enter the Three Marks are: 100 100 100 Name is: Krishnan Roll no is: 948 Marks are: 100 100 100 Total is : 300 Avg is : 100

Exercise 14. Multilevel Inheritance:#include<iostream.h> #include<conio.h> class base { protected: int rollno; char name[20]; public: void input() { cout<<"\nEnter the Rollno:"; cin>>rollno; cout<<"\nEnter the Name:"; cin>>name; } void output() { cout<<"\nRollno:"<<rollno; cout<<"\nName:"<<name; } }; class mark:public base { protected: int mark1,mark2,mark3; public: void getdata() { cout<<"\nEnter the Mark1:"; cin>>mark1; cout<<"\nEnter the Mark2:"; cin>>mark2; cout<<"\nEnter the Mark3:"; cin>>mark3; } void putdata() { cout<<"\nMark1 is:"<<mark1; cout<<"\nMark2 is:"<<mark2; cout<<"\nMark3 is:"<<mark3; } };

class final: public mark { protected: int total; float avg; public: void read() { base::input(); mark::getdata(); } void calculate() { total=mark1+mark2+mark3; avg=total/3; } void write() { base::output(); mark::putdata(); cout<<"\n total is:"<<total; cout<<"\nAvg is:"<<avg; } }; void main() { clrscr(); final f; f.read(); f.calculate(); f.write(); getch(); }

Output for Exercise 14:


Enter the Rollno: 948 Enter the Name: Krishnan Enter the Mark1: 100 Enter the Mark2:100 Enter the Mark3:100 Rollno:948 Name : Krishnan Mark1 is : 100 Mark2 is : 100 Mark3 is : 100 Total is : 300 Avg is : 100

Exercise 15. Hybrid Inheritance:#include<iostream.h> #include<conio.h> class player { protected: int playerno; public: void getno() { cout<<"Enter the player no\n"; cin>>playerno; } void putno() { cout<<"Player No : "<<playerno<<endl; } }; class score:public player { public: float match1,match2; void getscore() { cout<<"Enter the first and Second match score\n"; cin>>match1>>match2; } void putscore() { cout<<"1st Match score is : "<<match1<<endl; cout<<"2nd Match score is : "<<match2<<endl; } }; class name { public: char Team_Name[30]; void getdata() { cout<<"Enter the Team Name\n"; cin>>Team_Name; }

void putdata() { cout<<"The Team Name is } }; class result:public score, public name { public: int total; void display() { total=match1+match2; putno(); putdata(); putscore(); cout<<"Total Match Score } }; void main() { result rs; clrscr(); cout<<"\t\t\t|------------------|\n"; cout<<"\t\t\t|Hybrid Inhertiance|\n"; cout<<"\t\t\t|------------------|\n\n\n"; cout<<"\t\tEnter the player Data\n\n"; rs.getno(); rs.getdata(); rs.getscore(); cout<<"\t\tPlayer Details are\n"; cout<<"\t\t------------------\n"; rs.display(); getch(); }

"<<Team_Name<<endl;

"<<total<<endl;

Output for Exercise 15:Enter the Player Data Enter the Player No: 5 Enter the Team Name : India Enter the First and Second match score : 58 72 Player Details are Player No The Team Name is 1st Match score is 2nd Match score is Total Match score : : : : : 5 India 58 72 130

Exercise 16 .PLUS OPERATOR (+):#include<iostream.h> #include<conio.h> class complex { float x; float y; public: complex() {} complex(float real, float img) { x=real; y=img; } complex operator+(complex); void display(); }; complex complex::operator+(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return(temp); } void complex::display() { cout<<x<<"\t"<<y<<"\n"; } void main() { complex c1,c2,c3; clrscr(); c1=complex(2.5,3.5); c2=complex(1.0,1.0); c3=c1+c2; cout<<"\t\t|*********************************|\n"; cout<<"\t\t|Operator Overloading for uniary +|\n"; cout<<"\t\t|*********************************|\n"; cout<<"\n\n"; cout<<"c1 : c1.display(); ";

cout<<"c2 : "; c2.display(); cout<<"\n\n"; cout<<"The addition of c1 and c2 is\n"; cout<<"****************************\n"; c3.display(); getch(); }

Output for Exercise 16:Operator Overloading for Unary + C1 C2 : : 2.5 1 3.5 1

The addition of C1 and C2 is 3.5 4.5

Exercise 17. MINUS OPERATOR(-):#include<iostream.h> #include<conio.h> class space { int x; int y; int z; public: void getdata(int a, int b, int c); void operator-(); void display(); }; void space::operator-() { x=-x; y=-y; z=-z; } void space :: getdata(int a, int b, int c) { x=a; y=b; z=c; } void space::display() { cout<<"X value is : "<<x<<endl; cout<<"Y value is : "<<y<<endl; cout<<"Z value is : "<<z<<endl; }

void main() { space s; clrscr(); s.getdata(10,20,-30); cout<<"\t\t|*********************************|\n"; cout<<"\t\t|Operator Overloading for uniary -|\n"; cout<<"\t\t|*********************************|\n"; cout<<"\n\n"; cout<<"\tBefore changing the Symbol is\n"; s.display(); cout<<"\n\n"; -s; cout<<"\tAfter changing the Symbol is\n"; s.display(); getch(); }

Output for Exercise 17:Operator Overloading for Unary Before Changing the Symbol X value is : 10 Y value is : 20 Z value is : -30 After Changing the Symbol X value is : -10 Y values is : -20 Z value is : 30

Exercise 18. PLUS OR EQUAL OPERATOR (+=):#include<iostream.h> #include<conio.h> class plusorequal { private: int x,y,add,z; public: void getvalue() { cout<<"enter x value"; cin>>x; cout<<"\nenter y value"; cin>>y; z=x+y; cout<<"\nz value is"<<z<<endl; } void show(); plusorequal operator+=(plusorequal); }; void plusorequal::show() { cout<<"\nincremented value using+=opreator is"<<add<<endl; } plusorequal plusorequal::operator+=(plusorequal m2) { cout<<"\nenter s value"; cin>>add; add+=m2.z; return m2; } void main() { clrscr(); plusorequal m1,m2; m1.getvalue(); m2+=m1; m2.show(); getch(); }

Output for Exercise 18:-:


ENTER X VALUE: 10 ENTER Y VALUE: 10 Z VALUE IS: 20 ENTER S VALUE: 30 Incremented value using+=operator is: 50

Exercise 19. MINUS OR OPERATOR (-=):#include<iostream.h> #include<conio.h> class minusorequal { private: int x,y,min,z; public: void getvalue() { cout<<"enter x value :"; cin>>x; cout<<"\nenter y value :"; cin>>y; z=x+y; cout<<"\nz value is"<<z<<endl; } void show(); minusorequal operator-=(minusorequal); }; void minusorequal::show() { cout<<"\ndecremented value using-=opreator is"<<min<<endl; } minusorequal minusorequal::operator-=(minusorequal m2) { cout<<"\nenter s value"; cin>>min; min-=m2.z; return m2; } void main() { clrscr(); minusorequal m1,m2; m1.getvalue(); m2-=m1; m2.show(); getch(); }

Output for Exercise 19:-:


ENTER X VALUE:10 ENTER Y VALUE:10 Z IS VALUE:20 Decremented value using -= operator is 10

Exercise 20. Increment Operator (++):#include<iostream.h> #include<conio.h> class incre { int x,y,z; public: void getvalue() { cout<<"enter x value :"; cin>>x; cout<<"\nenter y value:"; cin>>y; } void show() { cout<<"\nx value is"<<x<<endl; cout<<"\ny value is"<<y<<endl; } void operator++() { x=++x; y=++y; z=x+y; } void disp() { cout<<"addition value after incermenting is"<<z; } }; void main() { clrscr(); incre d; d.getvalue(); d.show(); ++d; d.disp(); getch(); }

Output for Exercise 20:Enter the x value: 10 Enter the y value: 10 X value is: 10 Y value is: 10 After value after incrementing is: 22

Exercise 21. Decrement Operator (--):#include<iostream.h> #include<conio.h> class decre { int x,y,z; public: void getvalue() { cout<<"enter x value :"; cin>>x; cout<<"\nenter y value:"; cin>>y; } void show() { cout<<"\nx value is"<<x<<endl; cout<<"\ny value is"<<y<<endl; } void operator--() { x=x--; y=y--; z=x+y; } void disp() { cout<<"addition value after incermenting is"<<z; } }; void main() { clrscr(); decre d; d.getvalue(); d.show(); --d; d.disp(); getch(); }

Output for Exercise 21:ENTER X VALUE:10 ENTER Y VALUE:10 X IS VALUE:10 Y IS VALUE:10 ADDITION VALUE AFTER DECREMENT IS:20

Exercise 22. Friend Function:#include<iostream.h> #include<conio.h> class sample { int a,b; public: void setvalue() { a=10; b=20; } friend float mean(sample s) }; float mean(sample s) { return float(s.a+s.b)/2.0; } main() { clrscr(); sample x; x.setvalue(); cout<<"\nmean value="<<mean(x); getch(); }

Output for Exercise 22:-:


Mean Value: 15

Exercise 23. Inline Function:#include<iostream.h> #include<conio.h> inline int sum(int a,int b) { return a+b; } void main() { int c; clrscr(); c=sum(5,5); cout<<"\n sum of values:"<<c; getch(); }

Output for Exercise 23:-:


Sum of Values: 10

Exercise 24. Function Overloading:#include<iostream.h> #include<conio.h> class funover { public: void display() { cout<<"\nNo argument function\n"; cout<<"--------------------\n"; } void display(int a, int b) { cout<<"\nFunction overloading with 2 arguments\n"; cout<<"-------------------------------------\n"; cout<<"The two arguments are "<<a<<endl<<b; } }; void main() { funover fn; clrscr(); cout<<"\t\t\t--------------------\n"; cout<<"\t\t\tFunction overloading\n"; cout<<"\t\t\t--------------------\n\n\n"; fn.display(); fn.display(5,10); getch(); }

Output for Exercise 24:Function Overloading No argument function Function overloading with 2 arguments The two arguments are: 5 10

Exercise 25. Virtual Function:#include<iostream.h> #include<conio.h> #include<string.h> class media { protected: char title[50]; float price; public: media(char *s,float a) { strcpy(title,s); price=a; } virtual void display() { } }; class book:public media { int pages; public: book(char *s,float a,int p):media(s,a) { pages=p; } void display(); }; class tape:public media { float time; public: tape(char *s,float a, float t):media(s,a) { time=t; } void display(); }; void book::display() { cout<<"\nTitle:"<<title; cout<<"\nPages:"<<pages; cout<<"\nPrice:"<<price;

} void tape::display() { cout<<"\nTitle:"<<title; cout<<"\Play Time:"<<time<<"mins"; cout<<"\nPrice:"<<price; } int main() { char *title=new char[30]; float price,time; int pages; cout<<"\nENTER BOOK DETAILS\n"; cout<<"\nTitle:"; cin>>title; cout<<"\nPrice:"; cin>>price; cout<<"\nPages:"; cin>>pages; book book1(title,price,pages); cout<<"\nENTER TAPE DETAILS\n"; cout<<"\nTitle:"; cin>>title; cout<<"\nPrice:"; cin>>price; cout<<"\nPlay Time(mins):"; cin>>time; tape tape1(title,price,time); media *list[2]; list[0]=&book1; list[1]=&tape1; cout<<"\nMEDIA DETAILS:"; cout<<"\n......BOOK......"; list[0]->display(); cout<<"\n......TAPE......"; list[1]->display(); return 0; }

Output for Exercise 25:ENTER BOOK DETAILS Title:Programming in C Price:88 Pages:400 ENTER TAPE DETAILS Title:Computing_Concepts Price:90 Play Time(mins):55 MEDIA DETAILS ..BOOK. Title:Programming in C Price:88 Pages:400 .TAPE.. Title:Computing_Concepts Price:90 Play Time(mins):55

Exercise 26. Template Overloading:#include<iostream.h> #include<conio.h> template<class T> void bubble(T a[],int n) { for(int i=0;i<n-1;i++) for(int j=n-1;i<j;j--) if(a[j]<a[j-1]) { swap(a[j],a[j-1]); } } template<class x> void swap(x &a, x &b) { x temp=a; a=b; b=temp; } int main() { clrscr(); int x[5]={10,50,30,40,20}; float y[5]={1.1,5.5,3.3,4.4,2.2}; bubble(x,5); bubble(y,5); cout<<"sorted x-array:"; for(int i=0;i<5;i++) cout<<x[i]<<" "; cout<<endl; cout<<"sorted y-array:"; for(int j=0;j<5;j++) cout<<y[j]<<" "; cout<<endl; getch(); return 0; }

Output for Exercise 26:Sorted x-array: 10, 20, 30, 40, 50 Sorted Y-array: 1.1, 2.2, 3.3, 4.4, 5.5

Exercise 27. Overloading Function And Template:#include<iostream.h> #include<conio.h> template<class T> void swap(T &x,T &y) { T temp=x; x=y; y=temp; } void fun(int m,int n,float a,float b) { cout<<"M and N before Swap:\n"<<m<<endl<<n<<endl; swap(m,n); cout<<"\n"; cout<<"M and N after Swap:\n"<<m<<endl<<n; cout<<"\n\n"; cout<<"A and B before Swap:\n"<<a<<endl<<b<<endl; swap(a,b); cout<<"A and B after Swap:\n"<<a<<endl<<b<<endl; } void main() { fun(100,200,15.52,56.4); getch(); }

Output for Exercise 27:M AND N BEFORE SWAP: 100 200 M AND N AFTER SWAP: 200 100 A AND B BEFORE SWAP: 15.52 56.400002 A AND B AFTER SWAP: 56.400002 15.52

Exercise 28. Template in Class:#include<iostream.h> #include<conio.h> const size=3; template<class T> class vector { T* v; public: vector() { v=new T[size]; for(int i=0;i<size;i++) v[i]=0; } vector(T* a) { for(int i=0;i<size;i++) v[i]=a[i]; } T operator*(vector &y) { T sum=0; for(int i=0;i<size;i++) sum+=this->v[i] * y.v[i]; return sum; } }; int main() { int x[3]={1,2,3}; int y[3]={4,5,6}; vector<int>v1; vector<int>v2; v1=x; v2=y; int R=v1*v2; cout<<"R="<<R<<"\n"; getch(); }

Output for Exercise 28:R=32