Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//CODE MUST BE IN C++ Week 6 Lab Exercise =================== In this lab, youll start to work with inheritance in C++. ## Step 1: Clone

//CODE MUST BE IN C++

Week 6 Lab Exercise ===================

In this lab, youll start to work with inheritance in C++.

## Step 1: Clone the git repository for this lab

Were going to use git and GitHub Classroom again for this lab. Just like you did for the previous lab exercises, use git clone to download your repository onto your development machine using the clone URL from your repo on GitHub: ``` git clone REPO_URL ```

## Step 2: 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:

```c++ class Shape { private: std::string name; s td::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.

Once you have this all working, commit your `shape.hpp`, `shape.cpp`, `application.cpp`, and `Makefile` into your git repository, and push them back to your remote repo on GitHub.

## Step 3: 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:

```c++ 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.

Once you have this all working, add your new files to your git repository, commit all of your changes, and push them to your remote repo on GitHub.

## Step 4: 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:

```c++ 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. Make sure to add your new files to your git repo, commit all of your changes, and push them to your remote repo on GitHub.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

to encourage a drive for change by developing new ideas;

Answered: 1 week ago

Question

4 What are the alternatives to the competences approach?

Answered: 1 week ago