Headlines
Loading...
C++ program MULTIPLE INHERITANCE

C++ program MULTIPLE INHERITANCE

 MULTIPLE INHERITANCE

/* Sum Of Series x + x^3/3! - x^5/5! + x^7/7! ... */


SOURCE CODE

#include<iostream.h>

#include<conio.h>

class A1 // Base class 1

{

public:

int power(int x,int y) // to calculate the power of x

{

int p=1;

for(int i=1;i<=y;i++)

{

p=p*x;

}

return p;

}


};

class A2 // Base class 2

{

public:

int factorial(int x) // to find the factorialfor denominator of x

{

int fact=1,i;

for(i=1;i<=x;i++)

{

fact=fact*i;

}

return fact;

}


};

class A3:public A1,public A2 // derived class inherits from both the

base classes

{

public:

float series(int x,int n)

{

float ans=0; int y=3; ans=x; // Default values

for(int i=1;i<=n-1;i++)

{

if(i%2==0)

{

ans=ans-1.0*power(x,y)/factorial(y);


// if even position..(-) sign


}

else

{

ans=ans+1.0*power(x,y)/factorial(y);


// if odd position..(+) sign


}

y=y+2; // increments in odd numbers

}

return ans;

}


};

void main()

{

int x,n;

clrscr();

cout<<"Enter the values of X : ";

cin>>x;

cout<<"\nFor how many elements : ";

cin>>n;

A3 obj;

float k = obj.series(x,n); // Calls series function of class A3

cout<<"\nSeries sum = "<<k;

getch();

}


OUTPUT

Enter the values of X : 2

For how many elements : 3

Series sum = 3.066667


/* TO PRINT STUDENT DETAILS IN TABULAR FORMAT */


SOURCE CODE

#include<iostream.h>

#include<conio.h>

class test // Base class-1

{

private :

int rn; // Rollno

char na[20]; //Name

protected:

float m1,m2; //Marks of Paper 1,2

public:

void getdata()

{

cout<<"\n\nEnter Name And Roll No : ";

cin>>na>>rn;

}

void putdata()

{

cout<<endl<<na<<"\t"<<rn<<"\t";

}

void gettest()

{

cout<<endl<<"Enter your marks In Paper-1 And Paper-2 :";

cin>>m1>>m2;

}

void puttest()

{

cout<<m1<<"\t"<<m2<<"\t";

}


};

class sports // Base class-2

{

protected:

float score; // Points scored in sports

public:

void getscore()

{

cout<<endl<<"Enter your sports score :";

cin>>score;

}

void putscore()

{

cout<<score<<"\t";

}


};

class results : public test , public sports // Derived class inherits from both the base classes

{

private :

float total; //total of the marks and score

public :

void putresult()

{

total = m1+m2+score;

cout<<total;

}


};

void main()

{

results s[10];

int n;

clrscr();

cout<<"Enter total number of records : "; // No of records to be displayed

cin>>n;

for(int i=0;i<n;i++) // get data for several students

{

s[i].getdata();

s[i].gettest();

s[i].getscore();

}

cout<<endl<<endl;

cout<<"_______________________________________________________"<<endl;

cout<<endl<<"Name\tRollno\tPaper-1\tPaper-2\tScore\tTotal"<<endl;

cout<<"----------------------------------------------"<<endl;

for(i=0;i<n;i++) // display data for several students

{

s[i].putdata();

s[i].puttest();

s[i].putscore();

s[i].putresult();

}

cout<<endl<<"----------------------------------------------";

getch();

}


OUTPUT

Enter total number of records : 3


Enter Name And Roll No : Nisha 30

Enter your marks In Paper-1 And Paper-2 :78 89

Enter your sports score :45


Enter Name And Roll No : Sunil 25

Enter your marks In Paper-1 And Paper-2 :56 67

Enter your sports score :68


Enter Name And Roll No : Candy 31

Enter your marks In Paper-1 And Paper-2 :89 99

Enter your sports score :89


_______________________________________________________

Name Rollno Paper-1 Paper-2 Score Total

----------------------------------------------

Nisha 30 78 89 45 212

Sunil 25 56 67 68 191

Candy 31 89 99 89 277

----------------------------------------------

*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***