Program 10 and 11
Program 10 and 11
Experiment No: 10
Write a C++ program to find the sum of the series 1 + x + x2 + ...... + xn using
constructors.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class copy
{
int x,n;
public:
int calculate( );
copy(int xx, int nn)
{
x = xx;
n = nn;
}
};
int copy::calculate( )
{
int t, i, s;
s = 1;
t = x;
for(i = 1; i<=n; i++)
{
s = s + t;
t = t * x;
}
return s;
}
void main( )
{
int x,n;
clrscr( );
cout<<"Input the base and power";
cin>>x>>n;
copy O(x, n);
copy C = O;
cout<<"Object1: Sum of the series =" <<O.calculate( )<<endl;
cout<<"Object2: Sum of the series =" <<C.calculate( )<<endl;
-33-
Experiment No: 11
Write a C++ program to create a base class containing the data members rollno and
name. Also create a member function to read and display the data using the concept of
single level inheritance. Create a derived class that contains marks of two subjects and
total marks as the data members.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class student
{
private:
int regno;
char name[50];
public:
void getdata1( )
{
cout<<"Input the reg no and name"<<endl;
cin>> regno >>name;
}
void display1( )
{
cout<<"Reg no. is "<<regno<<endl;
cout<<"Name is " <<name<<endl;
}
};
void display2( )
{
cout<<"Subject1 ="<<M1<<endl;
cout<<"Subject2 ="<<M2<<endl;
cout<<"Total is= "<<T<<endl;
getch( );
}
};
int main( )
{
marks M;
clrscr( );
M.getdata1( );
M.getdata2( );
M.display1( );
M.display2( );
return 0;
}