help

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 .
1
2
3
4
5
6
7
8
class Crash
{
   int age;
   string injuryType;
   int severity;

   Crash( int a = 21, string i = "fatal", int s = 10 ) : age( a ), injuryType( i ), severity( s ) {}
};
What is meant by

 
Crash( int a = 21, string i = "fatal", int s = 10 ) : age( a ), injuryType( i ), severity( s ) {}
That is the constructor for the Crash class.

It takes three arguments, and initialises the class's data members to the values of those arguments.

The three arguments have default values, so that it is possible to invoke the constructor with fewer than three arguments.
Last edited on
Ok thanks for explaining, is that preferable to leaving them null or just contextually advisable so there is a default field?
I want to put a function 3 categories.

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?

Would the following code;
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
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;
}

output

15
minor
1

or

21
fatal
10
Last edited on
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 ... !
Last edited on
Thanks lastchance me too! It's quite horrifying!

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 understand if I said
 
crashes[0].blahMember = blahData;

then it would replace the data of blahMember at 0

but if I say
1
2
3
4
5
Crash tCrashes;

tCrashes.blahMember = blahData;

crashes.push_back(tCrashes);

do they both replace the default data?
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.
Last edited on
As long as you're afraid, that's what's important. So the constructor default values do not take a position within the vector. Thanks!
Documentation: http://www.cplusplus.com/reference/vector/vector/push_back/
Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element.

That means:
The new element is constructed and initialized with copy constructor (or move constructor).

1
2
3
4
5
Crash tCrash( 15, "minor", 1 );
Crash foo( tCrash ); // copy construction

std::vector<Crash> bar( 4 ); // 4 default constructed elements
bar.push_back( tCrash ); // 5th element is copy constructed, just like foo 


The compiler does automatically create a copy constructor for you:
Crash( const Crash& other )
(There are ways to prevent that.)
Topic archived. No new replies allowed.