0% found this document useful (0 votes)
25 views21 pages

All Programs C++ Class 12 HSC

The document contains a series of C++ programming experiments, each detailing specific tasks such as sorting arrays, swapping values, and implementing classes with various functionalities. Each experiment includes code snippets, expected outputs, and instructions for verification and hardcopy generation. The experiments cover a range of topics including sorting algorithms, class hierarchies, operator overloading, file handling, and the use of virtual functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views21 pages

All Programs C++ Class 12 HSC

The document contains a series of C++ programming experiments, each detailing specific tasks such as sorting arrays, swapping values, and implementing classes with various functionalities. Each experiment includes code snippets, expected outputs, and instructions for verification and hardcopy generation. The experiments cover a range of topics including sorting algorithms, class hierarchies, operator overloading, file handling, and the use of virtual functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

Experiment No 1

a) Write a program in C++ that first initializes an array of given 10 real numbers.
The program must sort numbers in ascending order using Bubble Sort method. It
should print the given list of numbers as well as the sorted list.
b) Enter the program and verify proper execution of the same on the computer.
c) Obtained a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z[10],n;
cout<<"enter any 10 no\n";
for(x=0;x<=9;x++)
{
cin>>z[x];
}
cout<<"number after sorting="<<"\n";
for(x=0;x<=8;x++)
{
for(y=0;y<=9-x;y++)
{
if(z[x]<z[y])
{
int temp;
temp=z[x];
z[x]=z[y];
z[y]=temp;
}}}
for(x=0;x<=9;x++)
{
cout<<z[x]<<"\t";
}
getch();
}
-----------------------------------------------------------------------
output
enter any 10 no
87 88 90 81 85 86 84 82 89 83
number after sorting=
81 82 83 84 85 86 87 88 89 90
Experiment No 2
a) Write a program in C++ that first initializes an array of given 10 real numbers.
The program must sort numbers in descending order using Bubble Sort method. It
should print the given list of numbers as well as the sorted list.
b) Enter the program and verify proper execution of the same on the computer.
c) Obtained a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z[10],n;
cout<<"enter any 10 no";
for(x=0;x<=9;x++)
{
cin>>z[x];
}
cout<<"number after sorting="<<"\n";
for(x=0;x<=8;x++)
{
for(y=0;y<=9-x;y++)
{
if(z[x]>z[y])
{
int temp;
temp=z[x];
z[x]=z[y];
z[y]=temp;
}}}
for(x=0;x<=9;x++)
{
cout<<z[x]<<"\t";
}
getch();
}
-----------------------------------------------------------------------
output
enter any 10 no 81 82 83 84 85 86 87 88 89 90

number after sorting=

90 89 88 87 86 85 84 83 82 81
Experiment No 3
a) Write a program in C++ that exchanges data (passing by reference) using swap
function to interchange the given two numbers.
b) Enter the program and verify proper execution of the same on the computer
c) Obtain hardcopy of the program listing as well as output. The output must list the
given numbers before as well as after swapping.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void swap(int*,int*);//function declaration
void main()
{
int a,b;
clrscr();
cout<<"\nEnter the values of A and B:";
cin>>a>>b;
swap(&a,&b);//call by reference
cout<<"\n\t\tA="<<a<<"and"; cout<<"B="<<b;
getch();
}
void swap(int *a,int *b)//function definition
{
int t;
t=*a;
*a=*b;
*b=t;
}
OUTPUT: Enter the values of A and B: 78 43
A=43 and B=78
Experiment No
a) Write a program in C++ that first initializes an array of given 10 sorted real
numbers. The program must verify whether a given element belongs this array or not,
using Binary Search technique. The element (to be searched) is to be entered at the
time of execution. if the number is found, the program should print its position in the
array otherwise it should print "The number not found."
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int i, arr[10], num, first, last, middle;
cout<<"Enter 10 Elements (in ascending order):";
for(i=0; i<10; i++)
{cin>>arr[i]; }
cout<<"\nEnter Element to be Search: ";
cin>>num;
first = 0;
last = 9;
middle = (first+last)/2;

while(first <= last)


{
if(arr[middle]<num)
first=middle+1;
else if(arr[middle]==num)
{
cout<<"\nThe number "<<num<<" found at Position "<<middle+1;
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
cout<<"\nThe number "<<num<<" is not found in given Array";
cout<<endl;
getch();
return 0;
}

output:-
Enter 10 Elements (in ascending order):1 2 3 4 5 6 7 8 9 10

Enter Element to be Search: 5

The number 5 found at Position 5


Experiment No 06
a) Write a program in C++ with a ratio class using member function like assign
function to initialize its member data, integer numerator and denominator. Convert
function to convert the ratio into double, invert function to get the inverse of the
ratio and print function to print the ratio and its reciprocal.
b) Enter the program and verify proper execution of the same on the computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
class ratio
{
int num, den;
double convert()
{
return((double)num/den);
}
double invert(double x)
{
return(1.0/x);
}
public:
void assign()
{
cout << "\nEnter Numerator and denominator of a number ";
cin >> num >> den;
}
void print();
};
void ratio :: print()
{
double x = convert();
double y = invert(x);
cout << "\nRatio of numbers is " << x << " \n& inverse of ratio is " << y;
}
void main()
{
clrscr();
ratio r;
r . assign();
r . print();
getch();
}
Enter Numerator and denominator of a number
45 22
Ratio of numbers is 2.045455
& inverse of ratio is 0.488889
Experiment No 05
a) Implement a circle class in C++. Each object of this class will represent a circle,
storing its radius and x y coordinates of its centers as float. Include a default
constructor, access function, an area function and a circumference function. The
program must print the coordinates with radius, area and circumference.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
class circle
{
int r,x,y;
float area()
{
return(3.14*r*r);
}
float circum()
{
return(2*3.14*r);
}
public:circle()
{
int a;
cout<<"enter value of radius";
cin>>a;
cout<<"enter value of x & y coordinates";
cin>>x>>y;
}
void access()
{
cout<<"\nradius="<<r;
cout<<"\nthe coordinates of a r"<<x<<y;
cout<<"\ncircumference of circle="<<circum();
cout<<"\n area of circle="<<area();
}
};
void main()
{
clrscr();
circle c1;
c1.access();
getch();
}
-------------------------------------------------------------
OUTPUT
enter value of radius7
enter value of x & y coordinates6 8
radius=7
the coordinates of a r6 8
circumference of circle=43.96
area of circle=153.86
Experiment No
a) Write a program in C++ that initializes a demo class with no parameter as a
default constructor. The program must print the message “Object is born” during
initialization. It should display the message “Now X is alive” when the first
member function print is called. It should display “Object dies” when the class
destructor is called for the object, when it reaches the end of its scope.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<conio.h>
#include<iostream.h>
class ratio
{
int n;
public:
ratio();
void x();
~ratio();
}
ratio::ratio()
{ cout<<"object is born"<<"\n";
}
void ratio:: x()
{
cout<<"object is alive";
}
ratio::~ratio()
{
cout<<"object is dies";
}
void main()
{
clrscr();
ratio r;
r.x();
getch();
}
output
object is born
object is alive
object is dies
Experiment No 07
a) Write a program in C++ with a complex constructor to add the given two complex
numbers A = _____ and B = _____. The program should print the given complex
numbers and their sum.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:complex()
{
}
complex(float r,float i)
{
x=r;
y=i;
}
complex operator+(complex c);
void show();
};
complex complex:: operator+(complex c)
{
complex t;
t.x=x+c.x;
t.y=y+c.y;
return(t);
}
void complex::show()
{
cout<<x<<"+j"<<y;
}
void main()
{
complex a,b,c;
a=complex(4.5,1.3);
b=complex(3.1,1.2);
c=a+b;
clrscr();
cout<<"\n a=";
a.show();
cout<<"\n b=";
b.show();
cout<<"\n c=";
c.show();
getch();
}
output:-

a=4.3+j1.3
b=2.3+j7.2
c=6.6+j8.5
Experiment No
a) Write a program in C++ to implement the addition and division operator for the
ration class. Hence print the given two ratios x and y, their sum (x + y) and their
division (x / y).
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
class ratio
{
float a;
public:
void input()
{
cout << "\nEnter any real number: ";
cin >> a;
}
void output()
{
cout <<”\n”<< a;
}
ratio operator +(ratio);
ratio operator /(ratio);
};
ratio ratio :: operator +(ratio y)
{
ratio x;
x.a = a + y.a;
return(x);
}
ratio ratio :: operator /(ratio y)
{
ratio x;
x.a = a / y.a;
return(x);
}
void main()
{
ratio x, y, z;
clrscr();
x . input();
y . input();
z = x + y;
cout<<"\
n******************************OUTPUT***********************\n"
cout << "\n\tAddition is:";
z . output();
z = x / y;
cout << "\n\tDivision is: ";
z . output();
getch();
}
Enter any real number: 9
9
Enter any real number: 3
3
******************************OUTPUT***********************
Addition is: 12
Division is: 3
Experiment No 09
a) Write a program in C++ to implement the following class hierarchy Class student
to obtain Roll No., Class test to obtain marks scored in two different subjects,
Class Sports to obtain weightage(marks) in sports and class result to calculate the
total marks. The program must print the roll no. individual marks obtained in two
subjects, sports and total marks.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void getdata()
{
cout<<"enter the roll no. of student\n";
cin>>rno;
}
void putdata()
{
cout<<"student roll no.="<<rno<<endl;
}
};
class test:public student
{
protected:
int sub1,sub2;
public:
void getmarks()
{
cout<<"enter the marks of two subject"<<endl;
cin>>sub1>>sub2;
}
void putmarks()
{
cout<<"marks in subject1="<<sub1<<endl;
cout<<"marks in subject2="<<sub2<<endl;
}
};
class sports
{
protected:
int score;
public:
void getscore()
{
cout<<"\nenter the sports score"<<endl;
cin>>score;
}
void putscore()
{
cout<<"score of sports="<<score<<endl;
}
};
class result:public test,public sports
{
private:
int total;
public:
void display()
{
total=sub1+sub2+score;
cout<<"total="<<total<<endl;
}
};
void main()
{
result obj;
obj.getmarks();
obj.putmarks();
obj.getscore();
obj.putscore();
obj.display();
getch();
}
OUTPUT
enter the roll no. of student
105
student roll no.= 105
enter the marks of two subject 60 61
marks in subject1=60
marks in subject2=61
enter the sports score 62
total=183
Experiment No.
a)Write a program in C to read the name of the country from one text file and name of
its corresponding capital city from another text file. The program must display the
country name and indicate corresponding capital in the output
b) Enter the program and verify proper execution of the same on the computer
C) Obtain the hardcopy of the program listing as well as output.*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
cout<<"program to print the country and the capital:\n"<<endl<<endl;
ofstream fout;
fout.open("country");
fout<<"united states of america"<<endl;
fout<<"india"<<endl;
fout<<"south korea"<<endl;
fout<<"\njapan";
fout<<"\nchnina";
fout.close();

fout.open("capital");
fout<<"\nWaasington DC";
fout<<"\ndelhi";
fout<<"\nseout";
fout<<"\ntokyo";
fout<<"\ntibet";
fout.close();

const N=80;
char line[N];
ifstream fin;
fin.open("country");
cout<<"content of the country file"<<endl<<endl;
while(fin)
{
fin.getline(line,N);
cout<<line<<endl;
}
fin.close();

fin.open("capitel");
cout<<"content of thecapital file"<<endl<<endl;
while(fin)
{
fin.getline(line,N);
cout<<line<<endl;
}
fin.close();
getch();
}

output

content of the country file


united states of america
india
south korea
japan
chnina

content of thecapital file


Waasington DC
delhi
seout
tokyo
tibet
Experiment
a) Write a program in C++ using virtual function. The program must declare p to
be a pointer to objects of the base class person. First, the program must assign p
to point to an instance x (name of the person e.g. "BOB" ) of class person. The
program must then assign p to point at an instance y (name of the student eg.
"TOM") of the derived class student. Define a print() function in the base class
such that it invokes the same base class function to print the name of the person
by default. The second call for the same should evoke the class function to print
the name of the student.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class x
{ char name[10];
public: x()
{

strcpy(name,"Bob");
}
virtual void print()
{

cout<<"\n\n\tPerson name is"<<name;


}

};
class y:public x
{
char name[10];
public: y()
{
strcpy(name,"Tom");
}
void print()
{

cout<<"\n\n\tPerson name is"<<name;


}

};
void main()
{

clrscr();
x A;
y B;
x *p; p=&A;
cout<<"\nP now point to base class";
p->print();
p=&B;
cout<<"\n\n\P now point to derived class";
p->print();
getch();
}
OUTPUT:
P now point to base class
Person name is Bob
P now point to derived class
Person name is Tom

Experiment No. 13
a) Write a function of C++ to input the given string (including spaces) and reverse it
using a function which locate the end of string and swap the first character with last
character.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[80];
int j, k, n;
clrscr();
cout << "Enter any string including spaces\n\n";
cin . getline(str, 80);
n = strlen(str);
for(j=0, k=n-1; j<k; j++, k--)
{
char temp = str[j];
str[j] = str[k];
str[k] = temp;
}
cout << "\n\nThe reverse string is\n\n" << str;
getch();
}
#output:-
Enter the string:- nlamar
Reverse of string:- raman

Experiment No. 10
a) Two classes polar and rectangle in C++ to represent points in polar and rectangular
systems from one system to other.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<stdio .h>
#include<conio.h>
class polarClass
{
public:
float r,th;
polarClass (float a, float b)
{ r=a; th=b;}
void show()
{

cout<<"In polarform:\n\tr="<<r<<"and theata="<<th;} };


class rectangularClass
{

float x,y,r;
public:
rectangular Class(polar Class p)
{

x=p.r*cos(p.th);
y=r*sin(p.th);}
voidshow()
{ cout<<"\n\In Rectangularform:\n\tx="<<x<<"and y="<<y;} };
void main()
{drscr();
float rad, thet;
cout<<"\n\tEnter the value of radius and theta:";
cin>>rad>>thet;
polar class p(rad,thet);
p.show();
rectangularClass r(P);
r=p;
r.show();
geth();
}
OUTPUT:
Enter the value of radius and theta:44 23
In polar form:
r=44 and theta=23
In rectangular form:
x=-23.444653 and y=-4.002091e-10

You might also like