Related questions
m6 lb
Dont copy previous answers they are incorrect. Please provide a typed code and not pictures. The template was given below the code. we are just making changes to main cpp. We are writing one line of code based on the comments.
C++
use shared pointers to objects, create
You need to write one line of code. Declare circleTwoPtr. Watch for **** in comments.
Template code is given make changes and add stuff based on comments:
Template given:
main.cpp:
#include<iostream>
#include "Circle.h"
#include <fstream>
#include <vector>
#include <memory>
#include<cstdlib>
#include <fstream>
#include <sstream>
using namespace std;
const int SIZE = 10;
// pay attention to this parameter list
int inputData(vector<shared_ptr<Circle>> &circlePointerArray, string filename)
{
ifstream inputFile(filename);
istringstream instream;
string data;
int count =0;
int x,y,radius;
try{
if (inputFile){
while (!inputFile.eof() && count <SIZE){
getline(inputFile,data);
istringstream instream(data);
instream>>x;
instream>>y;
instream>>radius;
//create a new Circle object and push it into
//the vector
shared_ptr<Circle> circle = make_shared<Circle>(x,y,radius);
circlePointerArray.push_back(circle);
count++;
}
}
else throw string("File Not Found");
}
catch (string message){
cout<<message<<endl;
exit(0);
}
return count;
}
int main(){
shared_ptr<Circle> circleOnePtr = make_shared<Circle>(0,0,5);
// create a new shared pointer to a Circle Object
//*************** create another shared pointer called circleTwoPtr
//
if (circleOnePtr->greaterThan(circleTwoPtr))
cout<<" Circle One is bigger "<<endl;
else
cout<<" Circle Two is bigger "<<endl;
// declare a vector of shared pointers to Circle objects
std::vector<shared_ptr<Circle>>circlePointerVector;
int count = inputData(circlePointerVector, "dataLab4.txt");
cout<<"The total number of circles is "<<count<<endl;
cout<<"They are :"<<endl;
for (int i=0;i<count;i++)
cout<<circlePointerVector[i]->toString()<<endl;
double sumOfAreas=0;
for (int i=0;i<count;i++)
sumOfAreas+=circlePointerVector[i]->getArea();
cout<<"The total sum of the areas is "<<sumOfAreas<<endl;
shared_ptr <Circle>tmpPtr=make_shared<Circle>();
circlePointerVector[1]=circlePointerVector[3];
cout<<"The modified array is "<<endl;
for (int i=0;i<count;i++)
cout<<circlePointerVector[i]->toString()<<endl;
return 0;
}
Circle.cpp:
//#include<string>
//#include <cstdlib>// no need for this as is in header
#include "Circle.h"
#include <cmath>
#include <iostream>
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<Circle> other){
return radius>other->getRadius();
}
Circle::~Circle(){
//std::cout<<"Inside Destructor "<<endl;
//if you have dynamically allocated memory
//you must release/delete it here
}
int Circle::count=0;
Circle.h:
#include<string>
#include <vector>
#include <memory>
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<Circle> other);
string toString();
~Circle();
//regular functions
//void inputData(vector<Circle>);
};
#endif
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- In C programming language. Objectives:• Use of functions and passing by value and reference.• Use of selection constructs.• Use of repetition constructs (loops).• The use of the random number generator and seeding.• Display formatted output using input/outputstatements.• Use format control strings to format text output. Description: For this project you are tasked with building a user application that will select sets of random numbers. Your application must use functions and pass values and pointers. Your program will pick sets of 6 random numbers with values of 1 to 53. The user should be able to choose how many sets to produce. The challenge will be to ensure that no number in a set of 6 numbers is a repeat. The number can be in another set just not a duplicate in the same set of 6. Your program should prompt the user and ask them how many number sets they wish to have generated. The program should not exit unless the user enters a value indicating they wish to quit. Minimum...arrow_forwardHello, can anyone help me with this program please? I need it in C++. I already have a bit of code that was given to me but I'm not sure how to go forward with it. Please help me. Modify the vehicle management program to allow an automobile rental company to manage its fleet of automobiles. First, define a class called CityCar that contains an array of pointers to the 100 objects in the Car class. This also allows you to store pointers to objects of the derived class types PassCar and Truck. The objects themselves will be created dynamically at runtime. Define a class CityCar with an array of pointers to the Car class and an int variable for the current number of elements in the array. The constructor will set the current number of array elements to 0. The destructor must release memory allocated dynamically for the remaining objects. Make sure that you use a virtual destructor definition in the base class Car to allow correct releasing of memory for trucks and passenger vehicles....arrow_forwardAssignment-3 With the help of a C program show how to typecast a void * to an int * . Attach the code and output and upload the file on classroom.arrow_forward
- Write a C++ program with two user defined functions. The first function named "functionWithArray" takes two user input arguments of character types and return True if first argument is smaller than the second argument (alphabetically) and returns False otherwise. Write the second function named "functionWithPointers" which behaves like the first functions but uses pointers to receive the arguments. You may assume that both character arrays contain only lower-case letters, and no blanks or other non- alphabetic characters. Write a suitable Main function to test these two functions. Sample Output: Enter First String: C++Programming Enter Second String: JavaProgramming According to functionWithArrays: 'c++programming' is smaller than 'javaprogramming'. According to functionWithPointers: 'c++programming' is smaller than 'javaprogramming'.arrow_forwardHelp me homework c++ The shots file holds a list of shots (imagine some hitscan weapon in a video game like a shotgun or something). Each shot has an origin and a direction. The origin is an (x,y) coordinate, like (5,3). The direction is a slope and whether the shot is traveling along that slope or in the reverse. A slope of "Vertical" means that the shot is travellingstraight up and down. The format is:x_location y_location slope(either a number like 2.1 or a non-number meaning"Vertical") forwards(1 meaning forwards, 0 meaning backwards) 0 0 0 00 0 0 10 0 Vertical 00 0 Squirrel 110 10 -1 1-10.1 -100.01 2.1 0 The first line is a horizontal line shooting left from the origin (0,0).The second line is a shot also travelling horizontally from the origin, but forward along the x axis instead of backwards.The third line is shooting straight down out of the originThe fourth line is shooting straight up out of the origin (any non-number means Vertical, not just Vertical)The fifth line is...arrow_forwardThis assignment will give you a chance to perform some simple tasks with pointers. The tasks are only loosely related to each other. Start the assignment by copying the code below and pasting it into a .cpp file, then add statements to accomplish each of the tasks listed. Don't delete any of the given code or comments. Some of the tasks will only require a single C++ statement, others will require more than one. No documentation is required for this part of the assignment. /* Type your code after each of the commented instructions below (except that the statements for instructions 11 and 21 should be written where the instructions indicate). I have written the first statement for you. */ #include <iostream> using namespace std; int main() { // 1. Create two integer variables named x and y. int x; // 2. Create an int pointer named p1. // 3. Store the address of x in p1. // 4. Use only p1 (not x) to set the value of x to 99. // 5. Using cout and x (not p1), display the value of x....arrow_forward
- It’s due in C++ Please tell me which code is for StudentMaim.c and which for studentInfo.h and studentInfo.carrow_forwardGive an example of a function with two arguments and statements that are executed when the function is called. -Provide details about the return statement and the purpose of your function. -Provide an example of an array and a vector. -Describe your experience using GitHub, GitLab, Git, or other versional control software in general. Explain what aspects of the GitHub process you are comfortable with and what areas you need more practice or help.arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education