You are on page 1of 26

Ex. No.

: 25 Date:

Virtual Base Class

Aim:
To write a C++ program to illustrate the concept of virtual base class.

Algorithm:
1. A class student is created. 2. The data variables, name and regno and the member functions, void get() and void disp() are declared. 3. Another class sports is created which is derived from class student in public mode as class sports: public virtual student 4. The class sports contains the data variables m3, m4 and sp and member functions, void get1() and void disp1(). 5. Another class test is created in a similar way which is also derived from the class sports with required variables and member functions. 6. A class result is derived from the classes sports and test where the total points are calculated. 7. In the main(), an object, r is created for the class result and the required functions are called to get and display the information..

Program:
#include<iostream.h> class student { protected: char name[10]; int regno; public: void get() { cout<<"\nEnter name:"; cin>>name; cout<<"\nEnter reg no.:"; cin>>regno; } void disp() { cout<<"\nName: "<<name; cout<<"\nReg No.: "<<regno<<endl; } }; class sports:public virtual student { protected: float m3,m4,sp;

public: void get1() { cout<<"\nEnter grades m3 and m4(out of 10): "; cin>>m3>>m4; sp=(m3+m4)/2; } void disp1() { cout<<"\nGrade m3: "<<m3<<"\nGrade m4: "<<m4; cout<<"\nSports points: "<<sp<<endl; } }; class test:virtual public student { protected: float m1,m2,ap; public: void get2() { get(); cout<<"\nEnter grades m1 and m2(out of 10): " ; cin>>m1>>m2; ap=(m1+m2)/2; }

void disp2() { disp(); cout<<"\nGrade m1: "<<m1<<"\nGrade m2: "<<m2; cout<<"\nAcademic pts: "<<ap<<endl; } }; class result:public sports,public test { float tp; public: void res() { tp=((sp+ap)/2)*5; cout<<"\nTotal pts: "<< tp<<endl; if(tp>40) { cout<<"\nResult: I class"; } else if((tp>30)&&(tp<41)) { cout<<"\nResult: II class"; } else if((tp>20)&&(tp<31)) {

cout<<"\nResult: III class"; } } }; void main() { result r; r.get2(); r.get1(); r.disp2(); r.disp1(); r.res(); }

Output:
Enter name: Saravana Kumar Enter reg. no.: 090106129046 Enter grades m1 and m2(out of 10): 10 Enter grades m3 and m4(out of 10): 9.8 9.9 9.7

Name: Saravana Kumar Reg. No.: 090106129046

Grade m1: 10 Grade m2: 9.9 Academic points: 9.95

Grade m3: 9.8 Grade m4: 9.7 Sports points: 9.75 Total points: 49.25

Result: I class

Result:
Thus, the C++ program to illustrate the concept of virtual base class was implemented and the output was verified.

Ex. No.: 26 Date:

Constructors in Derived Classes

Aim:
To write a C++ program to illustrate the concept of constructors in derived classes.

Algorithm:
1. A class alpha is created. 2. A data member i is declared and also a constructor, alpha(int i) is also created where the value of i is assigned to x. The required member function is also defined. 3. A class beta is created where the data variables p and q are declared. Also, a constructor, beta(float a, float b) is declared and defined in which a and b are assigned to p and q. The member function, void showbeta() is also defined. 4. A class gamma is created, which is derived from the classes alpha and beta in public mode. The data variables m, n are declared. 5. The constructor of the derived class is defined. 6. In main(), the values of k, l, m, n and o are obtained from the user. 7. The object, g is created for the class gamma which automatically invokes the constructor. 8. The functions, g.showalpha(), g.showbeta() and g.showgamma() are called which display all the required values.

Program:

#include<iostream.h> class alpha { int x; public: alpha(int i) { x=i; } void showalpha() { cout<<"\nx= "<<x; } }; class beta { float p,q; public: beta(float a,float b) { p=a; q=b; }

void showbeta() { cout<<"\np= "<<p<<"\nq= "<<q; } }; class gamma:public beta,public alpha { int m,n; public: gamma(int a,float b,float c,int d,int e):beta(b,c),alpha(a) { m=d; n=e; } void showgamma() { cout<<"\nm= "<<m<<"\nn= "<<n; } }; void main() { int k,n,o; float l,m; cout<<"\nEnter k,n,o: "; cin>>k>>n>>o;

cout<<"\nEnter l,m: "; cin>>l>>m; gamma g(k,l,m,n,o); g.showalpha(); g.showbeta(); g.showgamma(); }

Output:
Enter k,n,o: 5 9 10 Enter l,m: 2.5 7.5 x=5 p=2.5 q=7.5 m=9 n=10

Result:
Thus, the C++ program to illustrate the concept of constructors in derived classes was implemented and the output was verified.

Ex. No.: 27 Date:

Addition and Subtraction of two Polynomials

Aim:
To write a C++ program to perform addition and subtraction of two polynomials and display the sum and difference using operator overloading.

Algorithm:
1. A class polynomial is created where do - nothing, parameterized constructors are defined. 2. The required friend functions for performing addition, subtraction of two polynomials and overloading of << operator. 3. The addition and subtraction of polynomials are performed using the functions poly add(poly q1, poly q2) and poly sub(poly q1, poly q2) and the overloading function is declared as void operator <<(poly z1, poly z2). 4. In the main() function, the objects are created for the class. 5. The coefficients of x3, x2, x and constant terms are obtained for both the polynomials from the user. 6. The sum and difference of both the polynomials are found out by calling the respective functions and displayed.

Program:

#include<iostream.h> class poly { public: int a,b,c,d; poly() { } poly(int w,int x,int y,int z) { a=w; b=x; c=y; d=z; } friend poly add(poly,poly); friend poly sub(poly,poly); friend void operator <<(poly z1,poly z2) { cout<<z1.a<<"x^3+"<<z1.b<<"x^2+"<<z1.c<<"x+"<<z1.d; } };

poly add(poly q1,poly q2) { poly r; r.a=q1.a+q2.a; r.b=q1.b+q2.b; r.c=q1.c+q2.c; r.d=q1.d+q2.d; return r; } poly sub(poly q1,poly q2) { poly s; s.a=q1.a-q2.a; s.b=q1.b-q2.b; s.c=q1.c-q2.c; s.d=q1.d-q2.d; return s; } void main() { poly p1,p2,p3; cout<<"Enter the first polynomial:"; cout<<"\nx^3: "; cin>>p1.a; cout<<"\nx^2: ";

cin>>p1.b; cout<<"\nx: "; cin>>p1.c; cout<<"\nconstant: "; cin>>p1.d; cout<<"\n\nEnter the second polynomial:"; cout<<"\nx^3: "; cin>>p2.a; cout<<"\nx^2: "; cin>>p2.b; cout<<"\nx: "; cin>>p2.c; cout<<"\nconstant: "; cin>>p2.d; cout<<"\n\nSum: "; p3=add(p1,p2); p3<<p3; cout<<"\nDifference: "; p3=sub(p1,p2); p3<<p3; }

Output:
Enter the first polynomial: x^3: 6 x^2: 7 x: 8 constant: 9

Enter the second polynomial: x^3: 1 x^2: 2 x: 3 constant: 4

Sum: 7x^3+9x^2+11x+13 Difference: 5x^3+5x^2+5x+5

Result:
Thus, the C++ program to perform addition and subtraction of two polynomials and displaying the sum and difference using overloading was implemented and the output was verified.

Ex. No.: 28 Date:

Managing Bank Account using Inheritance concept

Aim:
To write a C++ program for managing bank account using inheritance concept.

Algorithm:
1. A class bank is created with the data variables customer name and account number. 2. The derived class saving is created with data variables withdraw, deposit and balance. 3. A class current is derived from the already derived class saving which has the member function, void info() required to display the amount deposited, withdrawn and balance. 4. In main() function, the customer name and the account number are obtained from the user. 5. Using swith-case, the required functions are called using the object, ac of class current.

Program:

#include<iostream.h> class bank { public: char cname[20]; long int acno; void display() { cout<<"\nAccount holder: "<<cname<<"\n"; cout<<"\nAccount number: "<<acno<<"\n"; } }; class saving:public bank { public: int wdraw,dep,bal; void saves() { bal=0; cout<<"\nEnter the amount deposited in Rs: "; cin>>dep; bal+=dep; cout<<"\nEnter the amount to be withdrawn in Rs: ";

cin>>wdraw; bal-=wdraw; cout<<"\nYour account balance is Rs: "<<bal<<"\n"; } }; class current:public saving { public: void info() { cout<<"Last amount withdrawn in Rs: "<<wdraw<<"\n"; cout<<"Last amount deposited in Rs: "<<dep<<"\n"; cout<<"Your account balance is: "<<bal<<"\n"; } }; int main() { int ch; current ac; cout<<"\nEnter the customer name: "; cin>>ac.cname; cout<<"\nEnter your account number: "; cin>>ac.acno; cout<<endl;

while(1) { cout<<"\n\nEnter the account type \n1.Savings \n2.Current \n3.Exit\n"; cin>>ch; switch(ch) { case 1: ac.display(); ac.saves(); break; case 2: ac.display(); ac.info(); break; case 3: break; default: cout<<"\nInvalid choice"; break; } if(ch==3) break; } return 0; }

Output:

Enter the customer name: Saravana Kumar Enter your account no.: 1293

Enter the account type: 1.Savings 2.Current 3.Exit 1 Account holder: Saravana Kumar Account number: 1293 Enter the amount deposited in Rs: 10000 Enter the amount to be withdrawn in Rs: 5000 Your account balance is: 5000

Enter the account type: 1.Savings 2.Current 3.Exit 2 Account holder: Saravana Kumar Account number: 1293 Last amount withdrawn in Rs: 5000 Last amount deposited in Rs: 10000

Your account balance is: 5000 Enter the account type: 1.Savings 2.Current 3.Exit 3

Result:
Thus, the C++ program for managing bank account using inheritance concept was implemented and the output was verified.

You might also like