Sunday, 26 April 2015

U4P1

Define a class to represent a string with operations string length, compare and reverse. Show its use by writing main().

Code :
 #include<iostream.h>  
 #include<conio.h>  
 #include<String.h>  
 class string  
 {  
      char str1[10],str2[10];  
      public:  
      void getstr()  
      {  
           cout<<"\nEnter First String : ";  
           cin>>str1;  
           cout<<"Enter Second String : ";  
           cin>>str2;  
      }  
      void length()  
      {  
           cout<<"\nLength of First String is : "<<strlen(str1);  
           cout<<"\nLength of Second String is : "<<strlen(str2);  
      }  
      void reverse()  
      {  
           cout<<"\n\nReversed String of First String is : "<<strrev(str1);  
           cout<<"\nReversed String of Second String is : "<<strrev(str2);  
      }  
      void compare()  
      {  
           strrev(str1);  
           strrev(str2);  
           if(strcmp(str1,str2)>0)  
           cout<<"\n\nComparing both strings : "<<str1<<" is larger.";  
           else if(stricmp(str1,str2)<0)  
           cout<<"\n\nComparing both strings : "<<str2<<" is larger.";  
           else  
           cout<<"\n\nComparing both strings : Both Strings are same.";  
      }  
 };  
 void main()  
 {  
      clrscr();  
      cout<<"Program will show use of string funtions : strlen(), strrev() and strcmp().\n";  
      string newstr;  
      newstr.getstr();  
      newstr.length();  
      newstr.reverse();  
      newstr.compare();  
      getch();  
 }  

Output:

U8P1

What is Generic Programming? How it is implemented in C++.Write General format of class templates and function Template. Write program to swap Number using Function Template. Function prototype is given below:
void swap(int, int , float , float )
Swap two integer numbers and swap two float number.

Code :
 #include<iostream.h>  
 #include<conio.h>  
 template<class T>  
 void swap(T &x,T&y)  
 {  
      T temp;  
      temp=x;  
      x=y;  
      y=temp;  
 }  
 void function(int m,int n,float a,float b)  
 {  
      cout<<"------Before Swap------"<<endl;  
      cout<<"M = "<<m<<endl;  
      cout<<"N = "<<n<<endl;  
      swap(m,n);  
      cout<<"\n---Interger Swap---"<<endl<<"M = "<<m<<endl<<"N = "<<n<<endl;  
      cout<<"\n------Before Swap------"<<endl;  
      cout<<"A = "<<a<<endl;  
      cout<<"B = "<<b<<endl;  
      swap(a,b);  
      cout<<"\n---Float Swap---"<<endl<<"A = "<<a<<endl<<"B = "<<b<<endl;  
 }  
 void main()  
 {  
      clrscr();  
      function(10,20,1.23,2.34);  
      getch();  
 }  

Output :

U7P2

Write a program that reads a text file and creates another text file that is identical except that every letter must be converted to lower case irrespective of its original case. (e.g. ‘a’ or ‘A’ will become ‘a’).

Code :
 #include<iostream.h>  
 #include<conio.h>  
 #include<fstream.h>  
 #include<string.h>  
 void main()  
 {  
      clrscr();  
      char c[100];  
      ofstream f1("File3.txt");  
      cout<<"Enter String : ";  
      cin.getline(c,100);  
      f1<<c;  
      f1.close();  
      ifstream f2("File3.txt");  
      f2.getline(c,50);  
      strlwr(c);  
      cout<<"Lowercase :- "<<c<<endl;  
      f2.close();  
      ofstream f3("File4.txt");  
      f3<<c;  
      f3.close();  
      getch();  
 }  

Output : 

U7P1

Write a program that reads a text file and creates another file that is identical except that every character is in upper case.

Code :
 #include<iostream.h>  
 #include<conio.h>  
 #include<fstream.h>  
 #include<string.h>  
 void main()  
 {  
      clrscr();  
      char c[100];  
      ofstream f1("File1.txt");  
      cout<<"Enter String : ";  
      cin.getline(c,100);  
      f1<<c;  
      f1.close();  
      ifstream f2("File1.txt");  
      f2.getline(c,50);  
      strupr(c);  
      cout<<"UPPERCASE :- "<<c<<endl;  
      f2.close();  
      ofstream f3("File2.txt");  
      f3<<c;  
      f3.close();  
      getch();  
 }  

Output :

U6P5

Define a class complex with real and imaginary as two data member with default & parameterized constructors ,function to initialize display data of class .It should overload the '+' operator to add two complex objects. Write a program to demonstrate use of complex class.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 class complex  
 {  
      public:  
      float x,y;  
      complex()  
      {  
           x=0;  
           y=0;  
      }  
      complex(float real,float image)  
      {  
           x=real;  
           y=image;  
      }  
      void display(complex c)  
      {  
           cout<<"Real: "<<c.x<<" + "<<"Img: "<<c.y<<endl;  
      }  
 };  
 complex sum(complex c1,complex c2)  
 {  
      complex c3;  
      c3.x=c1.x+c2.x;  
      c3.y=c1.y+c2.y;  
      return c3;  
 }  
 void main()  
 {  
      clrscr();  
      complex A(1.1,2.1),B(1.2,2.2),C;  
      C=sum(A,B);  
      cout<<"For A:\n";  
      A.display(A);  
      cout<<"\nFor B:\n";  
      B.display(B);  
      cout<<"\nFor A+B:\n";  
      C.display(C);  
      getch();  
 }  

Output : 

U6P4

Write a C++ program demonstrating use of the pure virtual function with the use of base and derived classes.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 class base  
 {  
      public:  
      int a;  
      virtual void show()=0;  
 };  
 class derived : public base  
 {  
      int b;  
      void show()  
      {  
           cout<<"Enter A : ";  
           cin>>a;  
           cout<<"Enter B : ";  
           cin>>b;  
           cout<<"\nSimple Addtion : "<<a+b;  
      }  
 };  
 void main()  
 {  
      clrscr();  
      cout<<"Program demonstrates the use of Pure Virtual Function.\n";  
      base *z=new derived();  
      z->show();  
      getch();  
 }  

Output :

U6P3

Create class Time that has three data members hour, minute and second and two constructor, default constructor and parameterized constructor to initialize data member. Write a program to add two times by overloading operator '+'.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 class time  
 {  
      int hour,minute,second;  
      public:  
      time()  
      {  
           hour = 0;  
           minute = 0;  
           second = 0;  
      }  
      time(int h,int m,int s)  
      {  
           hour = h;  
           minute = m;  
           second = s;  
      }  
      void show()  
      {  
           cout<<"\n-- Total Time --";  
           cout<<endl<<"Hours  : "<<hour<<endl<<"Minutes : "<<minute<<endl<<"Seconds : "<<second;  
      }  
      time operator +(time tt)  
      {  
           time t;  
           t.hour=tt.hour+hour;  
           t.minute=tt.minute+minute;  
           t.second=tt.second+second;  
           while(t.second>60)  
           {  
                t.second-=60;  
                t.minute++;  
           }  
           while(t.minute>60)  
           {  
                t.minute-=60;  
                t.hour++;  
           }  
           return t;  
      }  
 };  
 void main()  
 {  
      clrscr();  
      int h,m,s;  
      cout<<"Addition of two Timestamps by overloading + operator.";  
      cout<<"\n-- Enter Time1 --\n\nHours  : ";  
      cin>>h;  
      cout<<"Minutes : ";  
      cin>>m;  
      cout<<"Seconds : ";  
      cin>>s;  
      time t1(h,m,s);  
      cout<<"\n-- Enter Time2 --\n\nHours  : ";  
      cin>>h;  
      cout<<"Minutes : ";  
      cin>>m;  
      cout<<"Seconds : ";  
      cin>>s;  
      time t2(h,m,s),t3;  
      t3=t1+t2;  
      t3.show();  
      getch();  
 }  

Output : 

U6P2

Program to create a class THREE_D contains data members like X, Y, Z. Include constructor to initialize data. and overload Unary +,-,++,-- and Binary * Operator.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 class THREE_D  
 {  
      public:  
      int x;  
      int y,z;  
      THREE_D()  
      {  
           x=0;  
           y=0;  
           z=0;  
      }  
      THREE_D(int c)  
      {  
           z=c;  
      }  
      THREE_D(int a,int b)  
      {  
           x=a;  
           y=b;  
      }  
      void showadd()  
      {  
           cout<<"\nAddition (X + Y + z) : "<<x<<endl;  
      }  
      void showsub()  
      {  
           cout<<"\nSubstraction (Y - x) : "<<y<<endl;  
      }  
      void showmul()  
      {  
           cout<<"\nMultiplication (X * Y) : "<<z<<endl;  
      }  
      THREE_D operator +(THREE_D p)  
      {  
           THREE_D temp;  
           temp.x=p.x+x;  
           return temp;  
      }  
      THREE_D operator -(THREE_D p1)  
      {  
           THREE_D temp1;  
           temp1.y=p1.y-y;  
           return temp1;  
      }  
      THREE_D operator *(THREE_D p2)  
      {  
           THREE_D temp2;  
           temp2.z=p2.z*z;  
           return temp2;  
      }  
      THREE_D operator ++()  
      {  
           z++;  
      }  
      void displayin()  
      {  
           cout<<"\nIncrement Z(++) : " <<z<<endl;  
      }  
      THREE_D operator --()  
      {  
           z--;  
      }  
      void displayde()  
      {  
           cout<<"\nDecrement Y(--) : "<<z;  
      }  
 };  
 void main()  
 {  
      clrscr();  
      int x,y,z;  
      cout<<"Program will overload unary +, -, ++, -- and * operators."<<endl<<endl;  
      cout<<"Enter X : ";  
      cin>>x;  
      cout<<"Enter Y : ";  
      cin>>y;  
      cout<<"Enter Z : ";  
      cin>>z;  
      THREE_D z1(x,x),z2(y,y),z3(z,z),z4;  
      //ADDTION  
      z4=z1+z2+z3;  
      z4.showadd();  
      //SUBSTRACTION  
      z3=z1-z2;  
      z3.showsub();  
      //MULTIPLICATION  
      THREE_D z5(x),z6(y),z7;  
      z7=z5*z6;  
      z7.showmul();  
      //INCREMENT  
      THREE_D z8(z);  
      z8++;  
      z8.displayin();  
      //DECREMENT  
      z6--;  
      z6.displayde();  
      getch();  
 }  

Output : 

U6P1

Program to overload a single Unary Operator.

Code :
 #include<iostream.h>  
 #include<conio.h>  
 class optr  
 {  
      public:  
      int a;  
      optr()  
      {  
           a=0;  
      }  
      optr(int x)  
      {  
           a=x;  
      }  
      void show()  
      {  
           cout<<"Addition : "<<a;  
      }  
      optr operator +(optr p)  
      {  
           optr temp;  
           temp.a=p.a+a;  
           return temp;  
      }  
 };  
 void main()  
 {  
      clrscr();  
      int x,y;  
      cout<<"Program will give addition of given two values by overloading unary '+' operator"<<endl;  
      cout<<"Enter Value of X : ";  
      cin>>x;  
      cout<<"Enter Value of Y : ";  
      cin>>y;  
      optr z1(x),z2(y),z3;  
      z3=z1+z2;  
      z3.show();  
      getch();  
 }  

Output :

Tuesday, 21 April 2015

Gauss_Seidel

Gauss-Seidel Method

Code : 
 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 void main()  
 {  
      int accu,i,j,xt1,yt1,zt1,xt2,yt2,zt2;  
      float a[3][3],b[3],x=0,y=0,z=0,xt,yt,zt;  
      clrscr();  
      printf("Enter matrix of co-efficient:");  
      for(i=0;i<3;i++)  
      {  
           for(j=0;j<3;j++)  
           {  
                printf("Enter A[%d][%d]:",i,j);  
                scanf("%f",&a[i][j]);  
           }  
      }  
      printf("\nEnter constants:");  
      for(i=0;i<3;i++)  
      {  
           printf("\nEnter B[%d]:",i);  
           scanf("%f",&b[i]);  
      }  
      printf("\nEnter req. accuracy:");  
      scanf("%d",&accu);  
      do  
      {  
           xt=x;  
           yt=y;  
           zt=z;  
           x=(b[0]-a[0][1]*yt-a[0][2]*zt)/a[0][0];  
           y=(b[1]-a[1][0]*x-a[1][2]*zt)/a[1][1];  
           z=(b[2]-a[2][0]*x-a[2][1]*y)/a[2][2];  
           xt1=xt*pow(10,accu);  
           yt1=yt*pow(10,accu);  
           zt1=zt*pow(10,accu);  
           xt2=x*pow(10,accu);  
           yt2=y*pow(10,accu);  
           zt2=z*pow(10,accu);  
      }while((xt1-xt2)!=0 && (yt1-yt2)!=0 && (zt1-zt2)!=0);  
      x=xt1*pow(10,-accu);  
      y=yt1*pow(10,-accu);  
      z=zt1*pow(10,-accu);  
      printf("\nFinal solution is x=%g \t y=%g \t z=%g",x,y,z);  
      getch();  
 }  

Gauss_Jacobi

Gauss Jacobi Method

Code : 
 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 void main()  
 {  
      float x[50], y[50], z[50], xans, yans, zans;  
      int i,j,ac, xt, yt, zt, xt1, yt1, zt1;  
      float a[3][3],b[3];  
      clrscr();  
      printf("\nEnter Matrix for Gauss Jacobi Method : \n");  
      for(i=0;i<3;i++)  
      {  
           for(j=0;j<3;j++)  
           {  
                printf("Enter A[%d][%d] : ",i,j);  
                scanf("%f",&a[i][j]);  
           }  
      }  
      printf("\nEnter column vactor of constant : \n");  
      for(i=0;i<3;i++)  
      {  
           printf("Enter B[%d] : ",i);  
           scanf("%f",&b[i]);  
      }  
      x[0]=0;  
      y[0]=0;  
      z[0]=0;  
      printf("\nEnter accuracy : ");  
      scanf("%d",&ac);  
      printf("\nNo.\tX\t\tY\t\tZ");  
      for(i=0;i<50;i++)  
      {  
           x[i+1] = (1/a[0][0]) * (b[0] - (a[1][0]*y[i]) - (a[2][0]*z[i]) );  
           y[i+1] = (1/a[1][1]) * (b[1] - (a[0][1]*x[i]) - (a[2][1]*z[i]) );  
           z[i+1] = (1/a[2][2]) * (b[2] - (a[0][2]*x[i]) - (a[1][2]*y[i]) );  
           xt = x[i] * pow(10,ac);  
           yt = y[i] * pow(10,ac);  
           zt = z[i] * pow(10,ac);  
           xt1 = x[i+1] * pow(10,ac);  
           yt1 = y[i+1] * pow(10,ac);  
           zt1 = z[i+1] * pow(10,ac);  
           printf("\n%d.\t%f\t%f\t%f",i,x[i],y[i],z[i]);  
           if(abs(xt-xt1)==0 && abs(yt-yt1)==0 && abs(zt-zt1)==0)  
           {  
                break;  
           }  
      }  
      xans = xt * pow(10,-ac);  
      yans = yt * pow(10,-ac);  
      zans = zt * pow(10,-ac);  
      printf("\n\nRequired %d Decimal accurate answer is : \n==>\tX = %f\tY = %f\tZ = %f", ac, xans, yans, zans);  
      getch();  
 }  

Output : 

Regula_Falsi

Regula-Falsi Method

Code :
 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 double f(double x)  
 {  
      return ((x*x*x)-x-11);  
 }  
 void main()  
 {  
      double a,b,x=0,eps;  
      int acc,i=0,temp;  
      clrscr();  
      printf("Equation : X^3-X-11");  
      printf("\nenter a and b:");  
      scanf("%lf %lf",&a,&b);  
      while(f(a)*f(b)>0)  
      {  
           printf("\nintervals are invalid");  
           printf("\nenter a and b:");  
           scanf("%lf %lf",&a,&b);  
      }  
      printf("\nenter accuracy upto 6:");  
      scanf("%d",&acc);  
      eps=pow(10,-(acc));  
      while(fabs(x-a)>eps)  
      {  
           x=((b*f(a))-(a*f(b)))/(f(a)-f(b));  
           printf("\ninteration no.:%d\tx:%lf",i++,x);  
           a=b;  
           b=x;  
      }  
      a=x*pow(10,acc);  
      temp=(int)a;  
      x=temp/pow(10,acc);  
      printf("\nrequired %dD accurate ans is %g",acc,x);  
      getch();  
 }  

Output : 

Secant

Secant Method

Code : 
 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 double f(double x)  
 {  
      return ((x*x*x)-x-11);  
 }  
 void main()  
 {  
      double a,b,x=0,eps;  
      int acc,i=0,temp;  
      clrscr();  
      printf("Equation : X^3 - X - 11");  
      printf("\nenter a and b:");  
      scanf("%lf %lf",&a,&b);  
      while(f(a)*f(b)>0)  
      {  
           printf("\nintervals are invalid");  
           printf("\nenter a and b:");  
           scanf("%lf %lf",&a,&b);  
      }  
      printf("\nenter accuracy upto 6:");  
      scanf("%d",&acc);  
      eps=pow(10,-(acc));  
      while(fabs(x-a)>eps)  
      {  
           x=b-(f(b)*((a-b)/(f(a)-f(b))));  
           printf("\ninteration no.:%d\tx:%lf",i++,x);  
           a=b;  
           b=x;  
      }  
      a=x*pow(10,acc);  
      temp=(int)a;  
      x=temp/pow(10,acc);  
      printf("\n\nrequired %dD accurate ans is %g",acc,x);  
      getch();  
 }  

Output :

Newton_Rahphson

Newton Rahphson Method

Code :
 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 double f(double x)  
 {  
      return ((x*x*x)-x-11);  
 }  
 double df(double x)  
 {  
      return ((3*x*x)-1);  
 }  
 void main()  
 {  
      double a,b,x=0,eps;  
      int acc,i=0,temp;  
      clrscr();  
      printf("Equation : X^3-X-11");  
      printf("\nenter a and b:");  
      scanf("%lf %lf",&a,&b);  
      while(f(a)*f(b)>0)  
      {  
           printf("\nintervals are invalid");  
           printf("\nenter a and b:");  
           scanf("%lf %lf",&a,&b);  
      }  
      printf("\nenter accuracy upto 6:");  
      scanf("%d",&acc);  
      eps=pow(10,-(acc));  
      while(fabs(x-a)>eps)  
      {  
           x=a;  
           a=a-(f(a)/df(a));  
           printf("\ninteration no.:%d\tx:%lf",i++,a);  
      }  
      x=a*pow(10,acc);  
      temp=(int)x;  
      a=temp/pow(10,acc);  
      printf("\n\nrequired %dD accurate ans is %g",acc,a);  
      getch();  
 }  

Output:

Bisection

Bisection Method

Code : 


 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 float f(float x)  
 {  
      return ((x*x*x)-x-11);  
 }  
 void main()  
 {  
      float a,b,m,eps;  
      int c,acc,temp;  
      clrscr();  
      printf("Equation : X^3-X-11");  
      printf("enter a:");  
      scanf("%f",&a);  
      printf("enter b:");  
      scanf("%f",&b);  
      while(f(a)*f(b)>0)  
      {  
           printf("\nintervals are invalid:");  
           printf("\nenter a:");  
           scanf("%f",&a);  
           printf("\nenter b:");  
           scanf("%f",&b);  
      }  
      printf("\nenter accuracy:");  
      scanf("%d",&acc);  
      eps=pow(10,-(acc));  
      for(c=1;c<50;c++)  
      {  
           m=(a+b)/2;  
           if((f(m)*f(a))>0)  
                a=m;  
           else  
                b=m;  
           printf("\niteration no. x[%d] is %f",c,m);  
           if(fabs(a-b)<eps)  
                break;  
      }  
      a=m*pow(10,acc);  
      temp=(int)a;  
      m=temp/pow(10,acc);  
      printf("\nrequired %dD accurate ans is %g",acc,m);  
      getch();  
 }  

Output :

Wednesday, 1 April 2015

Hamming_code

HAMMING CODE

 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 void main()  
 {  
      int a[20],b[20],c[20],d[20],i,k,m,f,n,j,r,p,x,y,z,ch,key,q,v;  
      clrscr();  
      system("clear");  
      printf("\nEnter Length of Data word : ");  
      scanf("%d",&k);  
      printf("\nEnter The Data word : ");  
      for(i=1; i<=k; i++)  
      {  
           scanf("%d",&a[i]);  
      }  
      m=1;  
      while((k+m+1)>=pow(2,m))  
      {  
           m++;  
      }  
      printf("\nValue of M is : %d",m);  
      n=k+m;  
      j=1;  
      r=0;  
      for(i=1 ; i<=n;i++)  
      {  
           p = pow(2,r);  
           if(i==p)  
           {  
                b[i]=0;  
                r++;  
           }  
           else  
           {  
                b[i]=a[j];  
                j++;  
           }  
      }  
      printf("\nIntermediate Code Word is : \n");  
      for(i=1; i<=n; i++)  
           printf("%d",b[i]);  
      p=0;  
      for(i=1; i<=m; i++)  
      {  
           x=pow(2,p);  
           r=1;  
           for(j=x; j<=n; j=j+(x*2))  
           {  
                for(y=j; y<(j+x); y++)  
                {  
                     c[r] = b[y];  
                     r++;  
                }  
           }  
           z=0;  
           for(y=1; y<=(r-1); y++)  
           {  
                if(c[y]==1) z++;  
           }  
           if(z%2==0)  
                b[x]=0;  
           else  
                b[x]=1;  
           for(y=1; y<=20; y++)  
                c[y]=0;  
           p++;  
      }  
      printf("\n\nThe Hamming Code : ");  
      for(i=1; i<=n; i++)  
           printf("%d",b[i]);  
      getch();  
 }  


U5P7

Create a class student that stores roll_no,name. Create a class test that stores marks obtained in five subjects. Class result derived from student and test contains the total marks and percentage obtained in test. Input and display information of a student.

 #include<iostream.h>  
 #include<conio.h>  
 class student  
 {  
      public:  
      char name[10];  
      int roll;  
      void get_basic()  
      {  
           cout<<"Enter name of the Student : ";  
           cin>>name;  
           cout<<"Enter Roll Number of "<<name<<" : ";  
           cin>>roll;  
      }  
 };  
   
 class test  
 {  
      public:  
      int marks[5];  
      void get_marks()  
      {  
           cout<<"\nEnter marks of 5 Subjects(Out of 100): "<<endl;  
           for(int i=0;i<5;i++)  
           {  
                cout<<"Marks of Subject "<<i+1<<" : ";  
                cin>>marks[i];  
           }  
      }  
   
 };  
   
 class result : public student, public test  
 {  
      float total;  
      public:  
      float get_total()  
      {  
           for(int i=0;i<5;i++)  
           {  
                total = total + marks[i];  
           }  
           return total;  
      }  
      float get_perc()  
      {  
           return total/5;  
      }  
   
   
 };  
   
 void main()  
 {  
      clrscr();  
      result obj1;  
      obj1.get_basic();  
      obj1.get_marks();  
      cout<<"\n=========================";  
      cout<<"\nRoll No  : "<<obj1.roll;  
      cout<<"\nName    : "<<obj1.name;  
      cout<<"\nTotal   : "<<obj1.get_total()<<"/500";  
      cout<<"\nPercentage : "<<obj1.get_perc()<<" %";  
      cout<<"\n=========================";  
      getch();  
 }  

Output :

U5P6

Consider a class network as shown in figure given below. The class Employee derives information from both Account and Admin classes which in turn derive information from the class Person. Define all the four classes and write a program to input and display the information of an Employee.

 #include<iostream.h>  
 #include<conio.h>  
 class person  
 {  
      public:  
      char name[10];  
      int code;  
      void getp()  
      {  
           cout<<"Enter Name : ";  
           cin>>name;  
           cout<<"\nEnter Code : ";  
           cin>>code;  
      }  
 };  
   
 class account : virtual public person  
 {  
      public:  
      double pay;  
      void getac()  
      {  
           cout<<"\nEnter Salary : ";  
           cin>>pay;  
      }  
 };  
   
 class admin : virtual public person  
 {  
      public:  
      double exp;  
      void getad()  
      {  
           cout<<"\nEnter Experience : ";  
           cin>>exp;  
      }  
 };  
   
 class employee : public account, public admin  
 {  
      public:  
      void disp()  
      {  
           cout<<"\n=========================\nName\t\t: "<<name;  
           cout<<"\nCode\t\t: "<<code;  
           cout<<"\nSalary\t\t: "<<pay;  
           cout<<"\nExperience\t: "<<exp;  
           cout<<"\n=========================";  
      }  
 };  
   
 void main()  
 {  
      clrscr();  
      employee e1;  
      e1.getp();  
      e1.getac();  
      e1.getad();  
      e1.disp();  
      getch();  
 }  
   

Output :

U5P5

Assume that vehicle class is defined as base class with price and year of manufacturing. Derive two classes namely bus and truck from base class with bus with seating capacity and truck with loading capacity. Develop classes with necessary member functions to get and put data. Demonstrate its use in main ().

 #include<iostream.h>  
 #include<conio.h>  
 class vehicle  
 {  
      public:  
      double price;  
      int year;  
      void get_basic()  
      {  
           cout<<"\nEnter Price of the Vehicle : ";  
           cin>>price;  
           cout<<"Enter Year of Manufacture : ";  
           cin>>year;  
      }  
 };  
   
 class bus : public vehicle  
 {  
      int s_cap;  
      public:  
      void get_bus_cap()  
      {  
           cout<<"Enter Sitting Capacity : ";  
           cin>>s_cap;  
      }  
   
      void disp_bus()  
      {  
           cout<<endl<<"\n#############################";  
           cout<<"\nVehicle Type    : BUS";  
           cout<<"\nPrice       : "<<price;  
           cout<<"\nYear of Manfacture : "<<year;  
           cout<<"\nSitting Capacity  : "<<s_cap;  
      }  
 };  
   
 class truck : public vehicle  
 {  
      int l_cap;  
      public:  
      void get_truck_cap()  
      {  
           cout<<"Enter Loading Capacity(Tons) : ";  
           cin>>l_cap;  
      }  
   
      void disp_truck()  
      {  
           cout<<endl<<"\n#############################";  
           cout<<"\nVehicle Type    : TRUCK";  
           cout<<"\nPrice       : "<<price;  
           cout<<"\nYear of Manfacture : "<<year;  
           cout<<"\nLoading Capacity  : "<<l_cap<<" Tons";  
      }  
 };  
   
 void main()  
 {  
      clrscr();  
      bus b;  
      truck t;  
      cout<<"BUS : ";  
      b.get_basic();  
      b.get_bus_cap();  
      cout<<"\nTRUCK : ";  
      t.get_basic();  
      t.get_truck_cap();  
      b.disp_bus();  
      t.disp_truck();  
      getch();  
 }  

Output : 

U5P4

Assume that circle is defined using radius and cylinder is defined using radius and height. Write a circle class as base class and inherit the cylinder class from it. Develop classes such that user can compute the area of circle objects and volume of cylinder objects. Area of circle is pie*radius*radius, while volume of cylinder is pie*(radius)^2*height.

 #include<iostream.h>  
 #include<conio.h>  
 class circle  
 {  
      public:  
      float rad;  
      float area(float rad)  
      {  
           return 3.14*rad*rad;  
      }  
 };  
   
 class cylinder : public circle  
 {  
      public:  
      float rad2,height;  
      float volume(float rad2, float height)  
      {  
           return 3.14*(rad2*rad2)*height;  
      }  
 };  
   
 void main()  
 {  
      float r,r2,h;  
      clrscr();  
      cylinder obj;  
      cout<<"Radius of Circle : ";  
      cin>>r;  
      cout<<"\nArea of Circle : "<<obj.area(r);  
      cout<<"\n=======================\n\nRadius of Cylinder : ";  
      cin>>r2;  
      cout<<"\nHeight of Cylinder : ";  
      cin>>h;  
      cout<<"\n\nVolume of Cylinder : "<<obj.volume(r2,h);  
      getch();  
 }  
   


Output :

U5P2

Program to Implement Hybrid Inheritance.
Student class have two derived class named as Test, Sports and both Test, Sports has a derived class named as Result which display all the details.

 #include<iostream.h>  
 #include<conio.h>  
 class student  
 {  
      protected:  
      int roll;  
      public:  
      void get()  
      {    //roll =a;  
           cout<<"Roll Number of Student : ";  
           cin>>roll;  
      }  
 };  
   
 class test : virtual public student  
 {  
      public:  
      int marks;  
      void get2()  
      {  
           cout<<"Enter Total Marks Obtained in Test : ";  
           cin>>marks;  
      }  
 };  
   
 class sports : virtual public student  
 {  
      public:  
      int score;  
      void get3()  
      {  
           cout<<"Enter Total Score obtained in Sports : ";  
           cin>>score;  
      }  
 };  
   
 class result : public test, public sports  
 {  
      public:  
      void show_all()  
      {  
           cout<<endl<<"Roll Number of Student is  : "<<roll;  
           cout<<endl<<"Total Marks Obtained(Test)  : "<<marks;  
           cout<<endl<<"Total Score Obtained(Sports) : "<<score;  
      }  
 };  
   
 void main()  
 {  
      clrscr();  
      result obj;  
      obj.get();  
      obj.get2();  
      obj.get3();  
      obj.show_all();  
      getch();  
 }  
   


Output :

U5P1

Program to implement Single Inheritance.

 #include<iostream.h>  
 #include<conio.h>  
 class one  
 {  
      public:  
      int a,b;  
      void fun_one()  
      {  
           a=15; b=20;  
           cout<<"\nFunction of class ONE called....";  
           cout<<"\nA = "<<a<<"\nB = "<<b;  
      }  
 };  
   
 class two : public one  
 {  
      public:  
      void fun_two()  
      {  
           cout<<"\nFunction of class TWO called....\nTotal = "<<a+b;  
      }  
 };  
   
 void main()  
 {  
      clrscr();  
      two obj;  
      cout<<"\nObject of class TWO created....";  
      cout<<"\n\nAccessing Base Class function....";  
      obj.fun_one();  
      cout<<"\n\nAccessing Derived Class function....";  
      obj.fun_two();  
      getch();  
 }  

Output :