0% found this document useful (0 votes)
15 views3 pages

Program 10 and 11

Computer science

Uploaded by

mariyamnafeesa7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Program 10 and 11

Computer science

Uploaded by

mariyamnafeesa7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

-31-

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;
}
};

class marks : public student


{
private:
int M1,M2,T;
public:
void getdata2( )
{
cout<<"Input two subject marks ";
cin>>M1>>M2;
T = M1+ M2;
}
-34-

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;
}

You might also like