C++ Stars This lab exercise will practice creating objects with constructors and destructors and demonstrate when constructors and destructors are called. Star Class Create a class, Star. A Star object has two member variables: its name, and a solar radius. This class should have a constructor which takes a std::string, the name of the star, and a double, the solar radius of the star. In the constructor, the Star class should print to the terminal that the star was born. For example, if you create a Star as follows: Star my_star("Saiph", 22.2); Then the constructor should print: The star Saiph was born. In the destructor, the Star class should print to the terminal that the star was destroyed, along with the number of times the volume of the sun that that star was, formatted to two decimal places. Hint: use the following line to set the precision to 2 decimal places: std::cout << std::fixed << std::setprecision(2); For example, when my_star above has its destructor called, we would see something like: The star Saiph has gone supernova. It was 492.84 times the volume of our sun. The Star class will need member variables to store the information passed in the constructor. Additionally, the Star class needs a getter, GetName(), to return the name string. Calculating volume A solar radius is the number of times bigger a star's radius is than our sun's radius. You can use it to determine the relative volume between a star and our sun: The equation for volume of a sphere is 4 / 3 * pi * r ^ 2. Since 4 / 3 * pi is constant, and the solar radius r of the sun is by definition 1, you need only calculate the solar radius of your star squared. So if the solar radius of the star is 2, the star is 2 * 2 = 4 times larger than the sun by volume. Main program Your main program should ask the user for how many stars they want to create, and then for each of those stars it should ask for the star name and solar radius to create a Star object. After creation, your main program should print "Created star " for the user's given star name. For example: How many stars? 2 Enter star 0's name: Mintaka What is the solar radius? 16.5 The star Mintaka was born Created star Mintaka The star Mintaka has gone supernova. It was 272.25 times the volume of our sun. Enter star 1's name: Alnilam What is the solar radius? 32.4 The star Alnilam was born Created star Alnilam The star Alnilam has gone supernova. It was 1049.76 times the volume of our sun. Note: Because your Star objects are created within a loop, their destructors are automatically called at the end of each pass through the loop! main.cc file #include #include #include "star.h" int main() { int num = 0; std::cout << "How many stars? "; std::cin >> num; // =================== YOUR CODE HERE =================== // Write a loop to allow the user to create `num` stars. // Each iteration of the loop should ask the user for the // star's name and solar radius, and use that to construct // a `Star` object. // // After you create the `Star`, print "Created star " with // its name, to announce the birth of a new star. // // Notice that we don't need to explicitly call the // destructor since it automatically gets called when the // Star goes out of scope, at the end of each iteration. // ======================================================= return 0; } star.h file #include #include #include class Star { public: // ====================== YOUR CODE HERE ====================== // Define the following members for the Star class: // // 1. One member variable representing the name of the star, // and one representing the solar radius of the star. // 2. A constructor, which initializes the name of the star, // and the radius of the star. The constructor should // print that the star was born to the terminal. // 3. A destructor that prints to the terminal that the star // was destroyed (i.e. went supernova), and print how many // times the volume of the sun that the star was. // 4. An accessor function, `GetName` for the star's name. // ============================================================ }; star.cc file #include "star.h" #include #include #include // ========================= YOUR CODE HERE ========================= // This implementation file (star.cc) is where you should implement // the member functions declared in the header (star.h), only // if you didn't implement them inline within star.h. // // Remember to specify the name of the class with :: in this format: // MyClassName::MyFunction() { // ... // } // to tell the compiler that each function belongs to the Star class. // ===================================================================
C++
Stars
This lab exercise will practice creating objects with constructors and destructors and demonstrate when constructors and destructors are called.
Star Class
Create a class, Star. A Star object has two member variables: its name, and a solar radius. This class should have a constructor which takes a std::string, the name of the star, and a double, the solar radius of the star.
In the constructor, the Star class should print to the terminal that the star was born. For example, if you create a Star as follows:
Then the constructor should print:
In the destructor, the Star class should print to the terminal that the star was destroyed, along with the number of times the volume of the sun that that star was, formatted to two decimal places. Hint: use the following line to set the precision to 2 decimal places:
For example, when my_star above has its destructor called, we would see something like:
The Star class will need member variables to store the information passed in the constructor.
Additionally, the Star class needs a getter, GetName(), to return the name string.
Calculating volume
A solar radius is the number of times bigger a star's radius is than our sun's radius. You can use it to determine the relative volume between a star and our sun:
The equation for volume of a sphere is 4 / 3 * pi * r ^ 2.
Since 4 / 3 * pi is constant, and the solar radius r of the sun is by definition 1, you need only calculate the solar radius of your star squared.
So if the solar radius of the star is 2, the star is 2 * 2 = 4 times larger than the sun by volume.
Main program
Your main program should ask the user for how many stars they want to create, and then for each of those stars it should ask for the star name and solar radius to create a Star object. After creation, your main program should print "Created star " for the user's given star name.
For example:
Note: Because your Star objects are created within a loop, their destructors are automatically called at the end of each pass through the loop!
main.cc file
#include <iostream>
#include <string>
#include "star.h"
int main() {
int num = 0;
std::cout << "How many stars? ";
std::cin >> num;
// =================== YOUR CODE HERE ===================
// Write a loop to allow the user to create `num` stars.
// Each iteration of the loop should ask the user for the
// star's name and solar radius, and use that to construct
// a `Star` object.
//
// After you create the `Star`, print "Created star " with
// its name, to announce the birth of a new star.
//
// Notice that we don't need to explicitly call the
// destructor since it automatically gets called when the
// Star goes out of scope, at the end of each iteration.
// =======================================================
return 0;
}
star.h file
#include <iomanip>
#include <iostream>
#include <string>
class Star {
public:
// ====================== YOUR CODE HERE ======================
// Define the following members for the Star class:
//
// 1. One member variable representing the name of the star,
// and one representing the solar radius of the star.
// 2. A constructor, which initializes the name of the star,
// and the radius of the star. The constructor should
// print that the star was born to the terminal.
// 3. A destructor that prints to the terminal that the star
// was destroyed (i.e. went supernova), and print how many
// times the volume of the sun that the star was.
// 4. An accessor function, `GetName` for the star's name.
// ============================================================
};
star.cc file
#include "star.h"
#include <iomanip>
#include <iostream>
#include <string>
// ========================= YOUR CODE HERE =========================
// This implementation file (star.cc) is where you should implement
// the member functions declared in the header (star.h), only
// if you didn't implement them inline within star.h.
//
// Remember to specify the name of the class with :: in this format:
// <return type> MyClassName::MyFunction() {
// ...
// }
// to tell the compiler that each function belongs to the Star class.
// ===================================================================
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images