I’m reading a book, the Primer guide to C++.
I think I got the hang of it upto a point, I just want to clarify a few things.
-
It says that a
classis like a data type (A data type beingint,char, ect ..) and that anObject/Instance is like a variable. Is this true? -
What is a data form?
-
What part of a statement is the declaration? Is it the
data type+the variable, and the=is the assignment?
Not quite.
A class is “like a data type” in the sense that it’s a template for the creation of an object, but it isn’t itself an object that you can use.
When you use that template to create an instance of an object, then you can make use of that object. You may create as many instances as you want – you can think of those instances as variables.
For example:
This is a class definition for a Person. It is not a variable. You cannot call setName on it because it doens’t exist yet. But when you do:
You created two instances of people that you can assign to, change, and use – they are variables called instances.
I have no idea what “data form” means, ignore that.
As for declaration – the declaration states that an instance of something will be present. For example, if you were creating a class, and your header file had:
You are declaring that you have a function called bar that returns void and that you have an integer called x.
No memory is allocated for the variable, x, and no definition is provided or bar, so they’re just declarations. Your source file would probably provide definition for bar like:
and a constructor definiton for foo that would initialize x with a value and control how it was created (with an initializer list):