Language Bar

Friday, January 21, 2011

CS201 Assignment 4 SOLUTION

Assignment No. 4
Semester: Fall 2010
CS201: Introduction to Programming
Total Marks: 20

Due Date: 24th Jan, 2011

Please read the following instructions carefully before submitting your assignment:
It should be clear that your assignment will not get any credit if:

§         The assignment is submitted after due date.
§         The submitted assignment does not open or file is corrupt.

All types of plagiarism are strictly prohibited.

Note: You have to upload only .cpp file. Assignment in any other format (extension) will not be accepted. If you will submit code any other file format like .doc or .txt etc. you will get zero marks.

The objective of this assignment is to provide hands on experience of using

§         Classes and objects manipulation in c++
§         Operators overloading

§         Code should be properly aligned and well commented.
§         Follow C/C++ rules while writing variables names, function names etc.
§         Use only Dev-C++ IDE for this assignment.


Problem Statement: Price manipulation

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

  • Rupees
  • Paisas

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:

Detailed Description:

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

Note: you have to use same Class and Function name as mentioned in the assignment details. If we find any deviation, marks will be deducted.

Your Assignment solution must be submitted on or before January 24th, 2011.

Only CPP file will be accepted, file other than CPP will be awarded zero (0) marks.


#include <iostream.h>
#include <conio.h>
class price{                 //Decelaration Class name is Price
        int rupies;
        int paisas;
                            //Public function Declaring
        price();            //Default constructor
        price (int,int);    //Overloaded constructor
        void Print();            //print function for display the array content
        price operator+(price); //Operator overloading function for operator +

                      // Default constructor
    rupies = 0; 
    paisas = 0;

                     // Overload constructor
price::price(int crupies, int cpaisas)
    rupies = crupies;  
    paisas = cpaisas;

                         //Function to display the content
 void price::Print() 
    cout<<"  Price is  "<<rupies<<"  Rupees and  "<<paisas<<"  Paisas  "<< endl;
// Function for operator overloading
price price::operator+(price d2)
      int r = rupies + d2.rupies; //adding rupeies
      int p = paisas + d2.paisas; //adding paisas
      if (p>=100)                  //If paisas exceeds from 100 then adding
      p-=100;                          // then into rupies                       
      return price (r,p);     
price result;
price obj1(10, 60);
price obj2 (12, 80);
result = obj1+obj2;
cout<<"After Addition\n\n";



1 comment:

  1. Hey, I think your blog might be having browser compatibility issues.
    When I look at your blog site in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, very good blog!

    Feel free to surf to my webpage; natural cellulite treatment




comments powered by Disqus