Programs for C++
1. STUDENT MARK LIST
************************
PROGRAM
CODE:
#include<iostream.h>
#include<conio.h>
Class Student
{
private:
long int
no,m1,m2,m3,tot;
float avg;
char n[20];
public:
void getdata();
void putdata();
};
void a::getdata()
{
cout<<"\n
Enter name:";
cin>>n;
cout<<"\n
Enter no:";
cin>>no;
cout<<"\n
Enter m1:";
cin>>m1;
cout<<"\n
Enter m2:";
cin>>m2;
cout<<"\n
Enter m3:";
cin>>m3;
}
void a::putdata()
{
tot=m1+m2+m3;
avg=tot/3;
cout<<"\n\t\t
*********************\n";
cout<<"\n\t\t
STUDENT MARKLIST \n";
cout<<"\n\t\t
*********************\n";
cout<<"\n
Name :\t"<<n;
cout<<"\n
Rollno:\t"<<no;
cout<<"\n
Mark 1 :\t"<<m1;
cout<<"\n
Mark 2 :\t"<<m2;
cout<<"\n
Mark 3 :\t"<<m3;
cout<<"\n
Total :\t"<<tot;
cout<<"\n
Avg:\t\t"<<avg;
}
void main()
{
clrscr();
student. x;
x.getdata();
x.putdata ();
getch();
}
OUTPUT:
Enter name: RAM
Enter no: 100902
Enter m1: 95
Enter m2: 98
Enter m3: 85
********************
STUDENT MARKLIST
********************
Name : RAM
Rollno : 100902
Mark 1: 95
Mark 2: 98
Mark 3: 85
Total
: 278
Avg
: 92
2.
IMPLEMENT CONSTRuCTOR AND DESTRUCTOR
************************************************
PROGRAM
CODE:
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n\n\t\tNo.of
objects created\t"<<count;
}
~alpha()
{
cout<<"\n\n\t\tNo.of
objects destroyed\t"<<count;
count--;
}
};
int main()
{
cout<<"constructor";
clrscr();
cout<<”\n********************************************”;
cout<<”\nIMPLEMENT CONSTRUCTOR
AND DESTRUCTOR”;
cout<<”\n********************************************”:
cout<<"\n\n\tENTER
MAIN\n";
alpha A1,A2,A3,A4;
{
cout<<"\n\n\tENTER
BLOCK1\n";
alpha A5;
}
{
cout<<"\n\n\tENTER
BLOCK2\n";
alpha A6;
}
cout<<"\n\n\tRE-ENTER
MAIN\n";
getch();
return 0;
}
OUTPUT:
***********************************************
IMPLEMENT CONSTRUCTOR
AND DESTRUCTOR
***********************************************
No.of objects created 1
No.of objects created 2
No.of objects created 3
No.of objects created 4
ENTER
BLOCK1
No.of objects created 5
No.of objects destroyed 5
ENTER
BLOCK2
No.of objects created 5
No.of objects destroyed 5
RE-ENTER
MAIN
3. MATRIX MANIPULATION
**************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<process.h>
class matrix
{
int r,c,m[10][10];
public:
matrix()
{
r=0;
c=0;
}
void get();
void display();
matrix add(matrix);
matrix multiply(matrix);
~matrix()
{
}
};
void matrix::get()
{
cin>>r>>c;
cout<<"\nENTER
ELEMENTS:\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>m[i][j];
}
}
}
void matrix::display()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<m[i][j]<<"\t";
}
cout<<"\n";
}
}
matrix matrix::add(matrix p)
{
matrix s;
s.r=r;
s.c=c;
if(r==p.r&&c==p.c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
s.m[i][j]=m[i][j]+p.m[i][j];
}
}
}
else
{
cout<<"\n\t\t ADDITION
IS NOT POSSIBLE";
s.r=0;
s.c=0;
}
return(s);
}
matrix matrix::multiply(matrix x)
{
matrix s;
if(c==x.r)
{
s.r=r;
s.c=c;
for(int i=0;i<r;i++)
{
for(int j=0;j<x.c;j++)
{
s.m[i][j]=0;
for(int k=0;k<x.c;k++)
{
s.m[i][j]=s.m[i][j]+m[i][k]*x.m[k][j];
}
}
}
}
else
{
cout<<"\n CANNOT
DISPLAY:";
s.r=0;
s.c=0;
}
return(s);
}
void main()
{
char con;
int ch;
clrscr();
do
{
matrix a,b,c;
cout<<"\n\t........";
cout<<"\n\t\t.........MATRIX
MANIPULATION...........";
cout<<"\n\t
......";
cout<<"\n 1.ADD \n
2.MULTIPLY \n ENTER UP CHOICE:";
cin>>ch;
cout<<"\n ENTER THE
SIZE OF MATRIX 1:";
a.get();
cout<<"\n ENTER THE
SIZE OF MATRIX 2:";
b.get();
cout<<"\n MATRIX
1:\n";
a.display();
cout<<"\n MATRIX
2:\n";
b.display();
switch(ch)
{
case 1:
{
cout<<"\n ADDITION
RESULT:\n";
c=a.add(b);
c.display();
break;
}
case 2:
{
cout<<"\n
MULTIPULATION RESULT:\n";
c=a.multiply(b);
c.display();
break;
}
}
cout<<"\n DO U WISH TO
CONTINUE(Y/N):";
con=getch();
}
while(con=='y');
getch();
}
OUTPUT:
Enter elements:
1
2
3
4
Enter the size of matrix 2: 2 2
Enter elements:
1
2
4
5
Matrix 1:
1 2
3 4
Matrix 2:
1 2
4 5
Addition result:
2 4
7 9
Do u wish to contiune(y/n):
4.
FRIEND AND STATIC FUNCTION
****************************************
PROGRAM
CODE:
#include<iostream.h>
#include<conio.h>
class mean
{
static int a,b;
public:
static void set()
{
cout<<"\n\t\t\t*******************************”;
cout<<"\n\t\t\t FRIEND AND STATIC
FUNCTION";
cout<<"\n\t\t\t
*******************************";
cout<<"\n\n Enter A,B:";
cin>>a>>b;
}
friend
float meanval(mean m)
{
return
float (m.a+m.b)/2.0;
}
};
int mean ::a;
int mean ::b;
void main()
{
clrscr();
mean m;
mean ::set();
cout<<"\n\t\t\t MEAN VALUE OF A
& B IS";
cout<<meanval(m);
getch();
}
OUTPUT:
*******************************
FRIEND AND STATIC FUNCTION
*******************************
Enter A,B : 12 1
3
MEAN VALUE
OF A &
B IS
MEAN VALUE = 12.5
5. OPERATOR OVERLOADING
****************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
class ex
{
int a,b,c;
public:
void get(int x,int y,int z);
void put(void);
void operator-();
};
void ex::get(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
void ex::put(void)
{
cout<<"a="<<a<<'\t';
cout<<"b="<<b<<'\t';
cout<<"c="<<c<<"\n";
}
void ex::operator-()
{
a=-a;
b=-b;
c=-c;
}
int main()
{
clrscr();
cout<<"\t\t\t
******************\n";
cout<<"\t\t\t Operator
Overloading\n";
cout<<"\t\t\t
******************\n";
ex w;
w.get(100,-200,300);
cout<<”\n\t BEFORE THE OVERLOADING”;
w.put();
cout<<”\n\t AFTER THE
OVERLOADING”;
-w;
w.put();
getch();
return 0;
}
OUTPUT:
*******************
Operator Overloading
*******************
BEFORE THE OVERLOADING
a=100 b=-200
c=300
AFTER THE OVERLOADING
a=-100 b=200
c=-300
6. FUNCTION
OVERLOADING
**********************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
int volume(int);
double
volume(double,int);
long
volume(long,int,int);
int main()
{
cout <<
volume(10) <<"\n";
cout <<
volume(2.5,8) <<"\n";
cout <<
volume(100L,75,15) <<"\n";
getch();
return 0;
}
int volume(int s)
{
return(s*s*s);
}
double volume(double
r,int h)
{
return(3.14519*r*r*h);
}
long volume(long l,int
b,int h)
{
return(l*b*h);
}
OUTPUT:
.......FUNCTION
OVERLOADING.......
1000
157.2595
112500
7.
MULTIPLE INHERITANCE
***************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get-m(int);
};
class N
{
protected:
int n;
public:
void get-n(int);
};
class p:public M,publicN
{
public:
void display(void);
};
void M::get-m(int x)
{
m=x;
}
void N::get-n(int y)
{
n=y;
}
void p::display(void)
[
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
cout<<"m*n="<<m*n<<"\n";
}
int main( )
{
p P;
P.get-m(10);
P.get-n(20);
P.display( );
getch( );
return 0;
}
OUTPUT:
m=10
n=20
m*n=200
8. MULTILEVEL INHERITANCE
******************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student::get_number(int a)
{roll_number=a;}
void student::put_number()
{cout<<"ROLL
NO:"<<roll_number<<"\n";}
class test:public student
{
protected:
float sub1;
float sub2;
public:
void get_marks(float,float);
void put_marks(void);
};
void test::get_marks(float
x,float y)
{sub1=x;sub2=y;}
void test::put_marks()
{
cout<<"MARKS IN SUB
1="<<sub1<<"\n";
cout<<"MARKS IN SUB
2="<<sub2<<"\n";
}
class result:public test
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=sub1+sub2;
put_number();
put_marks();
cout<<"TOTAL="<<total<<"\n";
}
main()
{
clrscr();
result student1;
student1.get_number(111);
student1.get_marks(75.0,59.5);
student1.display();
getch();
return 0;
}
OUTPUT:
ROLL NUMBER: 111
MARKS IN SUB1 = 75
MARKS IN SUB2 = 59.5
TOTAL= 134.5
9. HIERARCHICAL INHERITANCE
***********************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void getnumber()
{
cout<<"\n enter
number:::\t";
cin>>a;
}
};
class B : public A
{
public:
void square()
{
getnumber();
cout<<"\n square of the number:::\t"<<(a*a);
cout<<"\n_________________________________";
}
};
class C : public A
{
public:
void cube()
{
getnumber();
cout<<"\n\n cube of the
number:::\t"<<(a*a*a);
cout<<"\n\n ________________________________";
}
};
void main()
{
clrscr();
cout<<"\n\t\t*******************************\n";
cout<<"\n\t\t HIERARCHICAL INHERITANCE\n";
cout<<"\n\t\t*******************************\n";
B b1;
b1.square();
C c1;
c1.cube();
getch();
}
OUTPUT:
****************************
HIERARCHICAL INHERITANCE
****************************
Enter number::: 3
Square of the number::: 9
______________________
Enter number::: 2
Cube of the number::: 8
______________________
10. HYBRID INHERITANCE
**************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
class
student
{
protected:
int
roll_number;
public:
void
get_number(int a)
{
roll_number=a;
}
void
put_number(void)
{
cout<<"Roll
no:"<<roll_number<<"\n";
}
};
class
test:public student
{
protected:
float
part1,part2;
public:
void
get_marks(float x,float y)
{
part1=x;part2=y;
}
void
put_marks(void)
{
cout<<"marks
obtained:"<<"\n"
<<"part1="<<part1<<"\n"
<<"part2="<<part2<<"\n";
}
};
class
sports
{
protected:
float
score;
public:
void
get_score(float s)
{
score=s;
}
void
put_score(void)
{
cout<<"sports
wt:"<<score<<"\n\n";
}
};
class
result:public test,public sports
{
float
total;
public:
void
display(void);
};
void
result::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"Total
score:"<<total<<"\n";
}
int
main()
{
result
student_1;
student_1.get_number(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
getch();
return
0;
}
OUTPUT:
Roll no: 160587
Marks obtained:
part1=27.5
part2=33
sports wt:6
Total score:66.5
11. STRING FUNCTIONS
**********************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
clrscr();
cout<<"\n\t\t*******************\n";
cout<<"\n\t\t STRING FUNCTIONS\n";
cout<<"\n\t\t********************\n";
cout<<"\n Enter any string:-
\t";
cin>>s1;
strcpy(s2,s1);
strrev(s2);
cout<<"\n Enterd string is :-
"<<s1
<<"\n & No.of letters in
string :-"<<strlen(s1)
<<"\n & Reverse string is
:- "<<s2;
if(strcmp(s1,s2)==0)
cout<<"\n\nSo, Entered string is a palindrome.";
else
cout<<"\n\nSo, Entered string is not a palindrome.";
getch();
}
OUTPUT:
******************
STRING
FUNCTIONS
******************
Enter any string:- MADAM
Enterd string is :- MADAM
No.of letters in string :- 5
Reverse string is :- MADAM
So, Entered string is not a
palindrome.
12. IMPLEMENT STACK USING ARRAY
***********************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout
<<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout
<<"stack under flow";
return;
}
cout
<<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<"
stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i]
<<" ";
}
};
main()
{
int
ch;
stack
st;
while(1)
{
cout <<"\n1.push 2.pop
3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter
the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT:
*******************************
IMPLEMENT STACK USING ARRAY
*******************************
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow
Enter ur choice2
stack under flow
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3
Enter ur choice1
enter the element3
inserted3
1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted3
Enter ur choice2
deleted3
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element5
inserted5
Enter ur choice1
enter the element5
inserted5
1.push 2.pop 3.display 4.exit
Enter ur choice3
5 2
Enter ur choice3
5 2
1.push 2.pop 3.display 4.exit
Enter ur choice4
Enter ur choice4
13.
IMPLEMENTATION OF QUEUE USING LINKED LIST
*************************************************
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue
over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout
<<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout
<<"queue under flow";
return;
}
cout
<<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<"
queue empty";
return;
}
for(int
i=front+1;i<=rear;i++)
cout <<queue1[i]<<"
";
}
};
main()
{
int ch;
queue qu;
while(1)
{
cout
<<"\n1.insert 2.delet 3.display
4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin
>> ch;
qu.insert(ch);
break;
case 2: qu.delet();
break;
case 3:
qu.display();break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT:
****************************
IMPLEMENTATION
OF QUEUE
****************************
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element21
inserted21
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element22
inserted22
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element16
inserted16
1.insert 2.delet 3.display 4.exit
Enter ur choice3
21 22 16
1.insert 2.delet 3.display 4.exit
Enter ur choice2
deleted21
1.insert 2.delet 3.display 4.exit
Enter ur choice4
14. QUICK SORT
************
PROGRAM
CODE:
#include<iostream.h>
#include<conio.h>
int a[10],l,u,i,j;
void quick(int *,int,int);
void main()
{
clrscr();
cout <<"enter 10
elements";
for(i=0;i<10;i++)
cin >> a[i];
l=0;
u=9;
quick(a,l,u);
cout <<"sorted
elements";
for(i=0;i<10;i++)
cout << a[i] <<
" ";
getch();
}
void quick(int a[],int l,int u)
{
int p,temp;
if(l<u)
{
p=a[l];
i=l;
j=u;
while(i<j)
{
while(a[i] <= p && i<j )
i++;
while(a[j]>p && i<=j )
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;}
}
temp=a[j];
a[j]=a[l];
a[l]=temp;
cout <<"\n";
for(i=0;i<10;i++)
cout <<a[i]<<" ";
quick(a,l,j-1);
quick(a,j+1,u);
}
}
OUTPUT:
enter 10 elements5 2 3 16 25 1 20
7 8 61 14
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 8 16 20 7 25 61
1 2 3 5 7 8 20 16 25 61
1 2 3 5 7 8 16 20 25 61
1 2 3 5 7 8 16 20 25 61
sorted elements1 2 3 5 7 8 16 20
25 61
15.
INSERTION SORT
*****************
PRORAM CODE:
#include <iostream.h>
#include <conio.h>
using namespace std;
int main()
{
int a[16], i, j, k, temp;
cout<<"enter the
elements\n";
for (i = 0; i < 16; i++)
{
cin>>a[i];
}
for (i = 1; i < 16; i++)
{
for (j = i; j >= 1; j--)
{
if (a[j] < a[j-1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
else
break;
}
}
cout<<"sorted
array\n"<<endl;
for (k = 0; k < 16; k++)
{
cout<<a[k]<<endl;
}
getch();
}
OUTPUT:
enter the elements
15
11
8
5
2
14
10
7
4
1
13
9
6
3
0
12
sorted array
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Comments
Post a Comment