Question

Need some help on this C++ program. Circle - color:String - radius:double +Circle() +Circle(newColor:String, newRadius:double) +setColor(color:String):void...

Need some help on this C++ program.

Circle
- color:String
- radius:double
+Circle()
+Circle(newColor:String, newRadius:double)

+setColor(color:String):void
+setRadius(radius:double):void

+getColor():String
+getRadius():double

+printCircleInfo():void

Create the class using three files - a .h, a .cpp and a main.cpp

  • Create a class called Circle as describe below
  • Constructor with no arguments
    • Sets radius to 1 and color to “black”
  • Constructor with arguments
    • Sets the color and radius to the values passed in
  • Get methods (accessors) return the field that the get refers to
  • Set methods (mutators) set the field to the new values passed in to it
  • printCircleInfo
    • declares a variable called area
    • computes the area of the circle
    • prints the color and area in the following format
           The red circle has area 78.54

Once you have tested the class. follow the comments in the main below to get the output shown

Main

#include 
#include 
#include "Circle.h"
using namespace std;

int main() {

    string color;
    double radius;

        // Declare and create a circle with the constructor that has no formal parameters
        // Call the object circle1


       // Declare and create a circle with the constructor that has formal parameters
       // The formal parameters to pass in should be "red" and 5
       // Call the object circle2


    cout << "Circle 1:" << endl;
       // Using the object circle1 call the print method in the class


    cout << "Circle 2:" << endl;
       // Using the object circle2 call the print method in the class       


    cout << "\nEnter a color for circle 1: ";
    cin >> color;
       // Use a method in the circle class to change the color of circle1 to be 
       // what the user just entered


    cout << "Enter a radius for circle 1: ";
    cin >> radius; 
       // Use a method in the circle class to change the radius of circle1 to be 
       // what the user just entered


    cout << "Enter a radius for circle 2: ";
    cin >> radius;
       // Use a method in the circle class to change the radius of circle2 to be 
       // what the user just entered


    cout << "\nCircle 1 after changes:" << endl;
       // Using the object circle1 call the print method in the class


    cout << "Circle 2 after changes:" << endl;
       // Using the object circle2 call the print method in the class       

    }

A sample output is given

Circle 1:
The black circle has area 3.14
Circle 2:
The red circle has area 78.54

Enter a color for circle 1: purple
Enter a radius for circle 1: 12.3
Enter a radius for circle 2: 5.28

Circle 1 after changes:
The purple circle has area 475.29
Circle 2 after changes:
The red circle has area 87.58 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H

class Circle
{
public:
Circle();
Circle(string newColor,double newradius);
// Setter and getter methods
void setRadius(double radius);

double getRadius();

// Function declarations
double getArea();

void setColor(string color);
void printCircleInfo();

string getColor();

private:
double radius;
string color;
};
#endif

______________________

// Circle.cpp

#include <iostream>
#define PI 3.14159
using namespace std;
#include "Circle.h"

Circle::Circle()
{
this->color="black";
this->radius=1;
}
Circle::Circle(string newColor,double newRadius)
{
this->radius=newRadius;
this->color=newColor;
}

//Setters and getters
void Circle::setRadius(double rad)
{
this->radius=rad;
}
double Circle::getRadius()
{
return radius;
}


void Circle::setColor(string color)
{
this->color=color;
}


string Circle::getColor()
{
return color;
}

void Circle::printCircleInfo()
{
double area;
area=3.14159*radius*radius;
cout<<"The "<<color<<" circle has area "<<area<<endl;
}

________________________

// main.cpp

#include <iostream>
using namespace std;
#include "Circle.h"
int main()
{

string color;
double radius;

// Declare and create a circle with the constructor that has no formal parameters
// Call the object circle1
Circle circle1;

// Declare and create a circle with the constructor that has formal parameters
// The formal parameters to pass in should be "red" and 5
// Call the object circle2
Circle circle2("red",5);

cout << "Circle 1:" << endl;
// Using the object circle1 call the print method in the class
circle1.printCircleInfo();

cout << "Circle 2:" << endl;
// Using the object circle2 call the print method in the class   
circle2.printCircleInfo();

cout << "\nEnter a color for circle 1: ";
cin >> color;
// Use a method in the circle class to change the color of circle1 to be
// what the user just entered
circle1.setColor(color);

cout << "Enter a radius for circle 1: ";
cin >> radius;
// Use a method in the circle class to change the radius of circle1 to be
// what the user just entered
circle1.setRadius(radius);

cout << "Enter a radius for circle 2: ";
cin >> radius;
// Use a method in the circle class to change the radius of circle2 to be
// what the user just entered
circle2.setRadius(radius);

cout << "\nCircle 1 after changes:" << endl;
// Using the object circle1 call the print method in the class
circle1.printCircleInfo();

cout << "Circle 2 after changes:" << endl;
// Using the object circle2 call the print method in the class   
circle2.printCircleInfo();

return 0;
}


__________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Need some help on this C++ program. Circle - color:String - radius:double +Circle() +Circle(newColor:String, newRadius:double) +setColor(color:String):void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Keep getting this error message when trying to compile in main1.cpp. Need help figuring out how...

    Keep getting this error message when trying to compile in main1.cpp. Need help figuring out how to make it compile. Thank you for your time. Error Message: main.cpp:(.text+0x13): undefined reference to `circle::circle()' main.cpp:(.text+0x32): undefined reference to `circle::circle(double)' main.cpp:(.text+0x3e): undefined reference to `circle::getArea() const' main.cpp:(.text+0x53): undefined reference to `circle::getRadius() const' main.cpp:(.text+0xb3): undefined reference to `circle::getArea() const' main.cpp:(.text+0xc8): undefined reference to `circle::getRadius() const' main.cpp:(.text+0x157): undefined reference to `circle::setRadius(double)' main.cpp:(.text+0x163): undefined reference to `circle::getArea() const' main.cpp:(.text+0x178): undefined reference to `circle::getRadius() const' main.cpp:(.text+0x207): undefined...

  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • Exercise 2: There can be several constructors as long as they differ in number of parameters...

    Exercise 2: There can be several constructors as long as they differ in number of parameters or data type. Alter the program so that the user can enter just the radius, the radius and the center, or nothing at the time the object is defined. Whatever the user does NOT include (radius or center) must be initialized somewhere. There is no setRadius function and there will no longer be a setCenter function. You can continue to assume that the default...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

  • #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius;...

    #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT