In: Computer Science
In C++
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 |
Create "Circle.h"
Create "Circle.cpp"
Complete Main.cpp
#include<iostream>
#include "Circle.h"
#include <fstream>
#include <vector>
#include<cstdlib>
#include <fstream>
#include <sstream>
void inputData(vector<Circle> &circleVector, string
filename){
//create the input string stream called instream
//open file
//if there is a problem opening the file, throw an exception and
exit - use a try catch statement
//otherwise
//use getline to read data line feed to the instream
//create a circle using the data in the instream
//add the circles to the vector
//keep doing this until all the data in the file is read
}
int main(){
cout<<"The number of circles, using getCount method is
"<<//<<endl;
cout<<"The numher of circles, using vetor size method is
"<<//<<endl;
//clear vector
cout<<"The number of circles, using getCount method is
"<<//<endl;
cout<<"The number of circles remaining is ";
return 0;
}
file "dataLab4.txt" contains:
0 0 4
0 0 6
-2 -9 6
4 5 7
7 8 9
// Circle.h
#ifndef CIRCLE_H_
#define CIRCLE_H_
#include <string>
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 using formula pi*r^2
double getCircumference(); // returns the circumference using the formula 2*pi*r
string toString(); // return 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
static int getCount(); //returns the number of circles created
};
#endif /* CIRCLE_H_ */
//end of Circle.h
// Circle.cpp
#include "Circle.h"
#include <cmath>
# define M_PI 3.14159265358979323846 // cmath doesn't contain the macro M_PI
int Circle::count = 0;
Circle::Circle()
{
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;
}
void Circle::setX(int newX)
{
x = newX;
}
void Circle::setY(int newY)
{
y = newY;
}
void Circle::setRadius(int newRadius)
{
radius = newRadius;
}
double Circle::getArea()
{
return M_PI * (radius * radius);
}
double Circle:: getCircumference()
{
return (2*M_PI) * (radius);
}
string Circle:: toString()
{
string str_x = to_string(x);
string str_y = to_string(y);
string str_radius = to_string(radius);
return "("+ str_x + "," + str_y + "):" + str_radius;
}
double Circle:: getDistance(Circle other)
{
return sqrt(pow(x-other.getX(),2)+ pow(y-other.getY(),2) );
}
void Circle::moveTo (int newX, int newY)
{
x = newX;
y = newY;
}
// if distance between the circles <= radius, then center of the other circle lies inside this circle
bool Circle:: intersects(Circle other)
{
double distance = getDistance(other);
return(distance <= radius);
}
void Circle:: resize(double scale)
{
radius *= scale;
}
Circle Circle:: resize(int scale)
{
return Circle(x,y,radius*scale);
}
int Circle::getCount()
{
return count;
}
//end of Circle.cpp
// Main.cpp
#include <iostream>
#include "Circle.h"
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
// function declaration
void inputData(vector<Circle> &circleVector, string filename);
int main()
{
vector<Circle> circles;
inputData(circles,"dataLab4.txt");
// Use an iterator to iterate through the vector to display these circles
vector<Circle>::iterator itr = circles.begin();
for(;itr != circles.end();itr++)
cout<<(*itr).toString()<<endl;
cout<<"The number of circles,using getCount method is "<<Circle::getCount()<<endl;
cout<<"The number of circles, using vector size method is "<<circles.size()<<endl;
// clear the vector
circles.clear();
cout<<"The number of circles,using getCount method is "<<Circle::getCount()<<endl;
cout<<"The number of circles, remaining "<<circles.size()<<endl;
return 0;
}
// function to read Circles from file and insert into vector
void inputData(vector<Circle> &circleVector, string filename)
{
ifstream fin;
fin.exceptions(ifstream::failbit | ifstream::badbit);
try{
fin.open(filename.c_str());
string line;
int x,y,radius;
// loop till the end of file
while(!fin.eof())
{
getline(fin,line); // read a line from file
istringstream ss(line);
ss>>x>>y>>radius;
circleVector.push_back(Circle(x,y,radius));
}
fin.close();
}catch(ifstream::failure &e)
{
cout<<"File Open Error"<<endl;
exit(1);
}
}
//end of Main.cpp
Output:
Input file:
Output: