Class / Member problem

if i make a class with private variables and i make public member functions to populate those private variables, can i populate the variable and return the variable in the same member function and how would i do that? Or do i have to make two separate member functions, 1 to store the variable and 1 to return it. For example,

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
class CarData
{
    private:
        int year;

    public:
        void CarData::SetYear(int year1);
        int CarData::GetYear(void);
};

int main()
{
return EXIT_SUCCESS;
};

void CarData4::SetYear(int year1)                          
    {
        year = year1;
    }

int CarData4::GetYear(void)
    {
        return year;
    }

Populate is probably the wrong word. I'd use "set" or "initialize" (if it was the first time value set)
and yes you could do both in the one member function. like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class CarData
{
    private:
        int year;

    public:
        int CarData::SetYearandReturn(int year1);
};

int main()
{
return EXIT_SUCCESS;
};

int CarData4::SetYearandReturn(int year1)                          
    {
        year = year1;
        return year;
    }
ok take a look at this and tell me what's wrong. It only outputs the last year the user input ever how many times the elementLength - 1 is to the screen instead of say getting 1990 2000 2005 i get 2005 2005 2005
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
class CarData4
{
    private:
        int year;

    public:
        int CarData4::SetYear(int year1);

};

int main()
{
    CarData4 car[50];
    int elementLength, ctr, userYr;

    cout << "How many vehicle's would you like to add to the inventory?: ";
    cin >> elementLength;
    cout << endl;

    if (elementLength <= 0)
    {
        return EXIT_SUCCESS;        
    }

    for (ctr = 0; ctr <= elementLength - 1; ctr++)
    {   
        cout << "Please enter the vehicle's year: ";
        cin >> userYr;
        car[ctr].SetYear(userYr);       
    };

    for (ctr = 0; ctr <= elementLength - 1; ctr++)
    {   
        cout << car[ctr].SetYear(userYr);       
    };
    
    


    system("PAUSE");
    return EXIT_SUCCESS;
}

int CarData4::SetYear(int year1)
{
    year = year1;
    return year;
}
Last edited on
You are setting the year every time you call CarData4::SetYear, including the times when you only mean to get the year.

Do you see a conceptual problem with your design?

Perhaps you meant to do something more like this:

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
#include <iostream>

using namespace std;

class CarData4
{
private:
    int year;

public:
    int year() const { return year; }
    void year(int yr) {year = yr;}

};

int main()
{
    CarData4 car[50];
    int elementLength, ctr, userYr;

    cout << "How many vehicle's would you like to add to the inventory?: ";
    cin >> elementLength;
    cout << endl;

    if (elementLength <= 0)
    {
        return EXIT_SUCCESS;
    }

    for (ctr = 0; ctr <= elementLength - 1; ctr++)
    {
        cout << "Please enter the vehicle's year: ";
        cin >> userYr;
        car[ctr].year(userYr);
    }

    for (ctr = 0; ctr <= elementLength - 1; ctr++)
        cout << car[ctr].year() << '\n';
}


Although, I prefer the get/set nomenclature myself.
thanks, yeah i prefer it too it's just my final project has to be less than 175 lines of code and the get/set method takes up a bunch of lines
Topic archived. No new replies allowed.