Mcs 043 Assignment Solution Cs304

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:

private:

    char* color;

    float speed, time,distance;

public:

           Duck(char*,float,float );

           virtual float calDistanceCovered();

           char* GetColor();

Horse class will have the following members:

private:

            char *color;

            float speed, time,distance;

  public:

             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;

Она попыталась собраться с мыслями, но они упрямо возвращали ее к. Дэвид Беккер. Единственный мужчина, которого она любила.

Comments

Leave a Reply

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