Question
Extend the code from Lab5B. Use the same UML as below and make extensions as necessary
Extend the code from Lab5B. Use the same UML as below and make extensions as necessary
Circle |
-int x //x coord of the center -int y // y coord of the center -int radius -static int count // static variable to keep count of number of circles created |
+ Circle() //default constructor that sets origin to (0,0) and radius to 1 +Circle(int x, int y, int radius) // regular constructor +getX(): int +getY(): int +getRadius(): int +setX(int newX: void +setY(int newY): void +setRadius(int newRadius):void +getArea(): double // returns the area using formula pi*r^2 +getCircumference // returns the circumference using the formula 2*pi*r +toString(): String // return the circle as a string in the form (x,y) : radius +getDistance(Circle other): double // * returns the distance between the center of this circle and the other circle +moveTo(int newX,int newY):void // * move the center of the circle to the new coordinates +intersects(Circle other): bool //* returns true if the center of the other circle lies inside this circle else returns false +resize(double scale):void// * multiply the radius by the scale +resize(int scale):Circle // * returns a new Circle with the same center as this circle but radius multiplied by scale +getCount():int //returns the number of circles created //note that the resize function is an overloaded function. The definitions have different signatures
|
- Write a brand new driver program that does the following:
- Creates a circle object circleOne at (0,0):5.
- Creates a shared pointer circleOnePtr to circleOne
- Creates a circle object circleTwo at (-2,-2): 10
- Creates a shared pointer circleTwoPtr to circleTwo
- Use greaterThan to check if circleTwo is bigger than circleOne and display results // **** you will need to change the parameter to shared pointer
- Declare a vector of pointers to circles- circlePointerVector
- Call a function with signature inputData(circlePointerVector &, string filename) that reads data from a file called dataLab4.txt into the array The following 1-7 are done in this function
// **** note how the & operator is used in the parameter list
- Use istringstream to create an input string stream called instream. Initialize it with each string that is read from the data file using the getline method.
- Read the coordinates for the center and the radius from instream to create the circles
- Include a try catch statement to take care of the exception that would occur if there was a file open error. Display the message “File Open Error” and exit if the exception occurs
- Display all the circles in this vector of shared pointers using the toString method
- Display the sum of the areas of all the circles in the array
- Switch the position of the second and fourth circles by swapping pointers to the circles
- Display this changed sequence of circles using toString method
Note that the template code is given to you. You need to go through it and learn how to use shared pointers to objects, create vectors of shared pointers to objects and pass these as parameters.
You need to write one line of code. Declare circleTwoPtr. Watch for **** in comments.
Template:
//Circle.h
#include
#include
#include
using namespace std;
#ifndef CIRCLE_H// this is optional
#define CIRCLE_H // works with or without it
class Circle{
//private
static int count;
int radius;
int x,y;
public:
Circle();
Circle(int xcoord,int ycoord, int r);
int getY();
int getX();
int getRadius();
static int getCount();
double getArea();
double getCircumference();
double getDistance(Circle other);
bool intersects(Circle other);
Circle resize(int scale);//to copy self to other;
void resize(double scale);//for self
void setX(int xcoord);
void setY(int xcoord);
void setRadius(int r);
bool greaterThan(shared_ptr
string toString();
~Circle();
//regular functions
//void inputData(vector
};
#endif
//end of Circle.h
//Circle.cpp
//#include
//#include
#define _USE_MATH_DEFINES
#include "Circle.h"
#include
#include
Circle::Circle(){
//if you have regular constructor you must
//define default constructor
//C++ will not auto create for you
x=0;
y=0;
radius=1;
count++;
}
Circle::Circle(int xcoord,int ycoord, int r){
x=xcoord;
y=ycoord;
radius=r;
count++;
}
int Circle::getX(){
return x;
}
int Circle::getY(){
return y;
}
int Circle::getRadius(){
return radius;
}
int Circle::getCount(){
return count;
}
void Circle::setX(int xcoord){
x=xcoord;
}
void Circle::setY(int xcoord){
x=xcoord;
}
void Circle::setRadius(int r){
radius=r;
}
double Circle::getArea(){
return M_PI*pow(radius,2);
}
double Circle::getCircumference(){
return M_PI*radius/2;
}
double Circle::getDistance(Circle other){
return sqrt(pow(x-other.getX(),2)+pow((y-other.getY()),2));
}
bool Circle::intersects(Circle other){
return (this->getDistance(other)<=radius);
}
void Circle::resize(double scale){
radius = scale*radius;
}
Circle Circle::resize(int scale){
Circle c(x,y,radius*scale);
return c;
}
string Circle::toString(){
return "(" +to_string(x)+","+to_string(y)+"):"+to_string(radius);
}
// *****note the change to this parameter
// make suitable changes where need to other instance methods
bool Circle::greaterThan(shared_ptr
return radius>other->getRadius();
}
Circle::~Circle(){
//std::cout<<"Inside Destructor "<
//you must release/delete it here
}
int Circle::count=0;
//end of Circle.cpp
//main.cpp
#include
#include "Circle.h"
#include
#include
#include
#include
#include
#include
using namespace std;
const int SIZE = 10;
// pay attention to this parameter list
int inputData(vector
{
ifstream inputFile(filename);
istringstream instream;
string data;
int count =0;
int x,y,radius;
try{
if (inputFile){
while (!inputFile.eof() && count
istringstream instream(data);
instream>>x;
instream>>y;
instream>>radius;
//create a new Circle object and push it into
//the vector
shared_ptr
circlePointerArray.push_back(circle);
count++;
}
}
else throw string("File Not Found");
}
catch (string message){
cout<
}
return count;
}
int main(){
shared_ptr
// create a new shared pointer to a Circle Object
//*************** create another shared pointer called circleTwoPtr
//
if (circleOnePtr->greaterThan(circleTwoPtr)) // declare a vector of shared pointers to Circle objects //end of main.cpp
cout<<" Circle One is bigger "<
cout<<" Circle Two is bigger "<
std::vector
int count = inputData(circlePointerVector, "dataLab4.txt");
cout<<"The total number of circles is "<
for (int i=0;i
cout<<"The total sum of the areas is "<
circlePointerVector[1]=circlePointerVector[3];
cout<<"The modified array is "<
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started