Consider the expression: cout << myCircle; cin>>yourCircle; Write the operator f
ID: 3802343 • Letter: C
Question
Consider the expression:cout << myCircle;
cin>>yourCircle;
Write the operator functions that overload << and >> for circleType such that the above expressions should work. The functions should be friend functions.
What are the header, main, and implementation files
---
class circleType
{
public:
void setRadius(double r);
//Function to set the radius.
//Postcondition: if (r >= 0) radius = r;
// otherwise radius = 0;
double getRadius();
//Function to return the radius.
//Postcondition: The value of radius is returned.
double area();
//Function to return the area of a circle.
//Postcondition: Area is calculated and returned.
double circumference();
//Function to return the circumference of a circle.
//Postcondition: Circumference is calculated and returned.
circleType(double r = 0);
//Constructor with a default parameter.
//Radius is set according to the parameter.
//The default value of the radius is 0.0;
//Postcondition: radius = r;
private:
double radius;
};
---
//Implementation File for the class circleType
#include <iostream>
#include "circleType.h"
using namespace std;
void circleType::setRadius(double r)
{
if (r >= 0)
radius = r;
else
radius = 0;
}
double circleType::getRadius()
{
return radius;
}
double circleType::area()
{
return 3.1416 * radius * radius;
}
double circleType::circumference()
{
return 2 * 3.1416 * radius;
}
circleType::circleType(double r)
{
setRadius(r);
} Consider the expression:
cout << myCircle;
cin>>yourCircle;
Write the operator functions that overload << and >> for circleType such that the above expressions should work. The functions should be friend functions.
What are the header, main, and implementation files
---
class circleType
{
public:
void setRadius(double r);
//Function to set the radius.
//Postcondition: if (r >= 0) radius = r;
// otherwise radius = 0;
double getRadius();
//Function to return the radius.
//Postcondition: The value of radius is returned.
double area();
//Function to return the area of a circle.
//Postcondition: Area is calculated and returned.
double circumference();
//Function to return the circumference of a circle.
//Postcondition: Circumference is calculated and returned.
circleType(double r = 0);
//Constructor with a default parameter.
//Radius is set according to the parameter.
//The default value of the radius is 0.0;
//Postcondition: radius = r;
private:
double radius;
};
---
//Implementation File for the class circleType
#include <iostream>
#include "circleType.h"
using namespace std;
void circleType::setRadius(double r)
{
if (r >= 0)
radius = r;
else
radius = 0;
}
double circleType::getRadius()
{
return radius;
}
double circleType::area()
{
return 3.1416 * radius * radius;
}
double circleType::circumference()
{
return 2 * 3.1416 * radius;
}
circleType::circleType(double r)
{
setRadius(r);
} Consider the expression:
cout << myCircle;
cin>>yourCircle;
Write the operator functions that overload << and >> for circleType such that the above expressions should work. The functions should be friend functions.
What are the header, main, and implementation files
---
class circleType
{
public:
void setRadius(double r);
//Function to set the radius.
//Postcondition: if (r >= 0) radius = r;
// otherwise radius = 0;
double getRadius();
//Function to return the radius.
//Postcondition: The value of radius is returned.
double area();
//Function to return the area of a circle.
//Postcondition: Area is calculated and returned.
double circumference();
//Function to return the circumference of a circle.
//Postcondition: Circumference is calculated and returned.
circleType(double r = 0);
//Constructor with a default parameter.
//Radius is set according to the parameter.
//The default value of the radius is 0.0;
//Postcondition: radius = r;
private:
double radius;
};
---
//Implementation File for the class circleType
#include <iostream>
#include "circleType.h"
using namespace std;
void circleType::setRadius(double r)
{
if (r >= 0)
radius = r;
else
radius = 0;
}
double circleType::getRadius()
{
return radius;
}
double circleType::area()
{
return 3.1416 * radius * radius;
}
double circleType::circumference()
{
return 2 * 3.1416 * radius;
}
circleType::circleType(double r)
{
setRadius(r);
}
What are the header, main, and implementation files
---
class circleType
{
public:
void setRadius(double r);
//Function to set the radius.
//Postcondition: if (r >= 0) radius = r;
// otherwise radius = 0;
double getRadius();
//Function to return the radius.
//Postcondition: The value of radius is returned.
double area();
//Function to return the area of a circle.
//Postcondition: Area is calculated and returned.
double circumference();
//Function to return the circumference of a circle.
//Postcondition: Circumference is calculated and returned.
circleType(double r = 0);
//Constructor with a default parameter.
//Radius is set according to the parameter.
//The default value of the radius is 0.0;
//Postcondition: radius = r;
private:
double radius;
};
---
//Implementation File for the class circleType
#include <iostream>
#include "circleType.h"
using namespace std;
void circleType::setRadius(double r)
{
if (r >= 0)
radius = r;
else
radius = 0;
}
double circleType::getRadius()
{
return radius;
}
double circleType::area()
{
return 3.1416 * radius * radius;
}
double circleType::circumference()
{
return 2 * 3.1416 * radius;
}
circleType::circleType(double r)
{
setRadius(r);
}
Explanation / Answer
Please refer below code
1) circleType.h
#include<iostream>
using namespace std;
class circleType
{
public:
void setRadius(double r);
//Function to set the radius.
//Postcondition: if (r >= 0) radius = r;
// otherwise radius = 0;
double getRadius();
//Function to return the radius.
//Postcondition: The value of radius is returned.
double area();
//Function to return the area of a circle.
//Postcondition: Area is calculated and returned.
double circumference();
//Function to return the circumference of a circle.
//Postcondition: Circumference is calculated and returned.
circleType(double r = 0);
//Constructor with a default parameter.
//Radius is set according to the parameter.
//The default value of the radius is 0.0;
//Postcondition: radius = r;
//friend ostream &operator<<(ostream &output, const circleType &C);
//friend istream &operator>>(istream &input, const circleType &C);
friend ostream &operator<<(ostream &output, const circleType &C)
{
output << C.radius << endl;
return output;
}
friend istream &operator>>(istream &input, circleType &C)
{
cout << "Enter Radius : ";
input >> C.radius;
return input;
}
private:
double radius;
};
2) circleType.cpp
#include "circleType.h"
using namespace std;
void circleType::setRadius(double r)
{
if (r >= 0)
this->radius = r;
else
this->radius = 0;
}
double circleType::getRadius()
{
return this->radius;
}
double circleType::area()
{
return 3.1416 * (this->radius) * (this->radius);
}
double circleType::circumference()
{
return 2 * 3.1416 * (this->radius);
}
circleType::circleType(double r)
{
setRadius(r);
}
3) main.cpp
#include "circleType.h"
using namespace std;
int main()
{
circleType C;
cin >> C;
cout << C;
return 0;
}
Please refer below output for refernce
Enter Radius : 6.23
6.23
Process returned 0 (0x0) execution time : 3.727 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.