exercise about classes

I'm going through an exercise as described below and I've included my code so far. The green comments in the code are given as a skeleton of the code that I'm supposed to write. Am I on the right track so far? I am struggling with the default/parameterized constructors - not exactly sure if I've done the right thing so far and what else the exercise is requiring me to do. Thanks.

Write a C++ class for describing galaxies
A galaxy object should contain the following (private) data
-Hubble type: string e.g. E0, E7, S0, Sa, Sc, Irr
-Redshift: double z in range [0,10]
-Total mass: double Mtot in range [1e7,1e12] M
-A stellar mass fraction: double f = M=Mtot in range [0,0.05]
The class should also contain several member functions
The main program should demonstrate use of the class through declaring and using objects.

Core marks (3 marks):
Your class should include
A default constructor; a parameterized constructor; a destructor; and a member function, de ned outside class, to print out an object's data (total 1 mark)
A member function to change the galaxy's Hubble type (0.5 marks)
A member function to return the stellar mass M = fMtot (0.5 marks)
Your main program should demonstrate use of all of the above. It should use a vector to store at least 2 objects and use iterator (1 mark)

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
// PHYS 30762 Programming in C++
// Assignment 4

// Simple demonstration of a C++ class

// Hubble types: E[0-7], S0, S[a-c], SB[a-c], Irr
// Redshift z in range [0,10]
// Total mass M_tot in range [1e7,1e12] M_sun
// Stellar mass fraction f_* in range [0,0.05]

#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;

class Galaxy
	{
	private:
		string HubbleType;
		double RedShift,TotalMass,StellarMassFraction;
		
	public:
		// Default constructor
		Galaxy() {HubbleType="Ghost";RedShift=0;TotalMass=0;StellarMassFraction=0;}
		// Parameterized constructor
		Galaxy(string &gHubbleType,double gRedShift,double gTotalMass,double gStellarMassFraction)
		{
			HubbleType=gHubbleType;
			RedShift=gRedShift;
			TotalMass=gTotalMass;
			StellarMassFraction=gStellarMassFraction;
		}
		// Destructor
		
		
		// Return stellar mass (M_* = f_* x M_tot)
		double StellarMass() {return StellarMassFraction*TotalMass;}
		// Change galaxy's Hubble type
		void ChangeType(string NewType) { }
		// Prototype for function to print out an object's data
		void PrintData();
		// Add satellite galaxy
		
	};
// Print out an object's data
void Galaxy::PrintData()
{
	cout<<"Galaxy is of Hubble type "<<HubbleType<<endl;
	cout<<"Red shift = "<<RedShift<<endl;
	cout<<"Total mass = "<<TotalMass<<endl;
	cout<<"Stellar mass fraction = "<<StellarMassFraction<<endl; 
}

// End of class and associated member functions

// Main program
int main()
{
	
	// Example using default constructor
	string HubbleType("E0");
	Galaxy g1(HubbleType,1,2,3); 
	g1.PrintData();
	double g1StellarMass=g1.StellarMass();
	cout<<"Stellar mass = "<<g1StellarMass<<endl;
	
	// Example using parameterised constructor
	
	// print out data

	
	// Get and print out stellar mass
	
	// Change Hubble type from Irr to S0
	
	// Add satellite galaxies
	getc(stdin);

	return 0;
}
You are on the right track...

Classes are private by default so declaring private data first is redundant. Most programmers declare their public interface first and declare their private data last.

I noticed you have a destructor comment. Destructors are used to de-allocate variables that used a new operator or to do some other cleanup so I don't think you will need to explicitly declare one in this case.

So with the "// Example using parameterised constructor" comment, have I actually done that in my code already? In other words, what am I missing right now?
Last edited on
closed account (o3hC5Di1)
Hi there,

You can't really explicitly call the constructor of a class, it is called automatically when you create an object of your class.

By doing Galaxy g1(HubbleType,1,2,3); the parameterised constructor is called.

In order to demonstrate to default constructor you would have to declare another object as such:

Galaxy g2;

Note that this doesn't take any parameters, does the default constructor will be called and will fill in the values as you have specified.

Let us know if you require any further help.

All the best,
NwN
Since the assignment says to include a destructor, then I would declare it:
~Galaxy();
As IceThatJaw points out, there is no cleanup that it needs to do, so the body is empty.

Not sute what "de ned" was supposed to be.

Your examples using the default constructor is mislabeled. What is labeled default constructor is actually using the parameterized constructor.

There is no implementation of ChangeType.

There is no use of a vector per the instructions.
Thank you for the replies. So once I've declared Galaxy g2, how do I use it? g2 doesn't have data yet, does it?

Oh "de ned" was meant to be "declared" - sorry about that.

In my code, I've followed an example given with the assignment and declared the following:

1
2
3
4
5
              
HubbleType=gHubbleType;
RedShift=gRedShift;
TotalMass=gTotalMass;
StellarMassFraction=gStellarMassFraction;


What is the purpose of redefining HubbleType, RedShift,TotalMass, and StellarMassFraction to those variables with the "g" in front?

Thanks again.
closed account (o3hC5Di1)
Hi there,

When you declare Galaxy g2; the compiler sees that you are not using parameters and so it will use your default constructor:

1
2
// Default constructor
Galaxy() {HubbleType="Ghost";RedShift=0;TotalMass=0;StellarMassFraction=0;}


As you can see the default constructor sets basic values for all your class variables now, because you have not passed any parameters yourself.

As for the parameterised constructor (as for g1) which is declared as such:

Galaxy(string &gHubbleType,double gRedShift,double gTotalMass,double gStellarMassFraction)

Notice the "g-variable" names in the parameters.
What the other code:

1
2
3
4
HubbleType=gHubbleType;
RedShift=gRedShift;
TotalMass=gTotalMass;
StellarMassFraction=gStellarMassFraction;


does, is say "assign the value of the parameter to the internal class variable". So in other words, you are bringin data from outside the object, into the object now.

Hope that's making sense to you?

All the best,
NwN
Topic archived. No new replies allowed.