Classes example - simple

Hi, I'm very new to classes. Sorry if my technical explanation for what I'm trying to do is a bit off.

I'm trying to make my program work by putting in the name of a state in a class member function. In the code below, I'd like it to pass "Texas" through the class, "State_Name_Class", and print out "Austin". But I seem to be missing something here. Says the state names are undefined. I have thought by using the part of the code,

1
2
private:
		string name;


I'd be defining the string variable "name", which is private, so only used by the this class which it is in (local variable).

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
/*
Goal:  Make a program in which the user defines which state they want to know the capital of, and in the class member in the main(),
         they type it in.  In the future, may be interesting to output names of avail. states, and have user input which to use.
*/

#include <iostream>
#include <string>

using namespace std;

class State_Name_Class
{
  public:

	void Choose_State(string name)
	{
		if (name = Massachusetts)
		    {cout << "Boston" << endl;}

		  else if (name = California)
		    {cout << "Sacramento" << endl;}

		  else if (name = Texas)
                    {cout << "Austin" << endl;}
	}

	private:
		string name;

};


int main()
{

// Classes available
  State_Name_Class state_name;  // creates an object, "state_name", which will point to a function within the class "State_Name_Class"
		  state_name.Choose_State("Texas");  //object "state_name" points to function "Choose_state", which is a class member
		  
  cin.ignore();

  return 0;
}



Any ideas where I'm off?
You are missing " " and when testing you need == not = which is assignment.

(name == "Massachusetts")
thank you! careless oversight!
So one remaining question. I added in a 2nd member function to the class, but I cannot seem to get it to display the print out when the program runs. It's gotta be simple, just not sure what I am doing wrong.


My 2nd member function doesn't seem to run:

 
state_or_car.Choose_Car("BWM");           //object "state_or_car" points to class member function of "Choose_Car" 


Here's the program:

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
/*
Goal:  Make a program in which the user defines which state they want to know the capital of, and in the class member in the main(),
         they type it in.  
		 
		 Also have the user define the name of a car, and program will display country where it is made.
*/

#include <iostream>
#include <string>

using namespace std;

class States_And_Cars_CLASS
{
  public:                            // place functions in here

    // stats and capitals
	void Choose_State(string state)
	{
		if (state == "Massachusetts")
		    {cout << "Boston" << endl;}

		  else if (state == "California")
		    {cout << "Sacramento" << endl;}

		  else if (state == "Texas")
            {cout << "Austin" << endl;}
	}

	// cars and countries of their origin
	void Choose_Car(string car)
	{
		if ( car == "Toyota")
		    {cout << "Japan" << endl;}

		  else if (car == "Ford")
		    {cout << "Unites States" << endl;}

		  else if (car == "BMW")
            {cout << "Germany" << endl;}
	}

	private:        // local variables in here (which only the above functions use)    
		
							// I've placed a local variable here which is to be used ONLY by the class
		                  // this is different than a global variable, which can be accessed anywhere in the code
		string state;
		string car;
};


int main()
{

// Classes available
	States_And_Cars_CLASS state_or_car;  // creates an object, "state_or_car", which will point to a function within the class "States_And_Cars_CLASS"
	      
		  state_or_car.Choose_State("California");  //object "state_or_car" points to class member function of "Choose_State"
		  state_or_car.Choose_Car("BWM");           //object "state_or_car" points to class member function of "Choose_Car"
		   
  cin.ignore();

  return 0;
}

Line 59 "BWM" you mean "BMW".

You should think about putting in an else so you have a default case. Even if you don't leave in it for the final project it will help you troubleshoot.
Last edited on
thanks for pointing this out! very much appreciated! I've made that change for the 'else' case, too.

Last question: I've made a 2nd class to test using function within it utilizing numbers for math operations, and I came across 2 sticking points:

1. In the member function, "Decision()" of class "Math_CLASS", the program seems to be giving me garbage values of num1, num2, num3, num4 when this line is printed:

1
2
3
4
5
6
7
8
9
10
11
void Decision()
			{
				if (add_answer > multiply_answer)
				  {cout << "Adding " << num1 << " and " << num2 << " is greater than multiplying " << num3 << " and " << num4 << endl;}

					else // (add_answer < multiply_answer)
			          {cout << "Multiplying " << num3 << " and " << num4 << " is greater than adding " << num1 << " and " << num2 << endl;}

				cout << "This program used " << num5 << " numbers of variables." << endl;

			}



2. The 2nd issue, is that I tried to set in the private access specifier,

 
int num5 = 5;


, but the program says,

Error: data member initializer is not allowed


Why won't it let me set the value of the local variable here?


Here is my 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Goal:  Make 2 classes

Class 1:  States and cars, 2 member functions of the class
   overview:  Make a program in which the user defines which state they want to know the capital of, and in the class member in the main(),
         they type it in.  Also have the user define the name of a car, and program will display country where it is made.

Class 2:  Math operations
   overview:  - take 2 numbers as the input and do addition on them, and multiply them  
              - total vaues of adding & multiplying
			  - take final number and somehow just pass in the value and make a decision, if/else on it, as if "processing" it
		           for a decisional outcome


*/

#include <iostream>
#include <string>

using namespace std;


int Total;



/**************************************** 
                CLASSES 
****************************************/
class States_And_Cars_CLASS
{
  public:                            // place functions in here

    // stats and capitals
	void Choose_State(string state)
	{
		if         (state == "Massachusetts")
		    {cout << "Boston" << endl;}

		  else if  (state == "California")
		    {cout << "Sacramento" << endl;}

		else //    (state == "Texas")
            {cout << "Austin" << endl;}
	}

	// cars and countries of their origin
	void Choose_Car(string car)
	{
		if        ( car == "Toyota")
		    {cout << "Japan" << endl;}

		  else if (car == "Ford")
		    {cout << "Unites States" << endl;}

		else //   (car == "BMW")
            {cout << "Germany" << endl;}
	}

	private:        // local variables in here (which only the above functions use)    
		
							// I've placed a local variable here which is to be used ONLY by the class
		                  // this is different than a global variable, which can be accessed anywhere in the code
		string state;
		string car;
};

class Math_CLASS
{
  public:                            // place functions in here

    // Add
			void Add(int num1, int num2)
			{		
				add_answer = num1 + num2;
				cout << "num1 + num2 = " << add_answer << endl;
			}

	// Multiply
			void Multiply(int num3, int num4)
			{
				multiply_answer = num3 + num4;
				cout << "num3 + num4 = " << multiply_answer << endl;
			}

	// Totaling up things
			void Total()
			{
				Total_Value = add_answer + multiply_answer;
				cout << "Total Value = " << Total_Value << endl;
			}


	// Desicion making based on answers in functions
			void Decision()
			{
				if (add_answer > multiply_answer)
				  {cout << "Adding " << num1 << " and " << num2 << " is greater than multiplying " << num3 << " and " << num4 << endl;}

					else // (add_answer < multiply_answer)
			          {cout << "Multiplying " << num3 << " and " << num4 << " is greater than adding " << num1 << " and " << num2 << endl;}

				cout << "This program used " << num5 << " numbers of variables." << endl;

			}
	
		private:        // local variables in here (which only the above functions use)    
		
							// I've placed a local variable here which is to be used ONLY by the class
		                  // this is different than a global variable, which can be accessed anywhere in the code
		
		// adding variables
		int num1, num2;
		int add_answer;
        
		// multiplying varaibles
		int num3, num4;
		int multiply_answer;

		// total answer (adding the above)
		int Total_Value;

		// total number of number variables used
		int num5 = 5;
};


int main()
{

// Load Classes available and their objects
	States_And_Cars_CLASS state_or_car;  // creates an object, "state_or_car", which will point to a function within the class "States_And_Cars_CLASS"
	Math_CLASS math_operation;           // creates an object, "math_operation", which will point to a function within the class "Math_CLASS"


		  state_or_car.Choose_State("California");  //object "state_or_car" points to class member function of "Choose_State"
		  state_or_car.Choose_Car("BMW");           //object "state_or_car" points to class member function of "Choose_Car"

		  cout << endl;

		  math_operation.Add(2,8);
		  math_operation.Multiply(10,200);
		  math_operation.Total();
		  math_operation.Decision();

		   
  cin.ignore();

  return 0;
}
I got it now - thank you for helping me out!
Topic archived. No new replies allowed.