12

54
INDEX S.N O DATE TOPIC PAGE NO STAFF SIGNATURE 1 DISTANCE CONVERSION PROBLEM 1 2 OVERLOADING OBJECTS 10 3 OVERLOADING CONVERSIONS 16 4 OVRELOADING MATRIX 22 5 AREA COMPUTATION USING DERIVED CLASS 32 6 VECTOR PROBLEM 37 7 INHERITANCE 43

Transcript of 12

Page 1: 12

INDEX

S.NO DATE TOPIC PAGE NO

STAFF SIGNATURE

1 DISTANCE CONVERSION PROBLEM

1

2 OVERLOADING OBJECTS 10

3 OVERLOADING CONVERSIONS

16

4 OVRELOADING MATRIX 22

5 AREA COMPUTATION USING DERIVED CLASS

32

6 VECTOR PROBLEM 37

7 INHERITANCE 43

Page 2: 12

EX.NO:1

DATE:

DISTANCE CONVERSION PROBLEM

AIM:

Create two classes DM and DB which store the value of distances. DM

store the value ofdistances. DM stores distances in meters and centimeters in

DB in feet and inches. Write a Program that can create the values of the class

objects and add one object DM with another object DB.

Use a friend function to carry out addition operation. The object that

stores the result may be DM object or DB object depending on the units in

which results are required.

The display should be in the order of meter and centimeter and feet or

inches depending on the order of display.

ALGORITHM:

STEP 1:

Start the program execution.STEP 2:

To create a class metric and british.

STEP 3:

To create a function input() and display().

STEP 4:

Accept the feet,inches value of british.

STEP 5: Accept the meter ,cm,value of metric.

STEP 6:

To convert metric value to british value.

Page 3: 12

STEP 7:

To convert british value to metric value.

STEP 8:

To display the metric values and british values.

STEP 9:

Stop the program execution.

CODING:

#include<iostream.h>

#include<conio.h>

#include<math.h>

class metric;

class british

{

public:

int feet,inches;

void input();

friend british addbritish(british & b,metric & m);

void display();

};

class metric

{

public:

int meter,cm;

Page 4: 12

void input();

friend metric addmetric(metric & m,british & b);

void display();

};

void british::input()

{

cout<<"\n\t enter the value of feet:";

cin>>feet;

cout<<"\n\t enter the value of inches:";

cin>>inches;

}

british addbritish(british & b,metric & m)

{

british b1;

int ftin,tmcm,tin,mtcm,tm,cm,tmcmtin,tot;

ftin=(b.feet)*12;

tin=ftin+b.inches;

mtcm=(m.meter)*100;

tmcm=mtcm+m.cm;

tmcmtin=tmcm/2.5;

tot=tin+tmcmtin;

b1.feet=tot/12;

b1.inches=tot%12;

return(b1);

}

Page 5: 12

void british::display()

{

cout<<"\n\t the feet value is:\t"<<feet;

cout<<"\n\t the inches value is:\t"<<inches;

}

void metric::input()

{

cout<<"\n\t enter the value of meter:\t";

cin>>meter;

cout<<"\n\t enter the value of cm:\t";

cin>>cm;

}

metric addmetric(metric & m,british & b)

{

metric m1;

int tin,ftin,mtcm,tincmt,tot;

ftin=(b.feet)*12;

tincmt=(ftin+b.inches)*2.5;

mtcm=(m.meter)*100;

tot=tincmt+mtcm+m.cm;

m1.meter=tot/100;

m1.cm=tot%100;

return(m1);

}

void metric::display()

Page 6: 12

{

cout<<"\n\t the meter value is \t"<<meter;

cout<<"\n\t the centimeter value is \t"<<cm;

}

void main()

{

int op;

clrscr();

british b1,b2;

metric m1,m2;

cout<<"\n Distance conversion";

b1.input();

m1.input();

do

{

clrscr();

cout<<"\n MENU";

cout<<"\n 1.Metric to British";

cout<<"\n 2.British to Metric";

cout<<"\n 3.Exit";

cout<<"\n Enter the choice:";

cin>>op;

switch(op)

{

case 1:

Page 7: 12

cout<<"\n\t metric to british";

cout<<"\n\t output";

b1.display();

m1.display();

b2=addbritish(b1,m1);

cout<<"\n\t output";

b2.display();

break;

case 2:

cout<<"\n\t british to metric";

cout<<"\n\t output";

b1.display();

m1.display();

m2=addmetric(m1,b1);

cout<<"\n output";

m2.display();

break;

case 3:

cout<<"\n exit";

break;

}

getch();

}

while (op!=3);

}

Page 8: 12

OUTPUT

Distance conversion

enter the value of feet: 10

enter the value of inches: 6

enter the value of meter: 4

enter the value of cm: 2

MENU

1.Metric to British

2.British to Metric

3.Exit

Enter the choice:1

metric to british

output

the feet value is: 10

the inches value is: 6

the meter value is 4

the centimeter value is 2

output

the feet value is: 23

the inches value is: 10

MENU

1.Metric to British

Page 9: 12

2.British to Metric

3.Exit

Enter the choice:2

british to metric

output

the feet value is: 10

the inches value is: 6

the meter value is 4

the centimeter value is 2

output

the meter value is 7

the centimeter value is 17

MENU

1.Metric to British

2.British to Metric

3.Exit

Enter the choice:3

Exit

RESULT

Page 10: 12

EX.NO:2

DATE:

OVERLOADING OBJECTS

AIM:

Create a class FLOAT that contains one float data member overload all

the four arithmetic operators so that operate on the objects of FLOAT.

ALGORITHM :

STEP 1:

Start the program execution

STEP 2:

To create a class FLOAT

STEP 3:

Accept the input values

STEP 4:

Overload the arithmetic operator +

STEP 5:

Overload the arithmetic operetor –

STEP 6:

Overload the arithmetic operator *

STEP 7:

Overload the arithmetic operator /

Page 11: 12

STEP 8:

To display the output values.

STEP 9:

Stop the program execution.

CODING:

#include<iostream.h>

#include<conio.h>

#include<math.h>

class FLOAT

{

float x,D;

public:

FLOAT()

{

}

FLOAT(float a)

{

x=a;

}

FLOAT operator+(FLOAT);

FLOAT operator-(FLOAT);

FLOAT operator*(FLOAT);

Page 12: 12

FLOAT operator/(FLOAT);

void outdata();

void indata()

{

cout<<"\n\n\t enter the value:";

cin>>x;

}

};

FLOAT FLOAT::operator+(FLOAT D)

{

FLOAT temp;

temp.x=x+D.x;

return(temp);

}

FLOAT FLOAT::operator-(FLOAT D)

{

FLOAT temp;

temp.x=x-D.x;

return(temp);

}

FLOAT FLOAT::operator*(FLOAT D)

{

FLOAT temp;

temp.x=x*D.x;

return(temp);

Page 13: 12

}

FLOAT FLOAT::operator/(FLOAT D)

{

FLOAT temp;

temp.x=x/D.x;

return(temp);

}

void FLOAT::outdata(void)

{

cout<<x<<endl;

}

int main()

{

FLOAT o1,o2,ADD,SUB,MUL,DIV;

clrscr();

o1.indata();

o2.indata();

ADD=o1+o2;

SUB=o1-o2;

MUL=o1*o2;

DIV=o1/o2;

cout<<"\n\n\t o1:";

o1.outdata();

cout<<"\n\n\t o2:";

o2.outdata();

Page 14: 12

cout<<"\n\n\t ADDITION VALUE IS (o1+o2)=";

ADD.outdata();

cout<<"\n\n\t SUBTRACTION VALUE IS (o1-o2)=";

SUB.outdata();

cout<<"\n\n\t MULTIPLICATION VALUE IS (o1*o2)=";

MUL.outdata();

cout<<"\n\n\t DIVISION VALUE IS (o1/o2)=";

DIV.outdata();

getch();

return(0);

}

Page 15: 12

2.OUTPUT:

enter the value:4

enter the value:4

o1:4

o2:4

ADDITION VALUE IS (o1+o2)=8

SUBTRACTION VALUE IS (o1-o2)=0

MULTIPLICATION VALUE IS (o1*o2)=16

DIVISION VALUE IS (o1/o2)=1

RESULT:

Page 16: 12

EX.NO:3

DATE:

OVERLOADING CONVERSIONS:AIM:

Design a class polar which describes a point in a plane using polar

Co-ordinates radius and angle. A point in polar Co-ordinates is as shown

below.

Use the overloader + operator to add two objects of polar. Note that we

cannot add polar values of two points directly. This requires first the

conversion.

Points into rectangular Co-ordinates and finally converting the result

into polar Co-ordinates.

You need to use following trigonometric formulas.

X= r * cos (a); Y= r * sin (a);

a= sqrt (X * X +Y * Y);

ALGORITHM :

STEP 1:

Start the program execution.

STEP 2:

To create class polar.

STEP 3:

Accept the radius and angle values.

STEP 4:

First we accept the radius and angle value is 0.

Page 17: 12

STEP 5:

To create arguments in polar.

STEP 6:

To overload the + operator to add two object of polar.

STEP 7:

To display the output values.

STEP 8:

Stop the program execution.

CODING:

#include<iostream.h>

#include<conio.h>

#include<math.h>

class polar

{

double radius,angle;

public:

polar()

{

radius=0;

angle=0;

}

Page 18: 12

polar(double r,double a)

{

radius=r;

angle=a;

}

void disp();

polar operator+(polar);

};

void polar::disp()

{

cout<<"\n\n\t radius value is:";

cout<<radius;

cout<<"\n\n\t angle value is:";

cout<<angle;

}

polar polar::operator+(polar p1)

{

polar p2;

double x1,x2,y1,y2,x,y;

x1=radius*cos(angle*3.14/180);

x2=p1.radius*cos(p1.angle*3.14/180);

y1=radius*sin(angle*3.14/180);

y2=p1.radius*sin(p1.angle*3.14/180);

Page 19: 12

cout<<"\n output\n";

cout<<"*****\n";

cout<<"\n\n radius 1="<<x1<<"\t angle 1="<<y1;

cout<<"\n\n radius 2="<<x2<<"\t angle 2="<<y2;

x=x1+x2;

y=y1=y2;

cout<<"\n\n sum of the radius in rectangular coordinates is:";

cout<<x;

cout<<"\n\n sum of the angle in rectangular coordinates is:";

cout<<y;

p2.radius=sqrt(x*x+y*y);

p2.angle=atan(y/x)*(180/3.14);

return(p2);

}

void main()

{

clrscr();

cout.precision(2);

polar o1,o2,o3;

double r1,r2,a1,a2;

char c;

do

{

Page 20: 12

cout<<"\n enter the radius 1 and angle 1:";

cin>>r1>>a1;

cout<<"\n enter the radius 2 and angle 2:";

cin>>r2>>a2;

o1=polar(r1,a1);

o2=polar(r2,a2);

o3=o1+o2;

o3.disp();

cout<<"\n\n Do you want to continue(y/n):";

cin>>c;

}

while(c=='y'||c!='n');

getch();

}

Page 21: 12

3.OUTPUT

enter the radius 1 and angle 1:

11

13

enter the radius 2 and angle 2:

13

15

output

*****

radius 1=10.72 angle 1=2.47

radius 2=12.56 angle 2=3.36

sum of the radius in rectangular coordinates is:23.28

sum of the angle in rectangular coordinates is:3.36

radius value is:23.52

angle value is:8.23

Do you want to continue(y/n):n

RESULT:

Page 22: 12

EX.NO:4

DATE:

OVRELOADING MATRIX:

AIM:

Create a class MAT of size M*N. Define all possible matrix operations

for MAT type objects. Verify the identity.

(A-B)^2 = A^2+B^2 –2*A*B

ALGORITHM :

STEP 1:

Start the program execution.

STEP 2:

To create a class MAT.

STEP 3:

To accept the element of matrices.

STEP 4:

To create a function inp(int) and disp().

STEP 5:

To overload the operator the operators +,-,*,/,^.

STEP 6:

To using for condition.

Page 23: 12

STEP 7:

To accept the element of first and second matrix.

STEP 8:

To calculate (A-B)^2.

STEP 9:

To calculate A^2+B^2-2*A*B.

STEP 10:

To verify (A-B)^2=A^2+B^2-2AB the identity is true using it condition.

STEP 11:

To verify (A-B)^2!=A^2+B^2-2AB the given identity is false.

STEP 12:

To create a output values.

STEP 13:

Stop the program execution.

CODING:

#include<iostream.h>

#include<conio.h>

#include<math.h>

const size=3;

class mat

{

int nrc;

Page 24: 12

int a[size][size];

public:

void inp(int);

void disp();

mat operator+(mat);

mat operator-(mat);

mat operator*(mat);

mat operator*(int);

mat operator^(int);

int operator==(mat);

};

void mat::inp(int n)

{

nrc=n;

for(int i=0;i<nrc;i++)

for(int j=0;j<nrc;j++)

cin>>a[i][j];

}

void mat::disp()

{

for(int i=0;i<nrc;i++)

{

for(int j=0;j<nrc;j++)

Page 25: 12

cout<<a[i][j]<<" ";

cout<<endl;

}

}

mat mat::operator+(mat m)

{

mat temp;

temp.nrc=nrc;

for(int i=0;i<nrc;i++)

for(int j=0;j<nrc;j++)

temp.a[i][j]=a[i][j]+m.a[i][j];

return temp;

}

mat mat::operator-(mat m)

{

mat temp;

temp.nrc=nrc;

for(int i=0;i<nrc;i++)

for(int j=0;j<nrc;j++)

temp.a[i][j]=a[i][j]-m.a[i][j];

return temp;

}

mat mat::operator*(mat m)

Page 26: 12

{

mat temp;

temp.nrc=nrc;

for(int i=0;i<nrc;i++)

for(int j=0;j<nrc;j++)

{

temp.a[i][j]=0;

for(int k=0;k<nrc;k++)

temp.a[i][j]+=a[i][j]*m.a[i][j];

}

return temp;

}

mat mat::operator*(int x)

{

mat temp;

temp.nrc=nrc;

for(int i=0;i<nrc;i++)

for(int j=0;j<nrc;j++)

temp.a[i][j]=x*a[i][j];

return temp;

}

mat mat::operator^(int)

{

Page 27: 12

mat temp;

temp.nrc=nrc;

for(int i=0;i<nrc;i++)

for(int j=0;j<nrc;j++)

{

temp.a[i][j]=0;

for(int k=0;k<nrc;k++)

temp.a[i][j]+=a[i][j]*a[i][j];

}

return temp;

}

int mat::operator==(mat m)

{

int t=1;

int f=0;

for(int i=0;i<nrc;i++)

for(int j=0;j<nrc;j++)

if(a[i][j]!=m.a[i][j])

return f;

else

return t;

return 0;

}

Page 28: 12

void main()

{

clrscr();

mat m1,m2,mat1,mat2;

int n;

cout<<"\n enter the order of matrices:";

cin>>n;

cout<<"\n enter the element of first matrix:";

m1.inp(n);

cout<<"\n enter the element of second matrix:";

m2.inp(n);

cout<<"\n first matrix is:";

m1.disp();

cout<<"\n second matrix is:";

m2.disp();

mat1=(m1-m2)^2;

cout<<"\n result of (A-B)^2 is:\n";

mat1.disp();

mat2=(m1^2)+(m2^2)-((m1*m2)*2);

cout<<"\n result of (A^2+B^2-2AB) is:\n";

mat2.disp();

if(mat1==mat2)

cout<<"\n the given identity is true:\n";

Page 29: 12

else

cout<<"\n the given identity is false:\n";

getch();

}

Page 30: 12

4.OUTPUT:

enter the order of matrices:2

enter the element of first matrix:

1

1

1

1

enter the element of second matrix:

1

1

1

1

first matrix is:

1 1

1 1

second matrix is:

1 1

1 1

result of (A-B)^2 is:

0 0

0 0

Page 31: 12

result of (A^2+B^2-2AB) is:

0 0

0 0

the given identity is true:

RESULT:

Page 32: 12

EX.NO:5

DATE:

AREA COMPUTATION USING DERIVED CLASS

AIM:

Area of rectangle = X*YArea of triangle = ½ * X * Y

ALGORITHM :

STEP 1:

Start the program execution.

STEP 2:

To create a class rectangle and triangle.

STEP 3:

Create a function get(),cal() and disp().

STEP 4:

Accept the two sides of rectangles and triangle.

STEP 5:

Calculate the area of rectangle are=X*Y.

STEP 5:

Calculate the area of triangle are =1/2*X*Y.

STEP 6:

Display the outputs.

Page 33: 12

STEP 7:

Stop the program execution.

CODING

#include<iostream.h>

#include<conio.h>

#include<math.h>

class rectangle

{

float area;

protected:

float x,y;

public:

void get();

void calc()

{

area=x*y;

}

void disp();

};

void rectangle::get()

{

Page 34: 12

cout<<"\n enter the sides of rectangle and triangle:";

cin>>x>>y;

}

void rectangle::disp()

{

cout<<"\n area of the rectangle is:"<<area<<endl;

}

class triangle:public rectangle

{

float area;

public:

void calc()

{

area=(x*y)/2;

}

void disp()

{

cout<<"\n area of the triangle is:"<<area;

}

};

void main()

{

clrscr();

Page 35: 12

triangle tr;

tr.get();

tr.rectangle::calc();

tr.rectangle::disp();

tr.calc();

tr.disp();

getch();

}

Page 36: 12

5.OUTPUT:

enter the sides of rectangle and triangle:

4

5

area of the rectangle is:20

area of the triangle is:10

RESULT:

Page 37: 12

EX.NO:6

DATE:

VECTOR PROBLEM:

AIM:

Define a class for vector containing scalar values. Apply overloading

concepts for vector addition, Multiplication of a vector by a scalar quantity,

replace the values in a position vector.

ALGORITHM

STEP 1:

Start the program execution.

STEP 2:

To create a class vector.

STEP 3:

To accept the input values.

STEP 4:

Overloading the *,+,<<,>> operators.

STEP 5:

To multiply the vector values.

STEP 6:

To calculate the sum of two vectors.

Page 38: 12

STEP 7:

To change the position of elements to be modified vector.

STEP 8:

To accept the input values.

STEP 9:

To display the output values.

STEP 10:

Stop the program execution.

CODING:

#include<iostream.h>

#include<conio.h>

#include<math.h>

const size=3;

class vector

{

public:

int *v;

vector()

{

v=new int[size];

}

int & operator[](int);

Page 39: 12

friend vector operator*(int,vector);

friend vector operator+(vector,vector);

friend istream & operator>>(istream &,vector&);

friend ostream & operator>>(ostream &,vector&);

};

int &vector::operator[](int pos)

{

return v[pos];

}

vector operator * (int a,vector b)

{

vector c;

for(int i=0;i<size;i++)

c.v[i]=a*b.v[i];

return c;

}

vector operator+(vector a,vector b)

{

vector c;

for(int i=0;i<size;i++)

c.v[i]=a.v[i]+b.v[i];

return c;

}

Page 40: 12

istream & operator>>(istream &cin,vector &b)

{

for(int i=0;i<size;i++)

cin>>b.v[i];

return cin;

}

ostream &operator<<(ostream &cout,vector &b)

{

cout<<","<<b.v[0];

for(int i=1;i<size;i++)

{

cout<<","<<b.v[i];

}

return cout;

}

void main()

{

clrscr();

vector m,n;

int sc;

cout<<"\n enter the element of vector 1:";

cin>>m;

cout<<"\n enter the element of vector 2:";

Page 41: 12

cin>>n;

cout<<"\n output";

cout<<"\n ****************";

cout<<"\n vector 1_"<<m;

cout<<"\n vector 2_"<<n;

vector sum,mul;

cout<<"\n scalar multiplication value:";

cin>>sc;

mul=sc * m;

sum=m+n;

cout<<"\n scalar multiplication of vector 1 is:"<<mul;

cout<<"\n sum of the two vector is :"<<sum;

int pos,nval;

cout<<"\n enter the position of the element to be modified (vector):";

cin>>pos;

cout<<"\n\n enter the value:";

cin>>nval;

m[pos-1]=nval;

cout<<"\n\n modified value is:"<<m;

getch();

}

Page 42: 12

6.OUTPUT

enter the element of vector 1:

5

6

4

enter the element of vector 2:

4

8

6

output

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

vector 1:5,6,4

vector 2:4,8,6

scalar multiplication value:3

scalar multiplication of vector 1 is: 15,18,12

sum of the two vector is : 9,14,10

enter the position of the element to be modified (vector):2

enter the value:5

modified value is:5,5,4

RESULT:

Page 43: 12

EX.NO:7

DATE:

INHERITANCE

AIM:

Create three classes alpha, beta and gamma, each containing one data member. The class gamma should be inherited from both alpha and beta. Use a constructor function in the class gamma to assign values to the data members of all the classes.

ALGORITHM:

STEP 1:

Start the program execution.

STEP 2:

To create a three classes alpha, beta and gamma

STEP 3:

To accept the input values.

STEP 4:

TO use constructor function

STEP 5:

To accept the input values.

STEP 6:

To display the output values.

STEP 7:

Stop the program execution.

Page 44: 12

CODING:

#include<iostream.h>

#include<conio.h>

#include<math.h>

class alpha

{

int x;

public:

alpha(int i)

{

x=i;

cout<<"alpha intialised\n";

}

void show_x(void)

{

cout<<"x="<<x<<"\n";

}

};

class beta

{

float y;

public:

Page 45: 12

beta(float j)

{

y=j;

cout<<"beta initialized\n";

}

void show_y(void)

{

cout<<"y="<<y<<"\n";

}

};

class gamma:public beta,public alpha

{

int m,n;

public:

gamma(int a,float b,int c,int d):alpha(a),beta(b)

{

m=c;

n=d;

cout<<"gamma initialized\n";

}

void show_mn(void)

{

cout<<"m="<<m<<"\n";

Page 46: 12

cout<<"n="<<n<<"\n";

}

};

void main()

{

clrscr();

gamma g(5,10.75,20,30);

g.show_x();

g.show_y();

g.show_mn();

getch();

}

Page 47: 12

7.OUTPUT

beta initialized

alpha intialised

gamma initialized

x=5

y=10.75

m=20

n=30

RESULT: