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;
}
|