what is a class? what does a class do? What is a constructor?

In the beginning before we knew what class was we used to make our functions seperately.

Making a class does it group all my functions together and thats it?

does a constructor just initialize?

Lets say you have a car. It has wheels (2, 4, 6..) , engine size (v4, v6, v12..), type (suv, truck, car..)

You can place all these variables under one "umbrella" or class which can be used sort of like a template. We can call this class car and it will contain the above mentioned variables. Or member variables because they are part of this class. So now lets say you have a car called Corvette. You instantiated the object corvette from class and it will have its own member variables (same ones from above) So now you can say you have a Corvette with for wheels and a v8. You can find out what engine size is by saying Corvette.engineSize

Just a quick explanation!
a class is a user defined variable type. Roughly, it is a way to group data (say, 2 strings, 3 doubles, a Boolean, and a vector of integers for example) and the functions that operate on that data.

a constructor is a function in the class that performs actions whenever a variable of that type is created. Say you need to initialize that Boolean to false and all the ints in the vector to zero, you do that here.

it groups the functions for that type together, not all functions. You can (and will, soon) have many classes in one piece of code, and maybe some loose functions as well.

yes, the constructor is just initialize. It may be a copy constructor, and initialize one instance from another, going with the car theme.. or it can just set values.

carclass car2(car1); //set all of car2 identical to existing item, car1.


Topic archived. No new replies allowed.