Question

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;
/****************************************************
* The code for Circle functions that set data,
* i.e. mutator functions.
*****************************************************/
void Circle::setRadius (double newRadius) {
if (newRadius >= 0){
radius = newRadius;
}
return;
}
void Circle::setColor(string newColor) {
color = newColor;
return;
}
/******************************************************
* The code for Circle functions that return data,
* i.e. accessor functions.
*******************************************************/
double Circle::getRadius() const {
return radius;
}
double Circle::getDiameter() const {
return (radius*2);
}
double Circle::getArea() const {
return (pi*radius*radius);
}
double Circle::getCircumference() const {
return (pi * getDiameter());
}
string Circle::getColor() const {
return color;
}
/******************************************************
* The implementation for all the constructors
*
* The constructors are special in that there is no
* return data type for the function. The function
* name for constructors is the same name as the class.
*******************************************************/
Circle::Circle() {
radius = 0;
pi = 3.14159;
color = "black";
}
Circle::Circle(double theRadius) {
radius = theRadius;
pi = 3.14159;
color = "black";
}
Circle::Circle(double theRadius, string theColor) {
radius = theRadius;
pi = 3.14159;
color = theColor;
}
/******************************************************
* The code for all the operators
*******************************************************/
Circle Circle::operator+(Circle op2) {
// Create a new circle using the original circle as a start
Circle result = Circle (radius, color);
// Increase the radius based on the 2nd circle operand
result.radius = radius + op2.radius;
// return the new Circle
return result;
}
Circle Circle::operator+(int op2) {
// Create a new circle using the original circle as a start
Circle result = Circle (radius, color);
// Increase the radius based on the 2nd integer operand
result.radius = radius + op2;
// return the new Circle
return result;
}
std::ostream &operator<<(std::ostream &os, const Circle &c) {
os << " color: " << c.getColor() << endl;
os << " radius: " << c.getRadius() << endl;
os << " diameter: " << c.getDiameter() << endl;
os << " circumference: " << c.getCircumference() << endl;
os << " area: " << c.getArea() << endl;
return os;
}


Circle.h

* 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;
/****************************************************
* The code for Circle functions that set data,
* i.e. mutator functions.
*****************************************************/
void Circle::setRadius (double newRadius) {
if (newRadius >= 0){
radius = newRadius;
}
return;
}
void Circle::setColor(string newColor) {
color = newColor;
return;
}
/******************************************************
* The code for Circle functions that return data,
* i.e. accessor functions.
*******************************************************/
double Circle::getRadius() const {
return radius;
}
double Circle::getDiameter() const {
return (radius*2);
}
double Circle::getArea() const {
return (pi*radius*radius);
}
double Circle::getCircumference() const {
return (pi * getDiameter());
}
string Circle::getColor() const {
return color;
}
/******************************************************
* The implementation for all the constructors
*
* The constructors are special in that there is no
* return data type for the function. The function
* name for constructors is the same name as the class.
*******************************************************/
Circle::Circle() {
radius = 0;
pi = 3.14159;
color = "black";
}
Circle::Circle(double theRadius) {
radius = theRadius;
pi = 3.14159;
color = "black";
}
Circle::Circle(double theRadius, string theColor) {
radius = theRadius;
pi = 3.14159;
color = theColor;
}
/******************************************************
* The code for all the operators
*******************************************************/
Circle Circle::operator+(Circle op2) {
// Create a new circle using the original circle as a start
Circle result = Circle (radius, color);
// Increase the radius based on the 2nd circle operand
result.radius = radius + op2.radius;
// return the new Circle
return result;
}
Circle Circle::operator+(int op2) {
// Create a new circle using the original circle as a start
Circle result = Circle (radius, color);
// Increase the radius based on the 2nd integer operand
result.radius = radius + op2;
// return the new Circle
return result;
}
std::ostream &operator<<(std::ostream &os, const Circle &c) {
os << " color: " << c.getColor() << endl;
os << " radius: " << c.getRadius() << endl;
os << " diameter: " << c.getDiameter() << endl;
os << " circumference: " << c.getCircumference() << endl;
os << " area: " << c.getArea() << endl;
return os;
}


Lab2a.cpp

/************************************************************
* Name : DeGood
* Class : CSC 230
* When : Fall 2019
* Professor : DeGood
*
* Assignment: Lab 2a
*
* To compile this, you also need circle.h and circle.cpp
*
* g++ lab2a.cpp circle.cpp -o lab2a.exe
*
* In this example, "Circle" is the class. circle1, circle2
* and circle3 are objects.
*************************************************************/
#include <iostream>
using namespace std;
#include "circle.h" // Include the definition for our Circle class
int main() {
// Define three different circle's using our new Circle class
// Each one uses a different one of the constructors we wrote.
Circle circle1;
Circle circle2 = Circle(5);
Circle circle3 = Circle(6,"red");
Circle circle4;
// Display the information about each circle
// When you call the print() method, the code that was
// written in the circle.cpp file for the print()
// method is executed.
cout << "Circle1's infomation:" << endl;
cout << circle1 << endl;
cout << "Circle2's infomation:" << endl;
cout << circle2 << endl;
cout << "Circle3's infomation:" << endl;
cout << circle3 << endl;
// test some of our other functions
circle3.setRadius(-2);
cout << "Circle3's new infomation:" << endl;
cout << circle3 << endl;
circle4 = circle2 + circle3;
cout << "Circle4's infomation:" << endl;
cout << circle4 << endl;
circle4 = circle4 + 5;
cout << "Circle4's new infomation:" << endl;
cout << circle4 << endl;
return 0;
} // end of main()

Homework Answers

Answer #1

//C++ code

//============ circle.h======

#ifndef _CIRCLE
#define _CIRCLE
#include<iostream>
using namespace std;
class Circle
{
private:
   double radius;
   string color;
   double pi;
public:
   void setRadius(double newRadius);
   void setColor(string newColor);
   double getRadius() const;
   double getDiameter() const;
   double getArea() const;
   double getCircumference() const;
   string getColor() const;
   Circle();
   Circle(double theRadius);
   Circle(double theRadius, string theColor);
   Circle operator+(Circle op2);
   Circle operator+(int op2);
   friend ostream& operator<<(std::ostream& os, const Circle& c);
};
#endif

//================== circle.cpp ===================

#include <iostream>
#include <string>
#include "circle.h" // the definion of our Circle class
using namespace std;
/****************************************************
* The code for Circle functions that set data,
* i.e. mutator functions.
*****************************************************/
void Circle::setRadius(double newRadius) {
   if (newRadius >= 0) {
       radius = newRadius;
   }
   return;
}
void Circle::setColor(string newColor) {
   color = newColor;
   return;
}
/******************************************************
* The code for Circle functions that return data,
* i.e. accessor functions.
*******************************************************/
double Circle::getRadius() const {
   return radius;
}
double Circle::getDiameter() const {
   return (radius * 2);
}
double Circle::getArea() const {
   return (pi * radius * radius);
}
double Circle::getCircumference() const {
   return (pi * getDiameter());
}
string Circle::getColor() const {
   return color;
}
/******************************************************
* The implementation for all the constructors
*
* The constructors are special in that there is no
* return data type for the function. The function
* name for constructors is the same name as the class.
*******************************************************/
Circle::Circle() {
   radius = 0;
   pi = 3.14159;
   color = "black";
}
Circle::Circle(double theRadius) {
   radius = theRadius;
   pi = 3.14159;
   color = "black";
}
Circle::Circle(double theRadius, string theColor) {
   radius = theRadius;
   pi = 3.14159;
   color = theColor;
}
/******************************************************
* The code for all the operators
*******************************************************/
Circle Circle::operator+(Circle op2) {
   // Create a new circle using the original circle as a start
   Circle result = Circle(radius, color);
   // Increase the radius based on the 2nd circle operand
   result.radius = radius + op2.radius;
   // return the new Circle
   return result;
}
Circle Circle::operator+(int op2) {
   // Create a new circle using the original circle as a start
   Circle result = Circle(radius, color);
   // Increase the radius based on the 2nd integer operand
   result.radius = radius + op2;
   // return the new Circle
   return result;
}
std::ostream& operator<<(std::ostream& os, const Circle& c) {
   os << " color: " << c.getColor() << endl;
   os << " radius: " << c.getRadius() << endl;
   os << " diameter: " << c.getDiameter() << endl;
   os << " circumference: " << c.getCircumference() << endl;
   os << " area: " << c.getArea() << endl;
   return os;
}

//======================= main.cpp =====================

#include <iostream>
using namespace std;
#include "circle.h" // Include the definition for our Circle class
int main() {
   // Define three different circle's using our new Circle class
   // Each one uses a different one of the constructors we wrote.
   Circle circle1;
   Circle circle2 = Circle(5);
   Circle circle3 = Circle(6, "red");
   Circle circle4;
   // Display the information about each circle
   // When you call the print() method, the code that was
   // written in the circle.cpp file for the print()
   // method is executed.
   cout << "Circle1's infomation:" << endl;
   cout << circle1 << endl;
   cout << "Circle2's infomation:" << endl;
   cout << circle2 << endl;
   cout << "Circle3's infomation:" << endl;
   cout << circle3 << endl;
   // test some of our other functions
   circle3.setRadius(-2);
   cout << "Circle3's new infomation:" << endl;
   cout << circle3 << endl;
   circle4 = circle2 + circle3;
   cout << "Circle4's infomation:" << endl;
   cout << circle4 << endl;
   circle4 = circle4 + 5;
   cout << "Circle4's new infomation:" << endl;
   cout << circle4 << endl;
   return 0;
} // end of main()

//Output

//If you need any help regarding this solution ............ please leave a comment ......... thanks

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
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include...
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include <string> #include <ostream> using namespace std; class Book { private:     string title;     string author;     unsigned isbn;     double price; public: /**Constructors*/    Book();    Book(string t, string a, unsigned i, double p);     /**Access Functions*/     string get_title();     string get_author();     unsigned get_isbn();     double get_price();     /**Manipulation Procedures*/    void set_title(string t);     void set_author(string a);     void set_isbn(unsigned...
What is my code missing? #include <iostream> #include <string> using namespace std; const double PI =...
What is my code missing? #include <iostream> #include <string> using namespace std; const double PI = 3.141592; const int LEGAL_AGE = 21; int main() {    double radius;   // input variable holds radius of circle    int age;       // input variable holds age of user    string name;       // input variable holds first name of user    // prompt user for radius    cout << "Please enter the radius of your circle: ";    cin >> radius;   ...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
Requirements The assignment is to create a dynamic array implementation of a set (defined in set.h)....
Requirements The assignment is to create a dynamic array implementation of a set (defined in set.h). Add the efficiency of each function to the documentation in the herder file. Use the test_set.cpp as your test program. _______________________________________________________________________________________________________________________________________________________ set.h file #ifndef _SET_H_ #define _SET_H_ #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; set(size_type initial_capacity); // postcondition: empty set with initial_capacity has been created ~set(); // postcondition: all dynamically allocated memory has been deallocated set(const set&...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark them in the code and explain the reason briefly in place. #include<iostream> using namespace std; int main() { cout<<”Please input the radius (in integer): ”>>endl; int 1var = 10, var=20; double area = 40; cin<< var; //get an input of the radius and store in var float continue; float const pi = 2.64; pi += 0.5; do { cout<<”The radius is ”<<”var”<<endl; //print the...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
Using C++, write the following program: The Point.h file declares the class and you will create...
Using C++, write the following program: The Point.h file declares the class and you will create a Point.cpp that contains the implementation and a main() that instantiates the Point class to add additional tests to cover the various overloaded operators Use the Point.h file that is found below (This class contains a point on a plane and this class is going to contain a X coordinate and Y coordinate. The class is also going to contain a member function that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT