Sie sind auf Seite 1von 4

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE EC101A: Computer Systems and Programming Autum Semester: 2010-2011 Oct 19, 2011

1. A store wants to computerize its cashier/check-out system. You as an IIT-R EC101A student have been hired to build this system using C++. The initial requirement is to capture the requirements and develop C++ classes. You are required to develop a base class called customer, which captures various parameters related to the customer and provides methods to use/manipulate these parameters. A customer has a unique id, which is assigned automatically by the system. This id will never change. Ids start from 1, for the first customer and go on increasing as customers are added. With every purchase, customers are awarded points, which are stored in the customers record. You should provide methods to retrieve points and customers id, respectively. Even though the id is assigned automatically, the store wants you to provide a method int assign_cust_id(int id) to assign a specific id. Assume: 1. Once a person becomes a customer, they are treated as permanent customers. Their ids do not change. 2. Store can handle maximum of 100 customers. 3. Points never decrease. 4. Do not worry about duplicate ids when using the method assign_cust_id (int id). 2. Create class one_d. The private member of this class is coordinate x of type int. The public members are: void set_coord (int a); virtual void display_coord( ). Then create class two_d as a derived class from class one_d. Create class three_d as derived class from class two_d. The main should display the contents of the objects of these three classes using display() function invoked through a pointer to one_d type. Create a class Volume {public: Volume( double r){radius = r;} virtual void displayVolume( ) = 0; protected: double radius, height; static double pi; }; Using class Volume as the base class, derive class Sphere, class Cylinder and class Cone. In these classes define the function displayVolume( ). Write main to test the classes and the polymorphic behavior of displayVolume function. Take pi = 3.14159. Create a class figure {protected ; double x, y; public: void set_dim( double i, double j); virtual void show_area = 0;};. From base class figure, derive class rectangle and class triangle to compute areas of rectangle and triangle, respectively. Write main to test the classes. Understand how the functions in the derived classes are invoked to compute the area.

3.

4.

5.

Write class Exchange {protected: double rs, exchangeRate, amount; public: Exchange (double rupees, double rate){rs = rupees; exchangeRate = rate;} virtual void convert( ) = 0; virtual void display( ) = 0;};. From the base class Exchange derive the class Rs_to_Dollar {public: Rs_to_Dollar (double rupees, double rate): Exchange (rupees, rate){} void convert( ); void display( )};. Further, from the base class Exchange, derive another class Rs_to_Euro. class Rs_to_Euro : public Exchange { public: Rs_to_Euro(double rupees, double rate) : Exchange ( rupees, rate){} void convert( ); void display( ); };. The convert function converts rs to appropriate currency using the exchange rate and puts the value into data member amount. Write main to test the classes. Consider the following program: int main ( ) { Rectangle r (4, 5); Triangle t (4, 5); Polygon *p1 = &r;//base class pointer declared and initialized Polygon *p2 = &t; P1->print_area( ); P2->print_area( ); cin.get( ); return 0; } The output of the above program is: Area of Rectangle object = 20 Area of Triangle object = 10 Develop the classes Polygon, Rectangle and Triangle in such a way that the program produces the shown output.

6.

7. Student marks are represented as x/y where x is the score obtained and y is the maximum marks, with the condition that the x y . Using a notation {x, y} for this marks object, some operations on the object can be defined as (rhs shows the result of operation) (i) {x, y } + N = { x+ N, y }//adding a constant to score such that x + N y (ii) {x1,y 1}+ { x2, y2 }= {x1+x2, y1+y2}//adding two marks objects (iii) {x, y} changeSscore (int m) = {m.y}// changing the score to a new value m taking care that m y (iv) {x1, y1} > {x2, y2} = true, if (x1/y1) > (x2/y2)//comparing two marks objects. Develop a class Marks to represent the above object and provide member functions for the operations listed above. The two plus-operations (i and ii) and the comparison should be done through operator overloading. Further, provide appropriate constructors, a copy constructor to the class, and a friend function to overload the insertion operator which displays the marks in the form ~The score is x out of y

8 Create a class Point having two float data members of which represent coordinates of a point in two-dimensional space. Following member functions are required in the class: a. A default constructor (point at origin), b. Constructor with two arguments x and y as coordinates, c. Operator overloading function that overloads insertion << operator to display the coordinates of a point object in the format: [x,y] d. Operator overloading function that overloads the binary operator - (minus) which returns the distance between two point objects, and e. A function void shift(float r, float theta) that shifts the point in twodimensional space as specified by its parameters. f. A function distance of the type int distance(Point p). Write C++ codes for all member functions of the class. Write a main that creates a point object A located at (2.3, 5.0). Further, create a point object D located at (4,6). Write statements to display the locations of two points. Further, using overload operator -(minus), write code to find and display the distance between the points A and D. . 9 Extend the Point class of question 8.to create a class Triangle as a composition of three point objects ,that represents a triangle in x-y plane.Include appropriate constructor(s) in this class and write a void shift_triangle(float r,float theta) function that makes use of the shift(float r, float theta) function of the Point class which shifts the triangle by an amount r along theta direction 10. Extend the Point class of question 8.through public inheritance to create a class Circle. Write a constructor for this class that create a unit circle at origin.Further write a member function bool insideCircle(Point p) that determines whether the point p is inside the circle or outside.A point on the circumference should be treated as inside. 11. Define a class Account ( to represent a bank account) with the following specifications: (a) All data members should be in private visibility mode. (b) The data members are: (i) AccountCount (integer type static member); (ii) Name of the account holder (string type); (iii) Type of account `S for saving account and `C for current account; (iv) Account Number (integer); (v) Balance Amount (double). Minimum balance in any account is Rs. 1000. (d) public member functions:-: (i) default constructor which increments is the static member AccountCount and sets the account number to the incremented value. (ii) void AccountOpening( ): to input the name, account type amount from user and set the data members accordingly. (iii) double deposit (double d): to deposite money- the function should increment the current balance by amount d and return the new balance;

(iv)

(v) (vi) (vii)

bool double withdraw (double w): to withdraw money- should decrement the current balance by amount w, but before doing the withdrawl, it should make sure that the balance after withdrawing will not drop below Rs. 1000 and give an appropriate message; should return true or false depending on whether withdrawl is possible or not. double getBalance ( ): to return the Balance Amount; to display all the data members through an operator overloading function; void transfer ( Account a, double b): to transfer an amount b from calling account object to the account object a. The transfer cannot be allowed if it leaves a balance of less than Rs. 1000 in the calling object.

12. Create a class Circle with private double type members radius. It should have the public members: (i) parameterized constructor, (ii) double area ( ), (iii) virtual double volume ( ). Create, through inheritance from Circle , a class cone with appropriate additional data members , constructor, definition of virtual function, and a member void display Volume( ). Write client code to test the classes.

Das könnte Ihnen auch gefallen