Monday 29 August 2011

You are required to create a class in C++ named Price with the following Data members, Data members should be publicly declared.


DetailedDescription:

The Price class presents Price in Rupees and Paisa. For instance, Price (10, 80) means 10 rupees and 80 paisas. The Price class should have the following features as described in detailed descriptions:




Constructors



Class Price must have



  • Default constructor, which must set Rupees and Paisas to zero.




  • Parameterized constructor that receives two parameters of type int and initializes its private data: Rupees and Paisas with them. Note that if Paisas are 100 or greater than 100 then also convert it in Rupees.


Member Functions

  • Create a function named Print()that displays the price of object in terms of rupees and paisas.


Operator overloading

  • A member function that overloads the + Operator to add two objects of Price.




There should be an overloaded + operators:



  • Add two objects and return Price object. Note that Paisas should not exceed 100.

  • Add first number into second objects and return Price object. Note that paisas should not exceed 100.






Output of your program should be as follows:



Price is 10 rupees and 60 paisas



Price is 12 rupees and 80 paisas



After Addition



Price is 23 rupees and 40 paisas


Answer

#include <iostream.h>
#include <stdlib.h>
//definition for class Price
class Price {         
     
      private:             
              // private portion will contain no data member or function
             
      public:
         
              int rupees, paisas;   //declaration for private data members of Price class
             
      //prototypes for member functions and constructors of class
     
              Price();                  // default constructor for class
             
              Price(int, int);           //parameterized constructor for class 
              
              Price operator +(Price &);         // + operator overloading function
                  
              void Print();             // function prototype for printing price on screen
             
};              // class body ends here
// definition for default constructor of class
Price::Price()
{
              rupees = 0;
              paisas = 0;
}
// definition for parameterized constructor of class
Price::Price(int r, int p)
{
                 if(p >= 100)   // condition to check if value of member paisa is great than 100 or not
                 {
                      paisas = p % 100;           // this will convert paisas into rupees if paisas are greater than 100
                      rupees = r + (p/100);       // if value of paisa is 100 or more then increment value of rupee by 1
                 }                
                 else
                 {
                      rupees = r;
                      paisas = p;
                 }                                   
}
// Function definition for overloading + operator 
Price Price::operator + (Price &p)
{
      Price temp;       // declaring a temporary variable for strong the result of addition
     
      temp.rupees = rupees + p.rupees;     // adding rupees value of both Price object in the rupees value of temporary object of Price       
     
      temp.paisas = paisas + p.paisas;     // adding paisas value of both Price objects in the paisas value of temporary object of Price
     
      if(temp.paisas >=100)                // checking if value of paisas after addition is greater than 100 or not
      {
                    
                     temp.rupees = temp.rupees + temp.paisas / 100;  // these statements below will convert paisas into rupees if value of paisas is greater than hundred
                     temp.paisas = temp.paisas % 100;                    
     
      }  
     
      cout<<"After Addition: "<<endl<<endl;    
     
      return temp;                              // returning the temporary object of type Price back to the calling function.
}               
// function definition to display the information about object on screen.
void Price::Print()
{
     cout<<"Price is "<<rupees <<" rupees and "<<paisas <<" paisas";    // statament to print price information on screen
     cout<<endl<<endl;   
}
// definition of main() function starts here
main()
{
      // declaring and initializing three objects of 'Price'
     
      Price P1(15, 80), P2(12, 50);        // parameterized constructor will be called here
     
      // default constructor Price object will be called here
      Price P3;       
     
      // displaying the information about the objects on the screen
      P1.Print();
      P2.Print();
     
      P3 = P1 + P2; // statement for calling overloading function for '+' operator     
     
      P3.Print();   // displaying information on the screen
      system("pause");    // function to halt the display screen
}



No comments:

Post a Comment