Cs304 Assignment No 2


Please read the following instructions carefully before submitting assignment:

It should be clear that your assignment will not get any credit if:

  • The assignment is submitted after due date.
  • The assignment is submitted via email.
  • The assignment is copied from Internet or from any other student.
  • The submitted assignment does not open or file is corrupt.
  • It is in some format other than.cpp.

Note:You are supposed to submit your assignment in .cpp format. Any other formats like scan images, PDF, zip, doc, rar and bmp etc will not be accepted.All types of plagiarism are strictly prohibited.

For any query about the assignment, contact atCS304@vu.edu.pk


The objective of this assignment is:

  • To give you the idea of practical implementation of Inheritance in CPP.

Problem Statement:

Suppose METRO Cash & Carry Pakistan has given you a task to develop an automatic checkout system in C++. All items are identifiable by means of a merchandise computer code (barcode) and the item name. Groceries are either sold in packs or by weight. Packed items have fixed prices. The price of groceries sold by weight is calculated by multiplying the weight by the current price per kilogram. As a software developer your task is to develop the classes needed to represent the products first and organize them hierarchically according to the UML diagram given on next page.



Bar code

Item Name









Price per piece









Price per rupee







Figure: UML forMETRO Cash & Carry Pakistan

Tasks for you:

  1. Make a base class named Item then define its private and public members. Define a constructor with parameters for both data members (Barcode, Item Name). You should add default values for the parameters to provide a default constructor for the class. In addition to the access methods setCode() and getCode(), you are also required to define the methods scanner() and printer(). These methods will simply output item data on screen or read the data of an item from the keyboard.
  2. Define two derived classes PackedFood and FreshFood. In addition to the Item class data, the PackedFood class should contain the unit price. The FreshFood class should contain a weight and a price per kilogram as data members. You are required to define a constructor with parameters providing default-values for all data members in both classes. Also define the access methods needed for the new data members.
  3. Make the main() function then test the classes in it, that creates two objects each of the types Item, PackedFood and FreshFood. One object of each type should be fully initialized in the object definition. You can use the default constructor to create the other object. The get() and set() methods and the scanner() method should be well written and the printer() method should display the items on screen.

Problem Statement

In this assignment you will write code about animal hierarchy classes. There should be base class Animal which is abstract class by declaring pure virtual function.

From Animal Class, two classes will be inherited. One is Duck and other is Horse class. Duck and Horse classes are concreteclasses.

Animal Class has the following members:

  • virtual char * GetColor() = 0;            //Pure Virtual Function
  • virtual float calDistanceCovered();

Here, GetColor() function returns color which will be passed while instantiating objects in main function and CalDistanceCoverered() function will calculate distance covered by any animal according to the following formula and return distance.

                     Distance=speed * time;

Speed and time will also be passed as parameter while instantiating objects of classes.

Speed, time and color should be set in constructors of the classes.

Duck class will have the following members:


    char* color;

    float speed, time,distance;


           Duck(char*,float,float );

           virtual float calDistanceCovered();

           char* GetColor();

Horse class will have the following members:


            char *color;

            float speed, time,distance;


             Horse(char*, float , float)

             float calDistanceCovered();

             virtual char * GetColor();

In main function, create an array of 5 pointers to Animal Class as:

               Animal *obj[5];

Then assign different objects of derived classes as:

obj[0]=new Duck("White",2,2.5);

            obj[1]=new Horse("Red",10,6);



Here the first parameter is color, 2nd is speed in Km/h and 3rd is for time in hours.

Now call the function as below by passing array pointer and size;

0 thoughts on “Cs304 Assignment No 2

Leave a Reply

Your email address will not be published. Required fields are marked *