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. // ===================================================================

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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 <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.
// ===================================================================

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
JQuery and Javascript
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education