Default string value

Feb 26, 2014 at 6:12pm
Hi everyone,

I am wanting for my object to have a default string value when it is created. In this example, when I create a new Vehicle object, I would like the name variable to be set to "CAR" once it is created.


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
//Vehicle.h

#ifndef __VEHICLE_INCLUDED__
#define __VEHICLE_INCLUDED__

#include <iostream>
#include <string>

using namespace std;

class Vehicle
{
        int numWheels;
        int numDoors;
        std::string name;

        public:

        Vehicle(int wheels=4, int doors=4, string newName ="CAR");

        void setNumWheels(int wheels);
        void setNumDoors(int doors);
        void setName(const std::string &newName);


        int getNumWheels();
        int getNumDoors();
        std::string getName();
        void printNumWheels();
};

#endif 


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
//Vehicle.cpp
#include <iostream>
#include "Vehicle.h"
#include <string>

using namespace std;


Vehicle::Vehicle(int wheels, int doors, string newName)
{
        numWheels = wheels;
        numDoors = doors;
        name = newName;
}

// Setters
void Vehicle::setNumWheels(int wheels) { numWheels = wheels; }
void Vehicle::setNumDoors( int doors)  { numDoors  = doors;  }

// Getters
int Vehicle::getNumWheels() { return numWheels; }
int Vehicle::getNumDoors()  { return numDoors ; }
string Vehicle::getName() { return name; }

void Vehicle::setName(const std::string &newName)
{
        name = newName;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//vehicle-test.cpp
#include <iostream>
#include "Vehicle.h"

using namespace std;

int main()
{
        // Creat an array of vehicles
        Vehicle garage[2];

        garage[0].setName("Jaguar\0");
        garage[1].setName("Audi");

        cout << garage[0].getName() << " has " << garage[0].getNumDoors() << " doors" << endl;
        cout << garage[1].getName() << endl;
        cout << garage[2].getName() << endl;

        return 0;
}


Program output:
1
2
3
Jaguar has 4 doors
Audi


I was expecting the last line of the program output to read "CAR", but it appears that the default value is not being done.

Many thanks
Feb 26, 2014 at 6:25pm
You are setting the default name correctly.

Your problem is something else:

1
2
3
4
5
6
7
8
9
10
11
        // Creat an array of vehicles
        Vehicle garage[2];  // <- you only have 2 vehicles in your garage
            // ie... [0] and [1] are valid indexes

        garage[0].setName("Jaguar\0");
        garage[1].setName("Audi");

        cout << garage[0].getName() << " has " << garage[0].getNumDoors() << " doors" << endl;
        cout << garage[1].getName() << endl;
        cout << garage[2].getName() << endl;  // <- OUT OF BOUNDS, this vehicle
            //  does not exist! 


You are attempting to access a 3rd vehicle when you only have 2. Therefore you are stepping out of bounds of your array and accessing invalid memory.


Change your array to have 3 elements instead of 2.
Feb 26, 2014 at 6:25pm
1
2
3
Vehicle garage[2]; //Create an array of 2 Vehicles, valid indices are [0] and [1]

cout << garage[2].getName() << endl; //Accessing out of bounds. Only luck have prevented crash here. 
Feb 26, 2014 at 6:30pm
DOH!! Silly me.. I've been playing with the default value for so long I didn't even spot that. Thanks :-)
Topic archived. No new replies allowed.