26.8.15

Create a 'MATRIX' class of size m X n

Create a 'MATRIX' class of size m X n. Overload the ‘+’ operator to add two MATRIX objects. Write a main function to implement it.


#include<iostream.h>
#include<conio.h> class mat

{

int m,n,a[20][20]; public:

mat(int x,int y); void readmat(); mat operator +(mat); void display();
};

mat :: mat(int x,int y)
{
m=x;n=y;
for(int i=0;i<m;i++)
{

for(int j=0;j<n;j++) a[i][j]=0;
}

}
void mat :: readmat()
{

cout<<"\nenter matrix elements\n"; for(int i=0;i<m;i++)
for(int j=0;j<n;j++) cin>>a[i][j];
}

mat mat:: operator +(mat obj)
{
mat temp(m,n); for(int i=0;i<m;i++) for(int j=0;j<n;j++)
{

temp.a[i][j]=a[i][j]+obj.a[i][j];
}
return temp;
}
void mat:: display()
{
int i,j; for(i=0;i<m;i++)

{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<"\t"<<a[i][j];
}

}
int main()

{

int m1,n1; clrscr();

cout<<"\nEnter the size(m,n) of matrix: "; cin>>m1>>n1;
mat a(m1,n1),b(m1,n1),c(m1,n1); cout<<"\nEnter martix 1: "; a.readmat();

cout<<"\nEnter matrix 2: "; b.readmat();
c=a.operator +(b); cout<<"\nFirst Matrix :\n"; a.display(); cout<<"\nSecond Matrix :\n"; b.display();

cout<<"\nmatrix 1+matrix 2: "; c.display();
getch(); return 0;
}

********************************OUTPUT**********************************

Enter the size(m,n) of matrix: 2 2

Enter martix 1: enter matrix elements 3 3 3 3


Enter matrix 2: enter matrix elements 4 4 4 4


First Matrix :
3
3
3
3
Second Matrix :
4
4
4
4

matrix 1 + matrix 2:
7
7
7
7

Create a class 'COMPLEX' to hold a complex number

Create a class 'COMPLEX' to hold a complex number. Write a friend function to add two complex numbers. Write a main function to add two COMPLEX objects.

#include<iostream.h>

#include<conio.h> class complex
{

float real,imag; public: void get_complex();

void show_complex();
friend complex add_complex(complex c1,complex c2);
};

void complex::get_complex()

{
cout<<"Enter real number :"; cin>> real;
cout<<"Enter Imaginary number :"; cin>>imag;
}
void complex::show_complex()
{
cout<<real<<"+i"<<imag;
}
complex add_complex(complex c1,complex c2)
{

complex c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c;

}

int main()

{
clrscr();
complex c1,c2,c3; c1.get_complex(); c2.get_complex(); c3=add_complex(c1,c2); cout<<"\nComplex Number 1 = "; c1.show_complex(); cout<<"\nComplex Number 2 = "; c2.show_complex();
cout<<"\nSum of Complex Number 1 and 2 = "; c3.show_complex();
getch(); 
return 0;


}

*******************************OUTPUT*****************************

Enter real number :12

Enter Imaginary number :10
Enter real number :3
Enter Imaginary number :5

Complex Number 1 = 12+i10

Complex Number 2 = 3+i5
Sum of Complex Number 1 and 2 = 15+i15


Create a class called 'TIME'

Create a class called 'TIME' that has

-     three integer data members for hours, minutes and seconds

-     constructor to initialize the object to zero

-     constructor to initialize the object to some constant value

-     member function to add two TIME objects

-     member function to display time in HH:MM:SS format

Write a main function to create two TIME objects, add them and display the result in HH:MM:SS format.

#include<iostream.h>

#include<conio.h> class Time

{
int hrs,min,sec;

public: Time()

{
hrs=0;min=0;sec=0;
}

Time(int a,int b,int c)

{
hrs=a; min=b;sec=c;
}

void addTime(Time tt1,Time tt2)

{
sec=tt1.sec+tt2.sec;
min=sec/60;
sec=sec%60;
min=min+tt1.min+tt2.min;
hrs=min/60;
min=min%60;
hrs=hrs+tt1.hrs+tt2.hrs;
}

void display()

{
cout<<hrs<<":"<<min<<":"<<sec<<endl;
}

};

int main()

{

clrscr();
Time t1(10,48,30);

Time t2(2,22,35); Time t3; t3.addTime(t1,t2); cout<<"\nTime 1 is :"; t1.display(); 
cout<<"\nTime 2 is :"; t2.display();

           cout<<"\nTime 1 + Time 2 is :";
          
          
           t3.display();
           
           getch();
           
    
  
           return(0);
}


***********************OUTPUT******************************

Time 1 is :10:48:30

Time 2 is :2:22:35

Time 1 + Time 2 is :13:11:5

Create a class called 'EMPLOYEE'

Create a class called 'EMPLOYEE' that has

-     EMPCODE and EMPNAME as data members

-     member function getdata( ) to input data

-     member function display( ) to output data

Write a main function to create EMP, an array of EMPLOYEE objects. Accept and display the details of at least 6 employees.


#include<iostream.h>

#include<conio.h>
#include<iomanip.h> class Employee

{
private:  int empcode;
char empname[10]; public: void getdata();

void display();
};
void Employee::getdata()
{
cout<<"\nNAME :"; cin>>empname; cout<<"\nCODE :"; cin>>empcode;

}
void Employee::display()
{
cout<<endl<<setw(20)<<empname<<setw(10)<<empcode;
}

int main()

{
Employee Emp[6]; clrscr();
cout<< "Enter employee details:\n "; for(int i=0;i<6;i++)
{

cout<<"\nemployee "<<i+1<<endl; Emp[i].getdata();
}

cout<<"\nEmployee details are as follows :"; cout<<"\n\n"<<setw(20)<<"NAME"<<setw(10)<<setiosflags(ios::right)<<"CODE";

cout<<"\n------------------------------
";
for(i=0;i<6;i++)

Emp[i].display();

getch();

return(0);

}

*************************OUTPUT*********************************

Enter employee details:

employee 1


NAME :ashok


CODE :111


employee 2


NAME :annapurna


CODE :112


employee 3


NAME :anupama


CODE :113


employee 4


NAME :anuradha


CODE :114


employee 5


NAME :ashraya


CODE :115


employee 6


NAME :akash


CODE :116


Employee details are as follows :

NAME
CODE

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

Ashok
111

annapurna
112

anupama
113

anuradha
114

ashraya
115

akash
116


Create a 'DISTANCE' class

Create a 'DISTANCE' class with :

-     feet and inches as data members

-     member function to input distance

-     member function to output distance

-     member function to add two distance objects

Write a main function to create objects of DISTANCE class. Input two distances and output the sum.

#include<iostream.h>

#include<conio.h>

class distance

{
int feet,inch;
public:
void getdistance(); void putdistance();

void adddistance(distance d1,distance d2);
};

void distance::getdistance()

{

cout<<"\nEnter the feet : "; cin>>feet;
cout<<"\nEnter the inches :"; cin>>inch;
}

void distance::putdistance()
{
cout<<"\n\nfeet = "<<feet; cout<<"\tinch = "<< inch;

}
void distance::adddistance(distance d1,distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
inch=inch%12;
feet=feet+d1.feet+d2.feet;
}

int main()

{

distance d1,d2,d3; clrscr();

cout<< "\nDistance 1 \n"; d1.getdistance();

cout<< "\nDistance 2 \n"; d2.getdistance(); d3.adddistance(d1,d2);
cout<<"\nThe Sum Of two distance is : "; d3.putdistance();
getch();

return(0);
}


*********************************OUTPUT****************************

Distance 1

Enter the feet : 13

Enter the inches :11

Distance 2

Enter the feet : 11

Enter the inches :11

The Sum Of two distance is :

feet = 25   inch = 10