You are on page 1of 50

EX.

NO: 01
STACK OPERATIONS
DATE:

AIM:
To create a class, to implement the data structure STACK with a constructor to

initialize the top of stack and create a member function PUSH ( ) and POP( ) to insert &delete

the element in the stack.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create the class to implement data structure stack.

Step 3 : Write a constructor to initialize the top of the stack.

Step 4 : Create PUSH ( ) and POP ( ) function to insert and delete the element & also
check the overview conditions.

Step 5 : Create DISPLAY() function to display the element in stack.

Step 6 : Create an object in the main() program to invoke all the member functions.

Step 7 : In switch structure, if choice=1, invoke PUSH ( ) function.

Step 8 : In switch structure, if choice=2, invoke POP ( ) function.

Step 9 : In switch structure, if choice=3, invoked DISPLAY ( ) function.

Step 10 : In switch structure, if choice=4, invoke EXI program.

Step 11 : Stop the process.

1
/*STACK OPERATIONS*/
#include<iostream.h>
#include<conio.h>
#include<process.h> int
top;
class STACK
{ int
a[10];
public:
STACK()
{
top=0;
}
void display();
void push(int ele)

{ top=top+
1;
a[top]=ele;
}
void pop()
{ int
ele;
ele=a[top];
top=top-1;
cout<<"\n The current top element is"<<ele<<"is deleted";
}
};
void STACK::display()
{ cout<<"\n";
for(int i=top; i>0; i--)
cout<<a[i]<<"\n";
}
void main()

{ clrscr
();
int ch,e;
STACK obj;
do
{ clrscr();

2
cout<<"\n\t\t
STACK
OPERATION
S"; cout<<"\n\
t\t
************
*******";
cout<<"\n
MENU";
cout<<"\n --------"; cout<<"\n
1)PUSH"; cout<<"\n 2)POP";
cout<<"\n 3)DISPLAY";
cout<<"\n 4)EXIT"; cout<<"\n
Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: if(top>=7)
cout<<"\n Stack is full";
else
{
cout<<"\n Enter the element:";
cin>>e;
obj.push(e);
}
break;
case 2: if(top<0)
cout<<"\n Stack is empty";
else
obj.pop();
break;
case 3: if(top<=0)
cout<<"\n The stack is empty";
else
{
cout<<"\n\t Stack items are: \n\t";
obj.display();
} break;
case 4: cout<<"\n Program is terminated";
}
getch();

3
}
while(ch!=4);
getch();
}
OUTPUT:

STACK OPERATIONS
*******************

MENU
---------
1) PUSH
2) POP
3) DISPLAY
4) EXIT

Enter your choice: 1

Enter the element: 45

Enter your choice: 1

Enter the element: 35

Enter your choice: 1

Enter the element: 55

Enter your choice: 2

The current top element 55 is deleted

Enter your choice: 3

Stack items are:


35
45

Enter your choice: 4

Program is terminated.

4
EX.NO: 02 ARITHMETIC OPERATIONS
DATE:

AIM:
To create class ARITH, which consists of float & integer variable and create the

function ADD(), SUB(), MUL() and DIV() to get and display values.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create the class ARITH with float & integer variables.

Step 3 : Using member function getdata(), get of integer & float values.

Step 4 : Create member function ADD() to carryout addition of integer & float values.

Step 5 : Create member function SUB() to carryout subtraction of integer & float values.

Step 6 : Create member function MUL() to carryout multiplication of integer & float
values.

Step 7 : Create member function DIV() to carryout division of integer & float values.

Step 8 : Create member function putdata() to call respective functions and display the
result.

Step 9 : In the main program, create an object for the class using object with dot operator
invoke all the above member function.

Step 10 : Stop the process.

5
/*ARITHMETIC OPERATIONS*/

#include<iostream.h>
#include<conio.h>
#include<math.h>
class ARITH
{
public : void
getdata(void); float
add(int, float); float
sub(int, float); float
mul(int, float); float
div(int, float);
};
float ARITH::add(int a, float b)
{
float y=a+b;
return(y);
}
float ARITH::sub(int a, float b)
{ float y = a-b;
return(y);
}
float ARITH::mul(int a, float b)
{ float y = a*b;
return(y);
}
float ARITH::div(int a, f loat b)
{ float y = a/b;
return(y);
}

void main()
{ int a;
float
b;
ARITH A;
clrscr();
cout<<"\n\t\t\t\t ARITHMETIC OPERATIONS";

6
cout<<"\n\t\t\t\t *************************";
cout<<"\n Enter the integer & float values: \n";
cin>>a>>b; cout<<”\n\t\t Arithmeticoperations”;
cout<<”\n\t\t ******************”;
cout<<"\n
======================================="; cout<<"\
n ADDITION = "<<A.add(a,b)<<"\n";
cout<<"\n SUBTRACTION = "<<A.sub(a,b)<<"\n";
cout<<"\n MULTIPLICATION = "<<A.mul(a,b)<<"\n";
cout<<"\n DIVISION = "<<A.div(a,b)<<"\n";
cout<<"\n =======================================";
getch();
}

OUTPUT:

ARITHMETIC OPERATIONS
**************************

7
Enter the integer & float values:

10 2.5

Arithmetic operations
*****************

===========================================

ADDITION = 12.5

SUBTRACTION = 7.5

MULTIPLICATION = 25

DIVISION = 4

===========================================

EX.NO: 03

8
DATE: SUM OF INDIVIDUAL DIGITS

AIM:
To create a program to read an integer number and find sum of all individual digits

using constructor, destructor and inline member function.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create a class named add.

Step 3 : Create a constructor to initialize the value.

Step 4 : Use getdata() function to get an integer value.

Step 5 : Use inline functions rem() & que() to find quotient & reminder of the given
values.
Step 6 : Create member function digit() to find sum of all individual digit..

Step 7 : Use destructor to destruct the object.

Step 8 : In the main program, create objects for above class.

Step 9 : Using the object, invoke all the above class.

Step 10 : Stop the process.

/*SUM OF INDIVIDUAL DIGITS*/

9
#include<iostream.h>
#include<conio.h> inline
long int rem(long int b)
{ return(b%10);
} inline long int que(long
int c)
{ return(c/10);
} class
add
{ public: long int
a,sum;
clrscr(); add()
{
cout<<"\n\t\t SUM OF INDIVIDUAL DIGITS"; cout<<"\
n\t\t **************************"; cout<<"\n
CONSTRUCTOR INVOKED";
cout<<"\n -------------------------------------";
a=0; sum=0;
cout<<"\n \t A="<<a<<"\t SUM="<<sum;
} void
getdata()
{ cout<<"\n Enter the
digits:";
cin>>a;
}
void digit()
{
long int t;
sum=0;
while(a>0)

{ t=rem(
a);
a=que(a);
sum=sum
+t;
if(a<=0)
{ if(sum>=10
)
{ a=sum;
sum=0;

10
}
}
} cout<<"\n Sum of the individual digits for given number
is:"<<sum; }
~add()
{
cout<<"\n\n DESTRUCTOR INVOKED";
cout<<"\n \n -----------------------------------";
cout<<"\n\t A="<<a<<"\t SUM="<<sum;
getch();
}
}; void
main()
{ clrscr(); add a1;
a1.getdata();
a1.digit();
getch();
}

OUTPUT:

SUM OF INDIVIDUAL DIGITS


****************************

CONSTRUCTOR INVOKED
-------------------------------------
A=0 SUM=0

11
Enter the digits : 987

Sum of individual digits for given number is :6

DESTRUCTOR INVOKED
----------------------------------

A=0 SUM=6

EX.NO: 04
FLOAT OBJECT OPERATOR
DATE:
OVERLOADING

AIM:
To create a class FLOAT that contains one float data member & overload all the four

arithmetic operators. so that they operate on the object FLOAT.

ALGORITHM:

12
Step 1 : Start the process.

Step 2 : Create a class FLOAT.

Step 3 : Using member function getdata(), get the float value.

Step 4 : Create an overload operator function (+) to add the float values.

Step 5 : Create an overload operator function (-) to subtract the float values.

Step 6 : Create an overload operator function (*) to multiply the float values.

Step 7 : Create an overload operator function (/) to divide the float values.

Step 8 : In the main program, create an object for class FLOAT.

Step 9 : With the help the object invoke the overload functions and display the result.

Step 10 : Stop the process.

/*FLOAT OBJECT OPERATOR OVERLOADING*/


#include<iostream.h>
#include<conio.h>
#include<iomanip.h> class
FLOAT
{ float f;
public:
void getdata()
{
cin>>f;
}
float operator+(FLOAT f2);
float operator-(FLOAT f2);
float operator*(FLOAT f2);

13
float operator/(FLOAT f2);
};
float FLOAT::operator+(FLOAT f2)
{ float t;
t=f+f2.f;
return(t);
}
float FLOAT::operator-(FLOAT f2)
{ float t; t=f-
f2.f;
return(t);
}
float FLOAT::operator*(FLOAT f2)
{ float t;
t=f*f2.f;
return(t);
}
float FLOAT::operator/(FLOAT f2)
{ float t;
t=f/f2.f;
return(t
);
} void
main()
{
FLOAT f1,f2;
float f;
clrscr();
cout<<"\n\t\t FLOAT OBJECT OPERATOR OVERLOADING"; cout<<"\n\t\t
*****************************************";
cout<<"\n Enter the float value for object-1:"; f1.getdata();
cout<<"\n Enter the float value for object-2:"; f2.getdata();
cout<<"\n\t\t OUTPUT FOR FLOAT OBJECT OPERATOR OVERLOADING"; cout<<"\n\
t\t ================================================";
cout<<"\n\t\t --------------------------------------------------------";
f=f1+f2;
cout<<"\n\t\t Sum of two objects f=f1- :"<<f<<endl;
f2;
cout<<"\n\t\t Difference of two objects :"<<f<<endl;
f=f1*f2;
cout<<"\n \t\tProduct of two objects :"<<f<<endl;

14
f=f1/f2;
cout<<"\n\t\t Division of two objects :"<<f<<endl;
cout<<"\n \t\t-------------------------------------------------------";
getch();
}

OUTPUT:

FLOAT OBJECT OPERATOR OVERLOADING


**************************************

Enter the float value for object-1 : 8.5 Enter

the float value for object-2 : 2.7

OUTPUT FOR FLOAT OBJECT OPERATOR OVERLOADING


================================================

---------------------------------------------------------------------------------
Sum of two objects : 11.2

15
Difference of two objects : 5.8

Product of two objects : 22.950001

Division of two objects : 3.148148


----------------------------------------------------------------------------------

EX.NO: 05
STRING FUNCTIONS
DATE:

AIM:
To create a class STRING and write member function to get and display strings.

overload the operator (+) to concatenate two string and overload the operator (==) to compare

two strings.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create a class STRING.

Step 3 : Create member function getdata(), to get the values for two strings.

16
Step 4 : In the class using member function operator “+” to concatenate two strings.

Step 5 : In the class using member function operator “==” to compare two strings.

Step 6 : Create a member function display(), to display the result.

Step 7 : Create an object in the main program to involve all the above member
functions.

Step 8 : Stop the process.

/*STRING FUNCTIONS*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h> class
STRING
{ char a[50],b[50],c[100];
public:
void getdata()
{ cout<<"\n Enter the first string: ";
cin>>a;
cout<<"\n Enter the second string:";
cin>>b;
}
void operator+(STRING&temp)
{
strcpy(temp.c,temp.a);
strcat(temp.c,temp.b);
}
void operator==(STRING&temp)
{

17
cout<<"\n\t STRING COMPARISION";
cout<<"\n\t **********************";
if(strcmp(temp.a,temp.b)!=0)
{ cout<<"\n Strings are not equal";
}
else
{ cout<<"\n Strings are Equal";
}
}
void display()
{
cout<<"\n\t STRING CONCATENATION"; cout<<"\
n\t *************************"; cout<<"\n First
String = "<<a;

18
: "<<strlen(a);
: "<<strlen(b);
: "<<strlen(c);
cout<<"\n Second String = "<<b;
cout<<"\n Concatenation of string = "<<c;
} void
length()
{
cout<<"\n\t LENGTH OF THE STRINGS";
cout<<"\n\t *************************";
cout<<"\n Length of the first string
cout<<"\n Length of the second string cout<<"\n
Length of the Concatenation of string }
};

void main()
{ int
ch;
STRING s;
con:
clrscr();
cout<<"\n\t\t\t STRING FUNCTIONS"; cout<<"\n\t\
t\t *******************";
cout<<"\n MENU";
cout<<"\n ---------";
cout<<"\n 1)STRING COMPARISION";
cout<<"\n 2)STRING CONCATENATION";
cout<<"\n 3)STRING LENGTH";
cout<<"\n 4)EXIT";
cout<<"\n Enter your choice:
"; cin>>ch; switch(ch)
{ case 1 : s.getdata();
s==s; getch();
goto con;
case 2 : s.getdata();
s+s;
s.display();
getch();
goto con;
20
19
case 3 : s.getdata();
s+s;
s.length();
getch();
goto con;
case 4 : cout<<"\n\n Program is terminated";
getch(); }
while(ch!
=4);
getch();
}

21
OUTPUT:

STRING FUNCTIONS
*******************
MENU
---------
1)STRING COMPARISION
2)STRING CONCATENATION
3) STRING LENGTH
4) EXIT

Enter your choice : 1


Enter the first string : RAJA
Enter the second string : RAJAN

STRING COMPARISION
***********************
Strings are not Equal

Enter your choice : 2


Enter the first string : RAJA
Enter the second string : RAJAN

STRING CONCATENATION
**************************
First String = RAJA
Second String = RAJAN
Concatenation of string = RAJARAJAN

Enter your choice : 3


Enter the first string : RAJA
Enter the second string : RAJAN

LENGTH OF THE STRINGS


**************************
Length of the first string : 4
Length of the second string : 5
Length of the Concatenation of string : 9

Enter your choice: 4

Program is terminated.

22
EX.NO: 06
EMPLOYEE DETAILS
DATE:

AIM:
To create a class EMPLOYEE having employee details and also another class pay from

above class and calculate DA, HRA, and PF depending upon the grade.

ALGORITHM:

STEP 1: Start the process.


STEP 2: Create a class Employee.
STEP 3: Create a member function getdata() to get the employee details.
STEP 4: Drive a sub-class pay to calculate HRA, DA according to grade.
STEP 5: Create a member function PF () to calculate and display the provident fund.
STEP 6: Create a member function sal () to calculate gross and net amount.
STEP 7: Create a member function putdata () to display all employee details.
STEP8: Create an array of object ‘P []' for class pay and access the above member
function according to the object.
STEP 9: Stop the process.

CODING:

#include<iostream.h>
#include<conio.h> #include<iomanip.h>
class EMPLOYEE
23
{ protected
: int
eno,i;
char ename[20],dept[20],grade;
float hra,pf,bs,da,net,gross;
public: void getdata()
{
clrscr();
cout<<"\n EMPLOYEE DETAILS:";
cout<<"\n ==================";
cout<<"\n ENTER THE EMPLOYEE NUMBER:";
cin>>eno;
cout<<"\n ENTER THE EMPLOYEE NAME:";
cin>>ename;
cout<<"\n ENTER THE EMPLOYEE DEPARTMENT:";
cin>>dept;
cout<<"\n ENTER THE BASIC SALARY:";
cin>>bs;
cout<<"\n ENTER THE GRADE (A/B/C/D/E):";
cin>>grade;
}
};
class pay : public EMPLOYEE
{ publi
c:
void get(void)
{
if(grade=='A')

{ hra=bs*0.5;
da=bs*50/100;
pf=bs*0.1;
}
else if(grade=='B')

{ hra=bs*0.4;
da=bs*30/100;
pf=bs*0.1;
}
else if(grade=='C')

{ hra=bs*0.3;
24
da=bs*20/100;
pf=bs*0.1;
}
else if(grade=='D')

{ hra=bs*0.2;
da=bs*10/100;
pf=bs*0.1;
}
else if(grade=='E')

{ hra=bs*0.
1; da=0;
pf=0;
} } void
sal(void)
{ gross=hra+da+
bs; net=gross-pf;
}
void putdata(void)
{ clrscr
();
cout<<"\n\t EMPLOYEE DETAILS";
cout<<"\n\t ==================";
cout<<"\n\t EMPLOYEE NUMBER :"<<eno<<endl;
cout<<"\n\t EMPLOYEE NAME :"<<ename<<endl; cout<<"\n\t
DEPARATMENT :"<<dept<<endl; cout<<"\n\t GRADE
:"<<grade<<endl; cout<<"\n\t BASIC
SALARY :"<<bs<<endl; cout<<"\n\t HOUSE RENT
ALLOWANCE :"<<hra<<endl; cout<<"\n\t DEARNES
ALLOWANCE :"<<da<<endl; cout<<"\n\t PROVIDENT FUND
:"<<pf<<endl;
cout<<"\n\t GROSS SALARY :"<<gross<<endl;
cout<<"\n\t -------------------------------------------------------------";
cout<<"\n\t NET SALARY :"<<net<<endl;
cout<<"\n\t
-------------------------------------------------------------"; }
};

25
void main() {
clrscr();
pay p[10];
int i,n;
clrscr();
cout<<"\n\t EMPLOYEE DETAILS"; cout<<"\n\t
=================="; cout<<"\n ENTER THE
NUMBER OF EMPLOYEES:"; cin>>n;
for(i=0;i<=(n-1);i++)
{
p[i].getdata();
}
for(i=0;i<=(n-1);i++)

{ p[i].get();
p[i].sal();
p[i].putdata();
getch();
}
}

26
OUTPUT:
EMPLOYEE DETAILS

=================

ENTER THE NUMBER OF EMPLOYEES: 2

EMPLOYEE DETAILS

=================
ENTER THE EMPLOYEE NUMBER : 2012

ENTER THE EMPLOYEE NAME : JOHN

ENTER THE DEPARTMENT : MANAGER

ENTER THE BASIC SALARY : 9500

ENTER THE GRADE (A/B/C/D/E) :A

EMPLOYEE DETAILS

==================

ENTER THE EMPLOYEE NUMBER : 2013

ENTER THE EMPLOYEE NAME : SIVA

ENTER THE DEPARTMENT : ACCOUNTS

ENTER THE BASIC SALARY : 7500

ENTER THE GRADE (A/B/C/D/E) :B

EMPLOYEE DETAILS
==================
EMPLOYEE NUMBER : 2012

EMPLOYEE NAME : JOHN


27
DEPARATMENT : MANAGER

GRADE :A

BASIC SALARY : 9500


HOUSE RENT ALLOWANCE: 4750

DEARNES ALLOWANCE : 4750

PROVIDENT FUND : 950

GROSS SALARY : 19000

------------------------------------------------------------------------------------------

NET SALARY : 18050

------------------------------------------------------------------------------------------
EMPLOYEE NUMBER : 2013

EMPLOYEE NAME : SIVA

DEPARATMENT : ACCOUNTS

GRADE :B

BASIC SALARY : 7500


HOUSE RENT ALLOWANCE: 3000

DEARNES ALLOWANCE : 2250

PROVIDENT FUND : 750

GROSS SALARY : 12750

------------------------------------------------------------------------------------------

NET SALARY : 12000

-------------------------------------------------------------------------------------------

EX.NO: 07
VIRTUAL FUNCTION
DATE:

28
AIM:
To create a class SHAPE which consists of two virtual functions & derive three
classes square, rectangle, triangle from class SHAPE and calculate Area and Perimeter.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create a class SHAPE which consists of two virtual functions call_area() &
call_peri() .

Step 3 : Create a sub-class square from class shape to calculate area & perimeter of
square.

Step 4 : Create a sub-class rectangle from class shape to calculate area & perimeter
of rectangle. .
Step 5 : Create a sub-class triangle from class shape to calculate area & perimeter
of triangle. .
Step 6 : Using member function area & perimeter of each classes.

Step 7 : In the main program using pointer and an objects invoke all the above member
function and display the result.

Step 8 : Stop the process.

/*VIRTUAL FUNCTION*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h> class
SHAPE
{ protected:
float
a,b,c,l,h;
public:
virtual float call_area()
{ return(0.00);

29
} virtual float
call_peri()
{ return(0.00);
}
};
class square:public SHAPE
{ public:
square()
{
cout<<"\n\t\t SQUARE OPERATIONS"; cout<<"\
n\t\t --------------------------------"; cout<<"\n Enter
the side of the square: ";
cin>>a;
} float
call_area()
{ cout<<"\n Area = ";
return(a*a);
} float
call_peri()
{ cout<<"\n Perimeter = ";
return(4*a);
}
};
class rectangle:public SHAPE
{ public:
rectangle()
{
cout<<"\n\t\t RECTANGLE OPERATIONS";
cout<<"\n\t\t --------------------------------------";
cout<<"\n Enter the length : ";
cin>>l;
cout<<"\n Enter the breadth : ";
cin>>b;
} float
call_area()
{ cout<<"\n Area = ";
return(l*b);
} float
call_peri()
{ cout<<"\n Perimeter = ";
return(2*(l+b));
}
30
}; class triangle:public
SHAPE
{ public:
triangle()
{
cout<<"\n\t\t TRIANGLE OPERATIONS"; cout<<"\
n\t\t -----------------------------------"; cout<<"\n Enter
the base : ";
cin>>l;
cout<<"\n Enter the height : ";
cin>>h;
cout<<"\n Enter the three sides : ";
cin>>a>>b>>c;
}

float call_area()
{ cout<<"\n Area = ";
return(0.5*l*h);
} float
call_peri()
{ cout<<"\n Perimeter = ";
return(a+b+c);
}
}; void
main()
{ int ch;
SHAPE *s;
do
{ clrscr();
cout<<"\n\t\t DISPLAY SHAPES";
cout<<"\n\t\t ****************";
cout<<"\n\t MENU"; cout<<"\n\t
--------";
cout<<"\n 1)SQUARE";
cout<<"\n 2)RECTANGLE" ;
cout<<"\n 3)TRIANGLE" ;
cout<<"\n 4)EXIT"; cout<<"\n
Enter your choice:" ; cin>>ch;
switch(ch)

31
{ case 1: square sq; s = &sq;
cout<<s->call_area();
cout<<s->call_peri(); break;
case 2: rectangle rec; s =
&rec; cout<<s-
>call_area();
cout<<s->call_peri();
break;
case 3: triangle tri; s=&tri;
cout<<s-
>call_area();
cout<<s->call_peri();
break;
case 4: cout<<"\n Program is terminated";
getch();
}
getch();
} while(ch!
=4); getch();
}

32
OUTPUT:

DISPLAY SHAPES
****************
MENU
--------
1) SQUARE
2) RECTANGLE
3) TRIANGLE
4) EXIT

Enter your choice : 1


SQUARE OPERATIONS
----------------------------------
Enter the side of the square : 2

Area = 4
Perimeter = 8

Enter your choice :2


RECTANGLE OPERATIOS
----------------------------------------
Enter the length :5
Enter the breadth :6

Area = 30

33
Perimeter = 22

Enter your choice :3


TRIANGLE OPERATIONS
--------------------------------------
Enter the base :3
Enter the height :5
Enter the three sides :1 5 9

Area = 7.5
Perimeter = 15

Enter your choice :4


Program is terminated

EX.NO: 08
FRIEND FUNCTION
DATE:

AIM:
To create two classes with a integer and float variable and create a friend functions to
display the integer and float values of both the classes separately.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create a class shape and corresponding getdata() function to get integer and
Float value an input

Step 3 : Create a class shape1 and corresponding getdata() function to get integer and
Float value an input

34
Step 4 : Create a friend function shape common to both classes & display the integer
and float values of two classes separately by taking object of two classes as
argument. .
Step 5 : In the main program create objects for both classes.
.
Step 6 : Call the friend function with these objects as argument.

Step 7 : Stop the process.

/*FRIEND FUNCTION*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
class shape1; class
shape
{ protected:
int a; float
b; public:
void getdata();
friend void shape2(shape,shape1);
}; class
shape1
{ protected: int
a1; float b1;
public: void
getdata();
friend void shape2(shape,shape1);
}; void
shape::getdata()
{ clrscr();
cout<<"\n\t\t FRIEND FUNCTION"; cout<<"\n\t\t
******************";
35
cout<<"\n Enter the integer and float value for first class: ";
cin>>a>>b;
} void
shape1::getdata()
{ cout<<"\n Enter the integer and float value for second class: ";
cin>>a1>>b1;
}

void shape2(shape c,shape1 d)


{ clrscr();
cout<<"\n\t\t OUTPUT FOR FRIEND FUNCTION \n"; cout<<"\n\t\t
============================ \n";
cout<<"\n --------------------------------------------------------------------";
cout<<"\n Integer value for first friend function : "<<c.a; cout<<"\n
Integer value for second friend function : "<<d.a1; cout<<"\n float
value for first friend function : "<<c.b;
cout<<"\n float value for second friend function : "<<d.b1;
cout<<"\n -------------------------------------------------------------------";
}; void
main()
{ clrscr();
shape s;
shape1
r;
s.getdata();
r.getdata();
shape2(s,r);
getch();
}

36
OUTPUT:

FRIEND FUNCTION
******************

Enter the integer and float value for first class : 3 8.5

Enter the integer and float value for second class : 9 1.4

OUTPUT FOR FRIEND FUNCTION


=============================
---------------------------------------------------------------------------------------------
Integer value for first friend function : 3

Integer value for second friend function : 9

Float value for first friend function : 8.5

Float value for second friend function : 1.4

---------------------------------------------------------------------------------------------

37
EX.NO: 09
SUM OF MATRIX
DATE:

AIM:
To create a program using function overloading to read two matrix of different data
types. Such as integer and float numbers and display the sum of two matrixes and also display
the sum of arrays individually.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create a class mat.

Step 3 : Create a member function read() to read the matrix of integer data type.

Step 4 : Create a member function get() to read the matrix of float data type.

Step 5 : Then create a member function sum(),to find the sum of two matrix and
display it.

Step 6 : And also find the sum of matrix individually and display it.

Step 7 : In the main program create object for the class.

Step 8 : Using class objects invoke the above member functions.

Step 9 : Stop the process.

38
/*SUM OF MATRIX*/

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
float r1=0; double
c1=0; class mat
{ int a[10][10],m,n,i,j;
double b[3][3],c[3]
[3]; public:
void get(int r1,float c1)
{ m=r1;
n=c1;
} void
read();
void get();
void sum();
}; void
mat::read()
{ cout<<"\n\t\t Sum of matrix"; cout<<"\n\t\
t ************"; cout<<"\n Enter the
integer matrix A: \n"; for(i=0;i<m;i++)
{ for(j=0;j<n;j+
+) cin>>a[i][j];
}
} void
mat::get()
{ cout<<"\n Enter the float matrix B: \n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cin>>b[i][j];
}
} void
mat::sum()
{ for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ c[i][j]=a[i][j]+b[i][j];
r1=r1+a[i][j];
c1=c1+b[i][j];
}

39
} cout<<"\n Sum of matrix A & B
is: \n"; for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cout<<c[i][j]<<"\t";
cout<<"\n";
} cout<<"\n Sum of matrix A is:
"<<r1; cout<<"\n Sum of matrix B is:
"<<c1;
} void
main()
{ clrscr(); int r,c; mat m; cout<<"\n\t\t SUM OF
MATRIX"; cout<<"\n\t\t ****************";
cout<<"\n Enter the no.of rows and columns: ";
cin>>r>>c; m.get(r,c);
m.read();
m.get();
m.sum();
getch();
}

OUTPUT:
SUM OF MATRIX ****************

Enter the no.of rows and columns: 3 3

Sum of matrix ************


Enter the integer matrix A:

1 5 9
7 5 3
2 4 6

Enter the float matrix B:

1.4 2.9 4.1 1.0


5.2 7.4
6.2 3.5 7.1

40
Sum of matrix A & B is:

2.4 7.9 13.1 8


10.2 10.4
8.2 7.5 13.1

Sum of matrix A is : 42
Sum of matrix B is : 38.8

EX.NO: 10
PALINDROME
DATE:

AIM:
To check whether the given string is palindrome or not palindrome by using pointer.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Declare pointer variable *p1 and *p2.

Step 3 : Copy string a to b.

Step 4 : Reverse the string b.

Step 5 : Assign the string a and b to pointer p1,p2.

Step 6 : Compare pointer p1 and p2.

Step 7 : If the value is 0, display the result is “string is palindrome”.

Step 8 : Else display the result “string is not palindrome”.

Step 9 : Stop the process.


41
/*PALINDROME*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main() {
clrscr();
char a[10],b[10],*p1,*p2; cout<<"\n\t\
t PALINDROME"; cout<<"\n\t\t
*************";
cout<<"\n Enter a string : ";
cin>>a;
strcpy(b,a);
cout<<"\n The given string is : "<<b;
cout<<"\n The reversed string is : "<<strrev(b);
p1=a; p2=b;
if(strcmp(p1,p2)==0
)
{ cout<<"\n String is palindrome";
} else cout<<"\n String is not
palindrome";
getch();
}

42
OUTPUT:

PALINDROME **************

Enter a string : MADAM

The given string is : MADAM

The reversed string is : MADAM

String is palindrome

Enter a string : MAD

The given string is : MAD

The reversed string is : DAM

String is not palindrome

43
EX.NO: 11
CONTENTS OF THE FILE WITH
DATE:
LINE NUMBER

AIM:
To check a file and display the contents of that file with line number.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Open an output file data.dat using ofstream class.

Step 3 : Enter the contents in data.dat.

Step 4 : Closing the file after enter the data.

Step 5 : Open an input file data.dat using ifstream class.

Step 6 : Viewing the contents with line numbers using in.

Step 7 : Close the input file.

Step 8 : Stop the process.

44
/*CONTENTS OF THE FILE WITH LINE NUMBER*/

#include<iostream.h>
#include<conio.h>
#include<fstream.h> void
main()
{ clrscr(); int n,i; char
str[20]; ofstream
out("data.dat");
cout<<"\n\t\t CONTENTS OF THE FILE"; cout<<"\n\t\t
***********************";
cout<<"\n";
cout<<"\n Enter no.of strings : ";
cin>>n; for(i=0;i<n;i++)
{ cout<<"\n Enter the strings: ";
cin>>str; out<<str<<endl;
} out.close(); ifstream in("data.dat"); cout<<"\n\t\t
Contents of the file with line numbers";
cout<<"\n\t\t -----------------------------------------------“; for(i=1;i<=n;i+
+)
{ in>>str;
cout<<"\n"<<i<<"."<<str<<endl;
}
in.close();
getch();
}

OUTPUT:

CONTENTS OF THE FILE


***********************

Enter no. of strings : 2

Enter the strings : PEN


45
Enter the strings : PENCIL

Contents of the file with line numbers


-------------------------------------------------
1) PEN

2) PENCIL

EX.NO: 12
MERGED FILES
DATE:

AIM:
To check a merged two file into a single file.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create two files “d1.dat” and “d2.dat” using input file stream.
46
Step 3 : Create file “d3.dat” using output stream for merge two files.

Step 4 : Enter the data for two files using get functions.

Step 5 : Close the files after enter the data.

Step 7 : Merge the files in “d3.dat”.

Step 8 : Check the merge file “d3.dat” by opening that file or by using command
prompt.

Step 9 : Stop the process.

/*MERGED FILES*/

#include<iostream.h>
#include<conio.h>
#include<fstream.h> void
main()
{ clrscr(); char c; ifstream
in1("d1.dat"); ifstream
in2("d2.dat");
ofstream
out("d3.dat");
while(in1.eof( )==0)
{ in1.get(c);
out<<c;

47
}
while(in2.eof( )==0
)
{ in2.get(c);
out<<c;
} in1.close( ); in2.close( );
out.close( ); cout<<"\n Merged
file: d3.dat";
getch();
}

OUTPUT:

Merged file: d3.dat

C:\TC\BIN> copy con d1.dat (or ) edit d1.dat


Raja
^z
1 file(s) copied

C:\TC\BIN> copy con d2.dat (or) edit d2.dat


Rajan
^z
1 file(s) copied

C:\TC\BIN> Type d1.dat

Raja

C:\TC\BIN> Type d2.dat


48
Rajan

C:\TC\BIN>Type d3.dat

Raja
Rajan

49

You might also like