I want to put a c++ function into age, injury type, and severity of injury. basically 3 categories. but I'm not sure how to get started, any suggestions would be helpful .
class Crash
{
int age;
string injuryType;
int severity;
Crash( int a = 21, string i = "fatal", int s = 10 ) : age( a ), injuryType( i ), severity( s ) {}
};
You have to explain more. You are not familiar with C++ and therefore you express your ideas with terms that we are not familiar with.
@EtherVae: "leave null"?
1 2 3 4 5 6 7 8
int main() {
Crash foo( 7, "boo", 42 ); // nearly obvious
// Calls Crash(int,string,int) with {7,"boo",42}
Crash bar( 6, "yay" ); // there is no Crash(int,const char*) or Crash(int,string)
// Calls Crash(int,string,int) with {6,"yay",42}
// the declared default is used for the third parameter
}
The default values achieve the same as this:
1 2 3 4 5 6 7 8 9 10 11
class Crash
{
int age;
string injuryType;
int severity;
Crash( int a, string i, int s ) : age( a ), injuryType( i ), severity( s ) {}
Crash( int a, string i ) : age( a ), injuryType( i ), severity( 10 ) {}
Crash( int a ) : age( a ), injuryType( "fatal" ), severity( 10 ) {}
Crash( ) : age( 21 ), injuryType( "fatal" ), severity( 10 ) {}
};
Or, in C++11:
1 2 3 4 5 6 7 8 9 10 11
class Crash
{
int age = 21;
string injuryType = "fatal";
int severity = 10;
Crash( int a, string i, int s ) : age( a ), injuryType( i ), severity( s ) {}
Crash( int a, string i ) : age( a ), injuryType( i ) {}
Crash( int a ) : age( a ) {}
Crash( ) {}
};
Thanks Keskiverto Would the default values by the constructor take up a position if the struct is assigned to a vector or just be used if the member within the struct vector is not defined?
struct Crash
{
int age = 21;
string injuryType = "fatal";
int severity = 10;
Crash( int a, string i, int s ) : age( a ), injuryType( i ), severity( s ) {}
Crash( int a, string i ) : age( a ), injuryType( i ) {}
Crash( int a ) : age( a ) {}
Crash( ) {}
}; vector<Crash> crashes(0)
int main()
{
Crash tCrash;
tCrash.age = 15;
tCrash.injuryType = "Minor";
tCrash.severity = 1;
crashes.push_back(tCrash);
std::cout << crashes[0].age << std::endl;
std::cout << crashes[0].injuryType << std::endl;
std::cout << crashes[0].severity << std::endl;
}
I think that you (and I) need to make the access to that constructor public:
As it stands you can't set the data members like that because they are private (by default).
However, if you made them public: then setting them individually like this would override the default values, so you would get 15, "minor", 1.
BTW, my original reply was actually meant as light-hearted and tongue-in-cheek because I didn't really follow what you were asking. I'm slightly horrified that it seems to be going forward ... !
Do constructors not place items into the vector by setting them "default" values? Does push_back overwrite them instead of adding to the end of the vector?
Not sure what you are saying. In your last code you created a Crash on line 15. This would have held the default values. Then you changed these to 15, "minor", 1. Then you pushed back the Crash with these changed values into the vector, giving it element[0]. No "overwriting" when putting in the vector.
If you correct the bugs in your program (missing headers and a semicolon) then it will give 15, "minor", 1.
Does push_back not remove dead space and then add the new data after existing data instead of replacing it as would be achieved by hardsetting the vector parameter? crashes (on line 11) is a vector while tCrashes (line 15) is just the base struct to take member data for cyclical input. When push_back is used on tCrash does it not add the member data to the next available vector position, where the default takes up crashes[0] and the values from tCrash then take crashes[1]?
I've no idea what you are thinking or saying, I'm afraid.
Your vector was empty to start with - you declared it with 0 elements - and you just pushed a Crash with data { 15, "minor", 1 } into it ... so that's exactly what it has.