How to define a class?

problem description:

Write a program that defines a class called Rocket that represents a rocket. The class should contain the rocket's mass (in grams), the mass of engine (grams), the mass of the propellant (grams), the average thrust(newtons), and the thrust duration (seconds). The variables must be made private. The class should have a member function that prints out the class data. The class should also define two member functions that calculate and return the max altitude and max speed of the rocket. The calculation function should NOT print anything. Assume that the flight is straight up, no atmospheric drag, and the mass during powered flight is an average value. The program should read in the data and create and object of class Rocket to store the data. The program should the call the member functions and print out the results of the calculations.

Example output:

enter the mass of the rocket: 23
enter the mass of engine: 16.2
enter the mass of propellant; 3.12
enter the average thrust of the engine: 8
enter the burn duration of the engine: .5

The rockets max velocity is 101.37
The rockets max altitude is 549.621

So far I:
-made the class called Rocket
-I made the variables private
-have the two functions that calculate the max altitude and max velocity.

I'm really confused on
-how to create a member function that prints the class data.
-how to write the member functions
-how to access the data members
-how to ask for the information of the rocket

besides that I put in question marks in areas where i need help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  

#include <iostream>
#include <cstdlib>
using namespace std;

class Rocket{

public:

	// default constructure
	Rocket() {}

	// prototype of constructor for class Rocket
	// initialized data members 
	Rocket(double, double, double, double, double);

	double velocity();// ???
	double altitude();// ????

private:
		double rocketmass;   // mass of rocket in grams
		double enginemass;   // mass of engine in grams
		double propemass;    // mass of propellant in grams 
		double avgthrust;    // average thrust in newtons
		double thrustdura;   // thrust duration in seconds

}

Rocket::Rocket(double rmass, double emass, double pmass, double avgth, double thdur)
{
	rocketmass = rmass;
	enginemass = emass;
	propemass = pmass;
	avgthrust = avgth;
	thrustdura = thdur;
}

// calculates the max velocity of the rocket
double Rocket :: velocity()
{
	double avgmass,       // average mass
		   acceleration,  // acceleration of rocket
		   maxvelocity;   // max velocity of rocket

	// calculates the average mass
	avgmass = ((rocketmass + enginemass + rocketmass + enginemass - propemass)/ 2)/ 1000;

	// calculates the acceleration of rocket
	acceleration = (avgthrust / avgmass) - 9.8;

	// calculates the max velocity
	maxvelocity = acceleration * thrustdura;

	return(maxvelocity);
}


double Rocket :: altitude()
{
	double height1,
		   height2,
		   maxaltitude,
		   avgmass,
		   acceleration;

	// calculates the average mass
	avgmass = ((rocketmass + enginemass + rocketmass + enginemass - propemass)/ 2)/ 1000;

	// calculates the acceleration of rocket
	acceleration = (avgthrust / avgmass) - 9.8;

	// calculates the altitude of power flight 
	height1 = (velocity() * velocity()) / (2 * 9.8);

	// calculates the altitude of non-powered flight
	height2 = 0.5 * acceleration * thrustdura * thrustdura;

	// calculates the maximum altitude of rocket
	maxaltitude = height1 + height2;

	return(maxaltitude);

}

// Main Program
int main( )
{
	Rocket rocketa; // ??????


		// ask user to enter mass of rocket in grams
		cout << "Enter the mass of rocket: ";
		cin >> ????;

		// ask user to enter mass of engine of rocket in grams
		cout << "Enter the mass of the engine: ";
		cin >> ???;

		// ask user to enter mass of propellant of rocket in grams
		cout << "Enter the mass of the propellant: ";
		cin >> ???;

		// ask user to enter average thrust of rocket in newtons
		cout << "Enter the average thrust of the engine: ";
		cin >> ???;

		// aks user to enter thrust duration of rocket in seconds
		cout << "Enter the burn duration of the engine: ";
		cin >> ???;


		cout << "The rockets maximum velocity is " << ??? << endl;
		cout << "The rockets maximum altitude is " << ??? << endl;


	return 0;
} 
declare rocketa later...

do your cin's into local variables
then create rocketa when you have the data from the user.
1
2
3
Rocket rocketa(rmass, emass, pmass, avgth, thdur);
cout rocketa.altitude();
cout rocketa.velocity();
followed your advice and was able to complete an run program. Thank You!
you're welcome.
Topic archived. No new replies allowed.