100% found this document useful (1 vote)
209 views48 pages

HSC CS 1 C++ Practicals

The document contains C++ code that initializes arrays of different numeric data types (short, float, double, long double) and calculates the sum of elements by traversing the array with a pointer. For each data type, the code prints the starting address of the array, size of elements, sum after each traversal, and ending address. It traverses the array using a pointer and calculates running sum to demonstrate pointer usage and array traversal.

Uploaded by

sujalandhale1
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
100% found this document useful (1 vote)
209 views48 pages

HSC CS 1 C++ Practicals

The document contains C++ code that initializes arrays of different numeric data types (short, float, double, long double) and calculates the sum of elements by traversing the array with a pointer. For each data type, the code prints the starting address of the array, size of elements, sum after each traversal, and ending address. It traverses the array using a pointer and calculates running sum to demonstrate pointer usage and array traversal.

Uploaded by

sujalandhale1
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/ 48

/*Write a program in C++ that first initialises an array of given 10 real

numbers. The program must sorts numbers in ascending or descending


order using bubble sort method. It should prints the given list of numbers
as well as the soted list */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[]={10.1,8.1,7.1,6.4,1.1,4.5,2.1,3.2,6.7,4.9};
cout<<"The original list of element is\n";
for(int i=0;i<10;i++)
{
cout.precision(2);
cout<<a[i]<<"\n";
}
cout<<"\n The sorted list in Ascending order\n";

//sorting nos by bubble sort


for(i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(a[i]<a[j])
{
cout.precision(2);
float s=a[i];
a[i]=a[j];
a[j]=s;
}
}
}
for(i=0;i<10;i++)
{
cout.precision(2);
cout<<a[i]<<"\n";
}
getch();
}
The original list of element is
10.1
8.1
7.1
6.4
1.1
4.5
2.1
3.2
6.7
4.9

The sorted list in Ascending order


1.1
2.1
3.2
4.5
4.9
6.4
6.7
7.1
8.1
10.1
/*Write a program in C++ that first initialises an array of given 10 real
numbers. The program must sorts numbers in ascending or descending
order using bubble sort method. It should prints the given list of numbers
as well as the soted list */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[]={10.1,8.1,7.1,6.4,1.1,4.5,2.1,3.2,6.7,4.9};
cout<<"The original list of element is\n";
for(int i=0;i<10;i++)
{
cout.precision(2);
cout<<a[i]<<"\n";
}
cout<<"\n The sorted list in descending order\n";

//sorting nos by bubble sort


for(i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(a[i]>a[j])
{
cout.precision(2);
float s=a[i];
a[i]=a[j];
a[j]=s;
}
}
}
for(i=0;i<10;i++)
{
cout.precision(2);
cout<<a[i]<<"\n";
}
getch();
}
The original list of element is

10.1

8.1

7.1

6.4

1.1

4.5

2.1

3.2

6.7

4.9

The sorted list in descending order

10.1

8.1

7.1

6.7

6.4

4.9

4.5

3.2

2.1

1.1
/*Write a function in C++ that exchanges data (passing)by reference using
swap functiopn to interchange the given two numbers */
#include <iostream.h>
#include <conio.h>
void swap(int &,int &);
void main()
{
int a,b;
clrscr();
cout<<"\nEnters two numbers";
cin>>a>>b;
cout<<endl<<"Original values : ";
cout<<endl<<"a = "<<a<<" b = "<<b;
swap(a,b);
getch();
}
void swap(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
cout<<endl<<"New values : ";
cout<<endl<<"a = "<<x<<" b = "<<y;
}
Enters two numbers

Original values :

a=4 b=5

New values :

a=5 b=4
/*Write a function in C++ that exchanges data (passing)by reference using
swap functiopn to interchange the given two numbers */
#include<iostream.h>
#include<conio.h>
void swap(float &x,float &y);
void main()
{
clrscr();
float a,b;
cout<<"\n Enters two floating numbers";
cin>>a>>b;
cout<<"\nBefore swap";
cout<<"a="<<a<<" b="<<b;
swap(a,b);
cout<<"\nAfter swap";
cout<<"a="<<a<<" b="<<b;
getch();
}
void swap(float &x, float &y)
{
float t=x;
x=y;
y=t;
}
Enters two floating numbers

8.3

2.5

Before swap a=8.3 b=2.5

After swap a=2.5 b=8.3


/*Write a function in C++ that exchanges data (passing)by reference using
swap functiopn to interchange the given two numbers */
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
clrscr();
int a,b;
cout<<"\nEnters two integer numbers";
cin>>a>>b;
cout<<"\nBefore swapping";
cout<<" a="<<a<<" b="<<b;
swap(&a,&b);
cout<<"\nAfter swapping";
cout<<" a="<<a<<" b="<<b;
getch();
}
void swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Enters two integer numbers

Before swapping a=5 b=6

After swapping a=6 b=5


/*Write a program in C++ that first initialises an array of given 10 sorted
real numbers. The program must is to verify whether the given element
belongs this array or not, using binary search technique. The element to be
searched is to entered at the time of execution. If number is found then
program should print its position in the array otherwise it should print the
number is not found */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
float a[10],p;
int i,top,bot,mid;
cout<<"Type 10 real numbers in ascending order"<<"\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
top=0;
bot=9;
back:cout<<"\ntype the no u want to search \n";
cin>>p;
mid=(top+bot)/2;
while((top<=bot)&&(a[mid]!=p))
{
if(p<a[mid])
bot=mid-1;
else
top=mid+1;
mid=(top+bot)/2;
}
if(a[mid]==p)
{
cout<<"The no is at position="<<(mid+1)<<"\n";
cout<<"\nThe binary search is successuful";
cout<<"\nEnter number not in list for unsuccessful";
goto back;
}
else
cout<<"\noh the search is unsuccessful";
cout<<"\nthe no is not found in entire list\n";
getch();
}
Type 10 real numbers in ascending order

50.1

100.2

150.3

200.4

250.5

300.6

350.7

400.8

450.9

500.10

type the no u want to search

400.8

The no is at position=8

The binary search is successuful

Enter number not in list for unsuccessful

type the no u want to search

600

oh the search is unsuccessful

the no is not found in entire list


/*Write a program in C++ that first initialises an array of five numbers
such as short/float/double. The program must add these numbers by
traversing this array with a pointer. The o/p should print the starting
address of an array with the size of number (n bytes)to what it points. The
program must also print the sum and pointer address with the addition of
every number as well as the ending address */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const size=5;
short a[size]={19,38,57,76,95};
cout<<"Starting Address is:"<<a<<endl;
cout<<"Size of (short):"<<sizeof(short)<<endl;
short *end=a+size;
short sum=0;
cout<<"The traversed array is:\n\n";
for(short*p=a;p<end;p++)
{
sum=sum+*p;
cout<<"\tp="<<p<<endl;
cout<<"\t*p="<<*p<<endl;;
cout<<"\tsum="<<sum<<endl;
}
cout<<"\nEnding Address:"<<end<<endl;
getch();
}
Starting Address is:0x8fc7ffea

Size of (short):2

The traversed array is:

p=0x8fc7ffea

*p=19

sum=19

p=0x8fc7ffec

*p=38

sum=57

p=0x8fc7ffee

*p=57

sum=114

p=0x8fc7fff0

*p=76

sum=190

p=0x8fc7fff2

*p=95

sum=285

Ending Address:0x8fc7fff4
/*Write a program in C++ that first initialises an array of five numbers
such as short/float/double.The program must add these numbers by
traversing this array with a pointer. The o/p should print the starting
address of an array with the size of number (n bytes)to what it points. The
program must also print the sum and pointer address with the addition of
every number as well as the ending address */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const size=5;
float a[size]={0.3,1.2,0.8,2.2,3.4};
cout<<"Starting Address is:"<<a<<endl;
cout<<"Size of (float):"<<sizeof(float)<<endl;
float *end=a+size;
float sum=0;
cout<<"The traversed array is:\n\n";
for(float*p=a;p<end;p++)
{
sum=sum+*p;
cout<<"\tp="<<p<<endl;
cout<<"\t*p="<<*p<<endl;;
cout<<"\tsum="<<sum<<endl;
}
cout<<"\nEnding Address:"<<end<<endl;
getch();
}
Starting Address is:0x8f7effdc

Size of (float):4

The traversed array is:

p=0x8f7effdc

*p=0.3

sum=0.3

p=0x8f7effe0

*p=1.2

sum=1.5

p=0x8f7effe4

*p=0.8

sum=2.3

p=0x8f7effe8

*p=2.2

sum=4.5

p=0x8f7effec

*p=3.4

sum=7.9

Ending Address:0x8f7efff0
/*Write a program in C++ that first initialises an array of five numbers
such as short/float/double.The program must add these numbers by
traversing this array with a pointer. The o/p should print the starting
address of an array with the size of number (n bytes)to what it points. The
program must also print the sum and pointer address with the addition of
every number as well as the ending address */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const size=5;
double a[size]={1.11,2.22,3.33,4.44,5.55};
cout<<"Starting Address is:"<<a<<endl;
cout<<"Size of (double):"<<sizeof(double)<<endl;
double *end=a+size;
double sum=0;
cout<<"The traversed array is:\n\n";
for(double*p=a;p<end;p++)
{
sum=sum+*p;
cout<<"\tp="<<p<<endl;
cout<<"\t*p="<<*p<<endl;;
cout<<"\tsum="<<sum<<endl;
}
cout<<"\nEnding Address:"<<end<<endl;
getch();
}
Starting Address is:0x8f7effc4

Size of (double):8

The traversed array is:

p=0x8f7effc4

*p=1.11

sum=1.11

p=0x8f7effcc

*p=2.22

sum=3.33

p=0x8f7effd4

*p=3.33

sum=6.66

p=0x8f7effdc

*p=4.44

sum=11.1

p=0x8f7effe4

*p=5.55

sum=16.65

Ending Address:0x8f7effec
/*Write a program in C++ that first initialises an array of five numbers
such as short/float/double.The program must add these numbers by
traversing this array with a pointer. The o/p should print the starting
address of an array with the size of number (n bytes)to what it points. The
program must also print the sum and pointer address with the addition of
every number as well as the ending address */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const size=5;
long double a[size]={150.1,300.2,450.3,500.4,550.5};
cout<<"Starting Address is:"<<a<<endl;
cout<<"Size of (long double):"<<sizeof(long double)<<endl;
long double*end=a+size;
long double sum=0;
cout<<"The traversed array is:\n\n";
for(long double*p=a;p<end;p++)
{
sum=sum+*p;
cout<<"\tp="<<p<<endl;
cout<<"\t*p="<<*p<<endl;;
cout<<"\tsum="<<sum<<endl;
}
cout<<"\nEnding Address:"<<end<<endl;
getch();
}
Starting Address is:0x8f7dffb8

Size of (long double):10

The traversed array is:

p=0x8f7dffb8

*p=150.1

sum=150.1

p=0x8f7dffc2

*p=300.2

sum=450.3

p=0x8f7dffcc

*p=450.3

sum=900.6

p=0x8f7dffd6

*p=500.4

sum=1401

p=0x8f7dffe0

*p=550.5

sum=1951.5

Ending Address:0x8f7dffea
/*Write a function in C++ to i/p the given string (including spaces)
and reverses it using function which locates the end of the string and
swap the first character with the last character, the second character
with the second last character and so on */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void swap(char *rev);
void main()
{
clrscr();
char str[80];
cout<<"\nEnters string\n";
cin.getline(str,80);
cout<<"\nThe original string is="<<str;
swap(str);
cout<<"\nAfter swapping string is="<<str;
getch();
}
void swap(char *rev)
{
strrev(rev);
}
Enters string

india is my country

The original string is=india is my country

After swapping string is=yrtnuoc ym si aidni


/*Write a C++ pgrogam which defineds a node class each of whose objects
contains an intege data number and next pointer.The program should allow
the user to create a linked list in reverse order and then traverses the
list printing each data value */
#include<iostream.h>
#include<conio.h>
class node
{
public:node(int d,node*p=0):data(d),next(p)
{
}
int data;
node*next;
};
void main()
{
clrscr();
int n;
node*p;
node*q;
cout<<"\nenter elements in linked list";
cout<<"\nfor last node press control z?"<<endl;
while(cin>>n)
{
p=new node(n,q);
q=p;
}
cout<<"\n The traversed linked list in reverse order"<<endl;
for(;p->next;p=p->next)
{
cout<<p->data<<"->";
}
cout<<"X\n";
getch();
}
enter elements in linked list

for last node press control z?

10

^Z

The traversed linked list in reverse order

10->9->3->7->6->5->X
/*Write a C++ with ratio class using member functions like assign()
function to initialise its member data(integer numerator and denominator),
convert()function converts the ratio into double, invert() function to get the
inverse of the ratio and print() function to print the ratio and its reciprocal*/
#include<iostream.h>
#include<conio.h>
class ratio
{
int num,den;
public:void assign()
{
cout<<"\nEnter numerator";
cin>>num;
cout<<"\nEnter denominator";
cin>>den;
cout<<"\nRatio and Reciprocal is";
}
public:void convert()
{
double c;
c=((num/den));
cout<<"\nThe double conversion is="<<c;
}
public:void invert()
{
int temp;
temp=num;
num=den;
den=temp;
cout<<"\nThe inversion of ratio and reciprocal=";
}
public:void print()
{
cout<<num<<"/"<<den;
}
};
void main()
{
clrscr();
ratio x;
x.assign();
x.print();
x.convert();
x.invert();
x.print();
getch();
}
Enter numerator

Enter denominator

Ratio and Reciprocal is5/2

The double conversion is=2

The inversion of ratio and reciprocal=2/5


/* Implement a circle class in C++. Each object of this class will represents
a circle, storing its radius and x and y co-ordinates of its centre as floats.
Include a default constructor and access function as area() and
circumference(). The program must print the co-ordinates with radius, area
and circuference of the circle */
#include<iostream.h>
#include<conio.h>
class circle
{
float r,x,y,cent,ar,circum;
public:circle()
{
cout<<"\nThis is default constructor";
}
public:void area()
{
cout<<"\nEnter x and y co-ordinates";
cin>>x>>y;
cout<<"\nThe x cordinate of circle is=x="<<x;
cout<<"\nThe y cordinate of circle is=y="<<y;
cent=((x+y)/2);
cout<<"\ncenter of co-ordinate"<<cent;
cout<<"\nEnter radius";
cin>>r;
cout<<"\nThe radius of circle is="<<r;
ar=3.14*r*r;
cout<<"\nThe area of circle="<<ar;
}
public:void circumference()
{
circum=2*3.14*r;
cout<<"\nThe circumference is="<<circum;
}
};
void main()
{
clrscr();
circle s;
s.area();
s.circumference();
getch();
}
This is default constructor

Enter x and y co-ordinates

The x cordinate of circle is=x=3

The y cordinate of circle is=y=4

center of co-ordinate3.5

Enter radius

The radius of circle is=3

The area of circle=28.26

The circumference is=18.84


/*Write a program in C++ that initialises a ratio class with no parameter as
a default constructor. Add a constructor having no parameters that
initialises the declared object with the default integer values -- and -- Add
another constructor that has one integer parameter. This constructor must
initialises the object to be the fractional equivalent of that integer. The o/p
should print the ratio as per the constructor*/
#include<iostream.h>
#include<conio.h>
class ratio
{
public:int num,den;
public:ratio()
{
num=0;
den=0;
}
public:ratio(int n)
{
num=n;
den=1;
}
public:void print()
{
cout<<num<<"/"<<den;
}
};
void main()
{
clrscr();
ratio x(9);
cout<<"\nThe ratio x is=";
x.print();
ratio y(11);
cout<<"\nThe ratio y is=";
y.print();
ratio z(33);
cout<<"\nThe ratio z is=";
z.print();
getch();
}
The ratio x is=9/1
The ratio y is=11/1
The ratio z is=33/1
/*Write a C++ program that initialises a ratio class with no parameters
as a default constructor.The program must print the message "OBJECT IS
BORN" during initialisation. It should display the message "NOW X IS
ALIVE" When the first member function ratio x called. The program must
display "OBJECT DIES", when the destructor is called for object when it
reached the end of its scope*/
#include<iostream.h>
#include<conio.h>
class ratio
{
public:ratio()
{
cout<<"\nAN OBJECT IS BORN";
float num,den,c;
num=22;
den=7;
c=num/den;
cout<<"\nThe ratio is="<<num<<"/"<<den<<"="<<c;
}
public:~ratio()
{
cout<<"\n At the end of scope";
cout<<"\nAN OBJECT IS DIES";
}
};
void main()
{
clrscr();
ratio x;
{
cout<<"\n NOW X IS ALIVE";
}
ratio y;
{
cout<<"\n NOW Y IS ALIVE";
}
getch();
}
AN OBJECT IS BORN
The ratio is=22/7=3.142857
NOW X IS ALIVE
AN OBJECT IS BORN
The ratio is=22/7=3.142857
NOW Y IS ALIVE
At the end of scope
AN OBJECT IS DIES
At the end of scope
AN OBJECT IS DIES
/*write a program in C++ with a complex constructor to adds the given
two complex numbers A=--- and B---. The program should prints the given
complex numbers and their sum */
#include <iomanip.h>
#include <iostream.h>
#include <conio.h>
class complex
{
private:
double real;
double imag;
public:
complex(double = 0.0, double = 0.0);
void add(complex, complex);
void show();
};
complex :: complex (double x, double y)
{
real = x;
imag = y;
}
void complex :: add (complex c1, complex c2)
{
real = c1.real + c2.real;
imag = c1.imag + c2.imag;
}
void complex :: show()
{
if (imag > 0)
cout << real << "+" << imag << "i" << endl;
else
cout << real << imag << "i" << endl;
}
int main ( )
{
clrscr ( );
complex c1 (6, 9);
complex c2 (5,3);
complex c3;
c3.add (c1, c2);
c3.show ( );
getch ( );
return 0;
}
11+12i
/*write a program in C++ with a complex constructor to adds the given
two complex numbers A=--- and B---. The program should prints the given
complex numbers and their sum */
#include <iomanip.h>
#include <iostream.h>
#include <conio.h>
class complex
{
float real,img;
public:complex()
{
real=0;
img=0;
}
complex(float re, float im)
{
real=re;
img=im;
}
void show()
{
cout<<"\nreal number="<<real;
cout<<"\nimaginary number="<<img;
}
complex sum(complex s)
{
complex total;
total.real=real+s.real;
total.img=img+s.img;
return(total);
}
};
void main()
{
clrscr();
complex x1(7.2,8.3);
complex x2(8.3,9.2);
complex x3;
x3=x1.sum(x2);
cout<<"\nThe first complex number is=";
x1.show();
cout<<"\nThe second complex number is=";
x2.show();
cout<<"\nThe sum=";
x3.show();
getch();
}
The first complex number is=
real number=7.2
imaginary number=8.3
The second complex number is=
real number=8.3
imaginary number=9.2
The sum=
real number=15.5
imaginary number=17.5
/*Write a program in C++ to implements the addition and division operator for the ratio
class.Hence it prints the given two ratios x and y and their sum(x+y) and division(x/y)*/
#include<iostream.h>
#include<conio.h>
class ratio
{
float x,y;
public:ratio()
{
}
ratio(float real,float imag)
{
x=real;
y=imag;
}
ratio operator+(ratio);
ratio operator/(ratio);
void display();
};
ratio ratio::operator+(ratio c)
{
ratio temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
ratio ratio::operator/(ratio c)
{
ratio temp;
temp.x=x/c.x;
temp.y=y/c.y;
return(temp);
}
void ratio::display()
{
cout<<" x="<<x<<" y="<<y<<"\n";
}
void main()
{
clrscr();
ratio c1,c2,c3,c4;
c1=ratio(2.5,3.5);
c2=ratio(1.6,2.7);
c3=c1+c2;
c4=c1/c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"\nThe sum is= ";
cout<<"c3=";
c3.display();
cout<<"\nThe division is= ";
cout<<"c4=";
c4.display();
getch();
}
c1= x=2.5 y=3.5
c2= x=1.6 y=2.7

The sum is= c3= x=4.1 y=6.2

The division is= c4= x=1.5625 y=1.296296


#include<iostream.h>
#include<conio.h>
#include<math.h>
class polar
{
float angle,r,x,y;
public: void getdata()
{
cout<<"\nEnters angle";
cin>>angle;
cout<<"\nEnters radius";
cin>>r;
}
void convert()
{
x=r*cos(angle);
y=r*sin(angle);
}
void putdata()
{
cout<<"\nThe polar co-ordinate of points are="<<angle;
cout<<"\nThe rectangular co-ordinate points are"<<"x="<<x<<" y="<<y;
}
};
class rectangle
{
float x,y,a,r;
public:void getdata()
{
cout<<"\nEnters x and y co-ordinates";
cin>>x>>y;
}
void convert()
{
a=atan(x/y);
r=sqrt(x*x+y*y);
}
void putdata()
{
cout<<"\nRectangle co-ordinates are=";
cout<<"x="<<x<<" y="<<y;
cout<<"\nPolar angle="<<a<<" radius="<<r;
}
};
void main()
{
clrscr();
polar p;
p.getdata();
p.convert();
p.putdata();
rectangle q;
q.getdata();
q.convert();
q.putdata();
getch();
}
Enters angle7

Enters radius5

The polar co-ordinate of points are=7


The rectangular co-ordinate points arex=3.769511 y=3.284933
Enters x and y co-ordinates2
2

Rectangle co-ordinates are=x=2 y=2


Polar angle=0.785398 radius=2.828427
#include<iostream.h>
#include<conio.h>
#include<math.h>
class polar
{
float angle,r,x,y;
public:
void getdata(float a,float b)
{
angle=a;
r=b;
}
void convert()
{
x=r*cos(angle);
y=r*sin(angle);
}
void putdata()
{
cout<<"\n THE POLAR CORDINATES OF THE POINT
ARE:"<<angle<<""<<endl;
cout<<"\n THE RECTANGULAR CORDINATES OF THE POINT
ARE:"<<x<<""<<y<<endl;
} };
class rectangle
{
float x,y,a,r;
public:
void getdata(float c,float b)
{
x=c;
y=b;
}
void convert()
{
a=atan(x/y);
r=sqrt(x*x+y*y);
}
void putdata()
{
cout<<"\n THE RECTANGULAR CO-ORDINATES OF THE";
cout<<"\n POINTS ARE"<<"x"<<x<<"y"<<y<<endl;
cout<<"\n THE POLAR CO-ORDINATES OF THE POINTS ARE";
cout<<"angle"<<a<<"radius"<<r<<endl;
} };
void main ()
{ clrscr();
polar p;
p.getdata(2.4,4.6);
p.convert();
p.putdata();
rectangle r;
r.getdata(2.4,4.6);
r.convert();
r.putdata();
getch();
}
THE POLAR CORDINATES OF THE POINT ARE:2.4

THE RECTANGULAR CORDINATES OF THE POINT ARE:-3.3920113.10713

THE RECTANGULAR CO-ORDINATES OF THE


POINTS AREx2.4y4.6

THE POLAR CO-ORDINATES OF THE POINTS


AREangle0.480887radius5.188449
#include<iostream.h>
#include<conio.h>
class student
{
int roll;
public:void getroll()
{
cout<<"\nEnters the roll number of student";
cin>>roll;
}};
class test:public student
{
public:int m1,m2;
public:void getmark()
{
cout<<"\nEnters the marks of two subjects";
cin>>m1>>m2;
}};
class sport
{
public:int marksp;
public:void getsp()
{
cout<<"\nEnters the weightage marks of sport";
cin>>marksp;
}};
class result:public test, public sport
{
int tot;
float per;
public:void display()
{
tot=m1+m2+marksp;
cout<<"\nTotal marks="<<tot;
per=tot/3;
cout<<"\nPercentage="<<per;
}};
void main()
{
clrscr();
result s1;
s1.getroll();
s1.getmark();
s1.getsp();
s1.display();
getch();
}
Enters the roll number of student
5

Enters the marks of two subjects


63
52

Enters the weightage marks of sport25

Total marks=140
Percentage=46
#include<iostream.h>
#include<conio.h>
#include<string.h>
class person
{
public: char *name;
person(char *s)
{
name=new char[strlen(s+1)];
strcpy(name,s);
}
virtual void print()
{
cout<<"My name is="<<name<<endl;
}

};
class student :public person
{
float rno;
public:
student(char *s,float r):person(s),rno(r)
{
}
void print()
{
cout<<"My name is="<<name<<"\nMy Roll Number="<<rno;
}

};

void main()
{
clrscr();
person *p;
person x("BOB");
p=&x;
p->print();
student y("TOM",101);
p=&y;
p->print();
getch();
}
My name is=BOB
My name is=TOM
My Roll Number=101
//program of name of country by using file handeling
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
ofstream fout;
fout.open("country");
fout<<"\nIndia";
fout<<"\nPakistan";
fout<<"\nEngland";
fout<<"\nSothafrica";
fout<<"\nAustralia";
fout.close();
fout.open("capital");
fout<<"\nDelhi";
fout<<"\nLahor";
fout<<"\nLondon";
fout<<"\nCaptown";
fout<<"\nSidany";
fout.close();
const int n=80;
char line[n];
ifstream fin;
fin.open("country");
cout<<"\ncontent of country file\n";
while(fin.eof()==0)
{
fin.getline(line,n);
cout<<"\n"<<line;
}
fin.close();
fin.open("capital");
cout<<"\nContents of capital file\n";
while(fin.eof()==0)
{
fin.getline(line,n);
cout<<"\n"<<line;
}
fin.close();
getch();
}
content of country file

India

Pakistan

England

Sothafrica

Australia

Contents of capital file

Delhi

Lahor

London

Captown

Sidany

You might also like