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()
//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
Get Answers For Free
Most questions answered within 1 hours.