Return Type

 Return Type 

1. Write a program to define class Distance with data members feet ,inches

of apropriate type.Define member function of class to adds

two distances and return to sum in main program.


#include<iostream.h>

#include<conio.h>

class Distance

{

private:

      int feet,inches;

public:

           void getdetails();

           void display();

          Distance returndis(Distance);

             };

void Distance::getdetails()

  {

    cout<<"Enter the value of feet:";

    cin>>feet;

    cout<<"Enter the value of inches:";

    cin>>inches;

       }

void Distance::display()

  {

    cout<<feet<<"-feet "<<inches<<"-inches"<<endl;

     }

 Distance Distance::returndis(Distance d2)

  {

   int feet1;

   Distance d3;

   inches=inches + d2.inches;

   d3.inches=inches%12;

   feet1=inches/12;

   d3.feet= feet1+ feet+ d2.feet;

   return d3;

      }

  void main()

  {

   Distance d1,d2,d3;

   cout<<"Enter the  first value for distance:"<<endl;

   d1.getdetails();

   cout<<endl;

   cout<<"Enter the second value for distance:"<<endl;

   d2.getdetails();

   cout<<endl;

   cout<<"The first value of distance is:"<<endl;

   d1.display();

   cout<<endl;

   cout<<"the second value of distance is:"<<endl;

   d2.display();

   cout<<endl;

   d3=d2.returndis(d1);

   cout<<"The distance after addition is :"<<endl;

   d3.display();

     getch();

        }

output:



2 Write a program to define class Complex with data members real,img(imaginary)

of apropriate type.Define member function of class to add

two complex and return to sum in main program.

 #include<iostream.h>

#include<conio.h>

class Complex

{

private:

          int real,img;

public:

         void getdetails();

         void display();

         Complex returnCom(Complex);

            };

 void Complex::getdetails()

   {

     cout<<"Real Part :";

     cin>>real;

     cout<<"Imaginary part :";

     cin>>img;

         }

   void Complex::display()

  {

    if(img>0)

    {

     cout<<real<<"+j"<<img;

         }

 else

  {

    cout<<real<<"j"<<(-1)*img;

    }

     }

  Complex Complex ::returnCom(Complex C1)

  {

  Complex C3;

  C3.real=C1.real+real;

  C3.img=C1.img+img;

  return C3;

  }

void main()

  {

  Complex C1,C2,C3;

  cout<<"enter the first complex number:"<<endl;

  C1.getdetails();

  cout<<endl;

  cout<<"enter the second complex number:"<<endl;

 C2.getdetails();

  cout<<endl;

  cout<<"First complex number is :"<<endl;

  C1.display();

  cout<<endl;

  cout<<"Second complex number is :"<<endl;

  C2.display();

  cout<<endl<<"The sum is :"<<endl;

  C3=C2.returnCom(C1);

 C3.display();

  getch();

    }

Output:




 3 Write a program to define a class named time with data members hours,
 minutes,seconds. Use appropriate member function of the class which accepts
 two objects of class and add them (BY REFERENCE VARIABLE)

#include<iostream.h>
#include<conio.h>
class Time
{
private:
   int hr,min,sec;

public:
     void getdetails();
     void display();
     void addTime(Time &,Time &);
        };
void Time::getdetails()
   {
       cout<<"Enter the hr :";
        cin>>hr;
      cout<<"Enter the min :";
       cin>>min;
      cout<<"Enter the sec :";
       cin>>sec;
           }
void Time::display()
     {
          cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
          }
 void Time:: addTime(Time &t1,Time &t2)
   {
    sec=t1.sec+t2.sec;
    min=sec/60;
    sec=sec%60;
    min=min+t1.min+t2.min;
    hr=min/60;
    min=min%60;

    hr=hr+t1.hr+t2.hr;
      }
  void main()
    {
       Time T1,T2,T3;
        cout<<"Enter the  first time :"<<endl;
         T1.getdetails();
         cout<<endl;
         cout<<"Enter the second time :"<<endl;
         T2.getdetails();
        cout<<endl;
        cout<<"The first time is:"<<endl;
      T1.display();
       cout<<endl;
      cout<<"the second time is:"<<endl;
      T2.display();
    cout<<endl;
      T3.addTime(T1,T2);

        cout<<"The resultant time after addition is :"<<endl;
       T3.display();
        getch();
             }
OUTPUT:



Comments