CS304 Object Oriented Programming Assignments Discussions and Solutions

Information

CS304 Object Oriented Programming  Assignments Discussions and Solutions

Assignments Discussions and Solutions Group

Website: http://www.virtualians.pk/
Members: 303
Latest Activity: Dec 6

Discussion Forum

CS304 Object Oriented Programming Assignment No. Solution and Discussion Fall 2015 Due Date: Feb 11, 2016

Started by ❀•*¨*❀мα¡яα в§.¡ţ❀•*¨*❀. Last reply by Maimoona Khan (MIT 1st) Feb 9, 2016. 8 Replies

Comment Wall

Comment

You need to be a member of CS304 Object Oriented Programming Assignments Discussions and Solutions to add comments!

Comment by Mudassar Nisar on August 1, 2015 at 3:32pm

ya solutin ksa ha

Comment by Mudassar Nisar on August 1, 2015 at 3:32pm

#include <iostream>

using namespace std;

class Animal{

 public:

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

  virtual float calDistanceCovered(){    // Virtual Function

  return 0.0;

 }

};

class Duck:public Animal{

 private:

  char* color;

  float speed,time,distance;

 public:

  Duck(char* clr,float s,float t){

   color = clr;

   speed = s;

   time = t;

  }

  float calDistanceCovered(){

   distance = speed*time;

   cout"\tDistance Covered by Duck in KM : ";

   return distance;

  }

  char* GetColor(){

   cout"Duck Color : ";

   return color;

  }

};

class Horse:public Animal{

 private:

  char*color;

  float speed,time,distance;

 public:

  Horse(char* clr,float s,float t){

   color = clr;

   speed = s;

   time = t;

  }

  float calDistanceCovered(){

   distance = speed*time;

   cout"\tDistance Covered by Horse in KM : ";

   return distance;

  }

  char* GetColor(){ 

   cout"Horse Color : ";

   return color;

  }

};

 

int main(){

 Animal* obj[5];

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

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

 obj[2]= new Horse("Brown",12.8,3);

 obj[3] = new Duck("Pink",6,2);

 obj[4]= new Duck("Red",1.5,1.5);

 for(int i=0;i<5;i++){

  coutobj[i]->GetColor();

  coutobj[i]->calDistanceCovered()endl;

 }

 return 0;

}

 

 

Comment by Mudassar Nisar on July 30, 2015 at 10:16pm

solve the assaigmnet 3 cs304 any one help me..........

Comment by Mudassar Nisar on July 30, 2015 at 10:16pm

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;

 
 
 

SPONSORED LINKS

SPONSORED LINKS

© 2018   Created by Irfan Khan MSCS.   Powered by

Badges  |  Report an Issue  |  Terms of Service