this notes series and wondering what should I do next. Well, I was like you
when I took my C++ course. Now being a amature pforgammer for about a year
or so, I'm starting to understand the "under the hood" works done by
compilers when we create custom data types - classes. So, starting from this
post, let's build a simple class - a "car" class - perhaps to use it in a
car manufacturing plant simulator or similar.
The first thing we need to think about is what member variables and
functions (methods) would this car class have. First, let's think about what
properties does a typical vehicle have: the car model, manufacturer, date of
manufacture and its type, among other things. Next, what sort of thing do we
want to do with this car? We need to find out its model and manufacturing
date, as well as have a plant worker label the car's type.
Based on that, a design of a car class comes out to be:
A class called "car":
{
* Member variables: a string to hold car's model, an int to hold its
manufacturing date, and a string to hold the type of car.
* Member functions: Useful constructors (to make the object exist),
accessors (getters) to find out the properties of a car and mutators to set
a car's property based on user input.
};
The above code is the English version of a typical C++ code for defining a
car class, which is reproduced below with comments:
class Car
{
private:
string model; // The model of our car.
int date_of_release; // The manufacturing date.
string car_type; // The type of our car.
public:
Car(); // The default constructor.
Car(string m, int y, string t); // A parameterized constructor (m = model, y
= year, t = type).
// Accessors:
int get_release_date() const;
string get_model() const;
// Mutators:
void set_model(string & m);
};
There are several things we might have seen elsewhere, but wasn't explained
in detail in code form until now. also, we have quite a new friends out
here, so I'll describe to you what they are:
* Whenever we don't want to change anything stored in a variable, we put the
keyword "const" (constant).
* Notice that I used access keywords (private and public). The private parts
are the member variables - this is called "data encapsulation" where the
outside code does not know what the internal properties does an object have
apart from the object itself.
* The default constructor does not have any arguments; however, we need to
initialize ALL members of this class to a known (or other) values as
default, which I'll show you on the next post.
The above class code is sometimes called "class interface" or class
definition, and is usually stored in a header file with the class name e.g.
car.h. The actual "body" of our class, or class implementation is stored as
a source code file e.g. car.cpp. Although it is possible to write the class
interface and implementation under one roof, it is far more easier (in terms
of maintenance) to store them separately (this is called multi-file
compilation). But when we use this method, we need to put in more extra text
to really make sure that the compiler would know what to do when
encountering a class that was defined already (called inclusion guard). The
full text of a typical .h file will be shown on the next post with few more
definitions. After this, I'll go through creating a class implementation
with comments about constructor creation.
// JL
No comments:
Post a Comment