C++ make extensions to my code to do the following: Extend the driver class to do the following: Declare a vector of circles Call a function with signature inputData(vector &, string filename) that reads data from a file called dataLab4.txt into the vector. The following c-e are done in this function 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 using the toString method Use an iterator to iterate through the vector to display these circles Display the count of all the circles in the vector using the getCount method Display the count of all the circles in the vector using the vector size method Clear the vector Create a circle called c using the default constructor Display the current count of all the circles using the getCount method on c Display the current count of all the circles using the vector size method Write functions in your main driver cpp file that perform the actions b-l. Your code should be modular and your main program should consist primarily of function calls
C++
make extensions to my code to do the following:
- Extend the driver class to do the following:
- Declare a vector of circles
- Call a function with signature inputData(vector<Circle> &, string filename) that reads data from a file called dataLab4.txt into the vector. The following c-e are done in this function
- 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 using the toString method
- Use an iterator to iterate through the vector to display these circles
- Display the count of all the circles in the vector using the getCount method
- Display the count of all the circles in the vector using the vector size method
- Clear the vector
- Create a circle called c using the default constructor
- Display the current count of all the circles using the getCount method on c
- Display the current count of all the circles using the vector size method
- Write functions in your main driver cpp file that perform the actions b-l. Your code should be modular and your main program should consist primarily of function calls
My code:
Circle:
#include <iostream>
#include "CircleHeader.h"
#include <string.h>
#include <math.h>
using namespace std;
//default constructor
Circle::Circle()
{
x=0;
y=0;
radius=1;
}
//regular constructor
Circle::Circle(int x, int y, int radius)
{
this->x=x;
this->y=y;
this->radius=radius;
}
//getters
int Circle::getX()
{
return x;
}
int Circle::getY()
{
return y;
}
int Circle::getRadius()
{
return radius;
}
//setters
void Circle::setX(int newX)
{
x=newX;
}
void Circle::setY(int newY)
{
y=newY;
}
void Circle::setRadius(int newRadius)
{
radius=newRadius;
}
//def to find area
double Circle::getArea()
{
double area=M_PI*radius*radius;
return area;
}
//def to find Circumference
double Circle::getCircumference()
{
double Circumference=2*M_PI*radius;
return Circumference;
}
//def to return cirlce as a string
string Circle::toString()
{
string s;
s="("+to_string(getX())+","+to_string(getY())+"):"+to_string(getRadius());
return s;
}
double Circle::getDistance(Circle other)
{
return sqrt((x - other.getX())*(x - other.getX()) + (y - other.getY()) * (y - other.getY()));
}
void Circle::moveTo(int newX,int newY)
{
x += newX;
y += newY;
}
bool Circle::intersects(Circle other)
{
int radSumSq = (radius + other.getRadius()) * (radius + other.getRadius());
if (getDistance(other) >= radSumSq)
return true;
else
return false;
}
void Circle::resize(double scale)
{
radius = radius*scale;
}
Circle Circle::resize(int scale)
{
Circle c(x, y, (radius*scale));
return c;
}
CircleHeader:
#ifndef CircleHeader_H
#define CircleHeader_H
#include<string.h>
#include<iostream>
using namespace std;
//declaring class
class Circle
{
//private
int x;
int y;
int radius;
public:
//default constructor
Circle();
//p constructor
Circle(int X, int Y, int Radius);
//declaration of setters
int getX();
int getY();
int getRadius();
//declaration of getters
void setX(int newX);
void setY(int newY);
void setRadius(int newRadius);
//declaration of functions to find area and Circumference
double getArea();
double getCircumference();
string toString();
double getDistance(Circle other);
void moveTo(int newX,int newY);
bool intersects(Circle other);
void resize(double scale);
Circle resize(int scale);
};
#endif
Main CPP:
#include "CircleHeader.h"
#include <iostream>
using namespace std;
int main()
{
//first circle
Circle circleOne;
Circle circleTwo(0,0,4);
cout<<"Circle one is : "<<"("<<circleOne.toString()<<endl;
cout<<"Circle Two is : "<<"("<<circleTwo.toString()<<endl;
cout<<"Distance Between Circle one and Circle two: "<<circleOne.getDistance(circleTwo)<<endl;Circle circleThree = circleTwo.resize(10);
cout<<"circle Three is : "<<circleThree.toString()<<endl;
circleThree.moveTo(4,5);
cout<<"Circle Three is : "<<"("<<circleThree.toString()<<endl;
cout<<"circleTwo intersects circleThree: "<<circleTwo.intersects(circleThree)<<endl;
Circle circleFour(4,5,1);
cout<<"Circle Four is : "<<"("<<circleFour.toString()<<endl;
cout<<"circleFour intersects circleOne: "<<circleFour.intersects(circleOne)<<endl;
circleThree.resize(9.5);
cout<<"circle Three is : "<<circleThree.toString()<<endl;
//showing area of all circles
cout<<"Area of circle One is : "<<circleOne.getArea()<<endl;
cout<<"Area of circle Two is : "<<circleTwo.getArea()<<endl;
cout<<"Area of circle Three is : "<<circleThree.getArea()<<endl;
//showing Circumference of all circles
cout<<"Circumference of circle One is : "<<circleOne.getCircumference()<<endl;
cout<<"Circumference of circle Two is : "<<circleTwo.getCircumference()<<endl;
cout<<"Circumference of circle Three is : "<<circleThree.getCircumference()<<endl;
return 0;
}
Output must look like attached picture
Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include <cmath>
using namespace std;
class Circle{
private:
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
public:
Circle(); // default constructor that sets origin to (0,0) and radius to 1
Circle(int x, int y, int radius); // regular constructor
int getX();
int getY();
int getRadius();
void setX(int newX);
void setY(int newY);
void setRadius(int newRadius);
double getArea(); // returns the area
double getCircumference(); // returns the circumference
string toString(); // returns the circle as a string in the form (x,y): radius
double getDistance(Circle other); //returns the distance between the center of this circle and the other circle
void moveTo(int newX,int newY); // move the center of the circle to the new coordinates
bool intersects(Circle other); // returns true if the center of the other circle lies inside this circle else returns false
void resize(double scale); // multiply the radius by the scale
Circle resize(int scale); // returns a new Circle with the same center as this circle but radius multiplied by scale
int getCount(); //returns the number of circles created
};
#endif
Step by step
Solved in 4 steps with 1 images