1. Why do we need classes, why can’t we just use structs?
Classes are important as they allow us to incorporate functions as well as data.
2. What are methods?
Methods are another name for member functions (functions within a class).
3. How would you define a class?
To define a class you use the keyword class followed by a definition name, ideally starting with a capital letter to differentiate it from a standard variable type, then you define each of the members within curly braces. This does not instantiate an object, it merely defines what it will look like.
This is what a class might look like:
class Maths
{
public:
int firstNumber;
int secondNumber;
void printResult()
{
cout<<"When added together, your numbers ("<<firstNumber<<" + "<<secondNumber<<") equal = "<<firstNumber+secondNumber<<endl;
};
};
int main()
{
Maths sum_1{8, 4};
sum_1.printResult();
return 0;
}
4. What is an object?
An object is just about anything that has been instantiated. A struct, array, function, class , etc. are all objects, but only once they have had a variable or function name assigned to them - at this point the computer creates and assigns memory to the object.