Tuesday, December 9, 2014

operator overloading converting and onverting int to class object in C++ Programming



Program for converting int to class object in C++ Programming

#include 
#include 
class time
{
  int hrs;
  int mins;
  public:
    time()
    {}
    time(int t)
    {
      hrs=t/60;
      mins=t%60;
    }
    void display()
    {
     cout<<"\nHours   = "<
     cout<<"\nMinutes = "<
    }
};
void main()
{
 clrscr();
 time o1;
 int  duration;
 cout<<"Enter Duration : ";
 cin>>duration;
 o1=duration;        
 o1.display();
 getch();
}

Perform operator overloading converting one class object to another in C++ Programming

#include 
#include 
class invent1
{
 int code;
 int items;
 float price;
  public:
  invent1()
  {}
  invent1(int a,int b,int c)
  {
    code=a;
    items=b;
    price=c;
  }
  void display()
  {
    cout<<"\nCode  : "<
    cout<<"\nItems : "<
    cout<<"\nPrice : "<
  }
  int getcode()
  {
    return code;
   }
  int getitem()
  {
    return items;
   }
  int getprice()
  {
       return price;
   }
};
class invent2
{
  int code;
  floatvalue;
   public:
             invent2()
    {
      code=0;
      value=0;
    }
    invent2(int x,float y)
    {
      code=x;
      value=y;
    }
    void display()
    {
     cout<<"Code  : "<
     cout<<"Value : "<<value<
    }
    invent2(invent1 p)
    {
     code=p.getcode();
     value=p.getitem()*p.getprice();
    }
};
void main()
{
 clrscr();
 invent1 s1(100,5,140);
 invent2 d1;
 d1=s1;  
 cout<<"\nProduct details - Invent1 type";
 s1.display();
 cout<<"\n\n\nProduct details - Invent2 type\n";
 d1.display();
 getch();
}

No comments:

Post a Comment