Related questions
Concept explainers
Please solve in C++
You are given the following files:
- main.cpp
- Classes.h
- Classes.cpp
The current implementation outputs:
Shape Colour: Red Shape Area: 0 Shape Colour: Blue Shape Area: 0
Change the code to use polymorphism and virtual functions to enable overriding of the Area function of the base class Shape to use the Area function of derived class Circle or Rectangle depending on the type of the object. The code should output:
Shape Colour: Red Shape Area: 78.5397 Shape Colour: Blue Shape Area: 24
main.cpp
#include <iostream>
#include <
#include "Classes.h"
int main() {
std::string red = "Red";
std::string blue = "Blue";
Circle circle(red, 5.0);
Rectangle rectangle(blue, 4.0, 6.0);
std::vector<Shape*> shapes;
shapes.push_back(&circle);
shapes.push_back(&rectangle);
for (Shape* shape : shapes) {
std::cout << "Shape Colour: " << shape->GetColour() << std::endl;
std::cout << "Shape Area: " << shape->Area() << std::endl;
}
return EXIT_SUCCESS;
}
Classes.h
#ifndef CLASSES_H
#define CLASSES_H
#include <string>
class Shape
{
protected:
std::string colour;
public:
Shape(std::string &c);
std::string GetColour();
double Area();
};
class Circle : public Shape
{
public:
Circle(std::string &c, double r);
double Area();\
private:
double radius;
};
class Rectangle : public Shape
{
private:
double width;
double height;
public:
Rectangle(std::string &c, double w, double h);
double Area();
};
#endif
Classes.cpp
#include "Classes.h"
Shape::Shape(std::string &c) : colour(c) {}
double Shape::Area()
{
return 0.0;
}
std::string Shape::GetColour()
{
return colour;
}
Circle::Circle(std::string &c, double r) : Shape(c), radius(r) {}
double Circle::Area()
{
return 3.14159 * radius * radius;
}
Rectangle::Rectangle(std::string &c, double w, double h) : Shape(c), width(w), height(h) {}
double Rectangle::Area()
{
return width * height;
}
Step by stepSolved in 4 steps with 1 images
- PART ONE In C# create a Contestant class with the following characteristics: The Contestant class contains public static arrays that hold talent codes and descriptions. Recall that the talent categories are Singing, Dancing, Musical instrument, and Other. Name these fields talentCodes and talentStrings respectively. The class contains an auto-implemented property Name that holds a contestant’s name. The class contains fields for a talent code (talentCode) and description (talent). The set accessor for the code assigns a code only if it is valid. Otherwise, it assigns I for Invalid. The talent description is a read-only property that is assigned a value when the code is set. Modify the GreenvilleRevenue.cs program (See Program Below) so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value...arrow_forwardImplement a nested class composition relationship between any two class types from the following list: Advisor Вook Classroom Department Friend Grade School Student Teacher Tutor Write all necessary code for both classes to demonstrate a nested composition relationship including the following: a. one encapsulated data member for each class b. inline default constructor using constructor delegation for each class c. inline one-parameter constructor for each class d. inline accessors for all data members e. inline mutators for all data membersarrow_forwardin c++ Define another new class named "Item" that must inherit from the given Name class. This Item class will manage the following info: name (string) and price (double). It must prevent the creation of an Item object with either an empty or all-blank name. This class must at least complete the methods defined in the Name class: toString method returns a string representation of the Item object in the format of "ITEM( <name> ) PRICE($ <price> ) such as ITEM(eraser) PRICE(1ドル.05) contains method that accepts a search string and returns true if only the name of the item contains the search string as a substring. It returns false otherwise. isTheSame method that compares two Item objects and returns true only if both Item objects have the exact same name (regardless of the price) and false otherwise. Please note that if the given object in the parameter is not an Item object, it will return false.arrow_forward
- (2) Your member functions will be: Make sure that the mutators for height and width only take positive numbers! Test your class with the driver program given below. //--- Test driver for class Rectangle #include using namespace std; setHeight (int h): mutator for height variable setWidth (int w): mutator for width variable setDimensions (int h, int w): sets both height and width getArea (): returns the area of the rectangle getPerimeter () returns the perimeter of the rectangle print (): prints a rectangle with your dimensions composed of asterisks (3) Create a default constructor that sets the default values of Rectangle class to 1. int main() { Rectangle rec rec2; recl.setHeight (10); recl.setWidth (20); Page rec2.setDimensions (5, 10); cout of 2 ZOOM +arrow_forwardInstructions-Java Assignment is to define a class named Address. The Address class will have three private instance variables: an int named street_number a String named street_name and a String named state. Write three constructors for the Address class: an empty constructor (no input parameters) that initializes the three instance variables with default values of your choice, a constructor that takes the street values as input but defaults the state to "Arizona", and a constructor that takes all three pieces of information as input Next create a driver class named Main.java. Put public static void main here and test out your class by creating three instances of Address, one using each of the constructors. You can choose the particular address values that are used. I recommend you make them up and do not use actual addresses. Run your code to make sure it works. Next add the following public methods to the Address class and test them from main as you go: Write getters and...arrow_forward
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY