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, dened 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;
}
|