Question

In C++, Write a Circle class that has the following member variables: radius : a double...

In C++,

Write a Circle class that has the following member variables:

  • radius : a double

The class should have the following member functions:

  • Default Constructor: default constructor that sets radius to 0.0.

  • Constructor: accepts the radius of the circle as an argument.

  • setRadius: an mutator function for the radius variable.

  • getRadius: an accessor function for the radius variable.

  • getArea: returns the area of the circle, which is calculated as area = pi * radius * radius

  • getCircumference: returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius

Step1: Create a declaration of the class.

Step2: Write a program that demonstrates the Circle class by asking the user for the circle’s radius, creating

a Circle object, and then reporting the circle’s area, and circumference.

Homework Answers

Answer #1
#include <iostream>

using namespace std;

#define pi 3.14159

class Circle
{
private:
   double radius;
public:
   Circle();
   Circle(double r);
   void setRadius(double);
   double getRadius();
   double getArea();
   double getCircumference();
};

Circle::Circle() {
   radius = 0;
}

Circle::Circle(double r) {
   setRadius(r);
}

void Circle::setRadius(double r) {
   radius = r;
}

double Circle::getRadius() {
   return radius;
}

double Circle::getArea() {
   return pi * radius * radius;
}

double Circle::getCircumference() {
   return 2 * pi * radius;
}

int main() {
   Circle circle;
   double radius;
   cout << "Enter radius of circle: ";
   cin >> radius;
   circle.setRadius(radius);
   cout << "Area: " << circle.getArea() << endl;
   cout << "Circumference: " << circle.getCircumference() << endl;
   return 0;
}

Know the answer?
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
Similar Questions
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
Write an object oriented programming (JAVA) code. Create a class called Circle. The class has an...
Write an object oriented programming (JAVA) code. Create a class called Circle. The class has an attribute radius, which default to 1 by the first no-arg constructor. The class also has another constructor that instantiates the radius to a given value. Provide methods that calculate the circumference and the area of the circle (use Math.PI and Math.pow). Provide set and get methods. The set method should verify that radius is greater than or equal to 0.0 and less than 20.0,...
java Consider the following class definition: public class Circle { private double radius; public Circle (double...
java Consider the following class definition: public class Circle { private double radius; public Circle (double r) { radius = r ; } public double getArea(){ return Math.PI * radius * radius; } public double getRadius(){ return radius; } } a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.” b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return...
C++ Derive the cylinder class from the base circle class. Assume the circle class has a...
C++ Derive the cylinder class from the base circle class. Assume the circle class has a protected member variable representing the radius called radius and declared as a double with a default value of 1.0. It also has a public function called calcVal that evaluates the area of a circle as PI * radius * radius where PI is a constant 3.14. In your derived class include an additional protected member representing the length of the cylinder. Call this variable...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other functions * with the following differences: * 1) The function name has the format Class::Method * 2) The function can access any private or public data * defined for the class, e.g. "radius" in this example * 3) The constructor function(s) have no return data type ********************************************************/ #include <iostream> #include <string> #include "circle.h" // the definion of our Circle class using namespace std; /****************************************************...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
c++ Design a class named Account that contains (keep the data fields private): a) An int...
c++ Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate. f) A function named getMonthlyInterestRate() that...