Question: Step 1: Implement a generic Shape class Were going to work with shapes in this lab exercise. Well create several classes to represent different shapes,

Step 1: Implement a generic Shape class

Were going to work with shapes in this lab exercise. Well create several classes to represent different shapes, some of them using inheritance. The first class well write is one to represent a generic shape with a name and a color.

Create two new files, shape.hpp and shape.cpp, and in them, define a Shape class. Heres the start of a class definition you should use:

class Shape { private:

std::string name;

std::string color;

public:

... };

You class should also have constructors, accessors, and mutators, as appropriate. In addition, your class should have an area() method for computing the shapes area. For this generic Shape class, the area() method can simply return 0, since we arent actually defining the shape itself.

In addition to your files shape.hpp and shape.cpp, create a new file application.cpp. In this file, write a simple main() function that instantiates some Shape objects and prints out their information. In addition, write a Makefile to specify compilation of your program. Make sure you compile your Shape class into an object file first, separately from the compilation of your application, and then use that object file when youre compiling your application.

Step 2: Implement Rectangle and Circle classes

Create new files rectangle.hpp, rectangle.cpp, circle.hpp, and circle.cpp, and in them, implement a Rectangle class and a Circle class. Both of these classes should be derived from your Shape class. The Rectangle class should have a width and a height, and the Circle class should have a radius. Here are the beginnings of definitions for these classes:

class Rectangle : public Shape {

private:

float width;

float height;

public:

...

};

class Circle : public Shape {

private:

float radius;

public:

...

};

Both of these classes should have constructors, accessors, and mutators, as needed, and each one should override the Shape classs area() method to compute areas that are appropriate for rectangles and circles.

Add some code to your application to instantiate and print out some Rectangle and Circle objects, and add rules to your Makefile to compile each of your new classes into separate object files, which you should then use when compiling your application.

Step 3: Implement a Square class

Now, create new files square.hpp and square.cpp, and in them, implement a Square class that derives from your Rectangle class. Your Square class should not contain any new data members, nor may you change any members of the Rectangle class to protected or public access. Instead, you should figure out how to implement a public interface for your Square class by appropriately using the width and height of your Rectangle class via its public interface (i.e. via the Rectangle classs constructors, accessors, and mutators). Specifically, the public interface to your Square class should use the public interface of your Rectangle class while enforcing the constraint that a squares width and height are equal.

Heres the start of a definition for your Square class, with no new data members:

class Square : public Rectangle {

public:

...

};

Once your Square class is written, add some lines to your application to instantiate and print out some Square objects, and add a Makefile rule to compile your class into an object file thats used in the compilation of your application.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!