c++ problem 1) Create a class named Planets with a default constructor, a public
ID: 3579921 • Letter: C
Question
c++ problem
1) Create a class named Planets with a default constructor, a public method ReadFile, and a public data member distances, which is an array with 100 floating point values. Use -1.0 to initialize all the values of the array.
2) The public method ReadFile will read each line from the given filename and store each value into the array. For each line of the file, print the line to the console and place the value into the array. This method does not return a value.
You only need to write the class definition and any code that is required for that class. Place all code within the class definition. Our code will create and test your class.
Hint: remember that converting a string to a floating point number can be done by "atof(str.c_str())"
Explanation / Answer
Here is the class for you:
#include <iostream>
#include <fstream>
using namespace std;
class Planets
{
public:
//a public data member distances, which is an array with 100 floating point values.
float distances[100];
//default constructor
//Use -1.0 to initialize all the values of the array.
Planets()
{
for(int i = 0; i < 100; i++)
distances[i] = -1;
}
//The public method ReadFile will read each line from the given filename and store each value into the array.
//This method does not return a value.
void ReadFile(string fileName)
{
ifstream fin;
fin.open(fileName);
int count = 0;
//For each line of the file, print the line to the console and place the value into the array.
while(!fin.eof())
{
fin>>distances[count];
cout<<distances[count]<<endl;
count++;
}
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.