building a class called "car."
Note: Some of the variable names may differ from what I've declared as part
of the class interface earlier.
Logically, in order for an object to exist, we need to create, or construct
it from scratch. This is what a constructor is. Here, a constructor is a
special function that instanciates, or make an object exist on a computer's
memory. It usually takes a value from input and assigns it to the member
variables of a class; in some cases, we (the class designers) need to assign
default values for our member variables for our classes (called default
constructor).
A typical constructor looks like:
class::class(type whatever, additional data)
{
member_var = value;
// Additional ones...
}
There are quite a few things here. I think this is the first time you are
not seeing a return type for a function like this. Actually, a return type
does exist, but it is not that apparent: that return type is to tell the
program we want to create an object, that's all. Also, I believe this is the
first time you are seeing double colons (::). This is called "scope
resolution operator" and is used for telling us (the programmers) where a
data or a function belongs to. For instance, if we put:
std::cout << "Hi..." << endl;
we are saying that we want to use the "cout" operator under standard
library. Similarly, if we put our class name and the double colon in front
of a print function, like:
class::print();
We are saying that we want to use the print function - a print function that
is member of an object. This becomes important later when we talk about
inheritance.
Now, let's create a default constructor for our car class - a constructor
that will be invoked when we don't supply our data when we declare a car
object. In order for this to work, we need to provide our own default values
for our car object. For instance, we can say that the default car is 2004
Ford Escape (a likely car). In order to do that, we can say:
* Create a default constructor called "car" with member variables of the car
object (model, manufacturer, date of release) provided by myself.
In C++, it would look something like:
Car::Car()
{
model = "Escape";
release_date = 2004;
manufacturer = "Ford";
}
So, for instance, if we declare something like:
Car test;
It would invoke the default constructor (the values we've just declared
above). As I've been saying: you need to provide initial values for ALL
member variables of a class.
But what if we want users to actually give us the model, manufacturer and
release date of a car? We can come up with so-called "parameterized
constructor" to take a user's input and assign it to the member variables.
In this context, it would be two strings for model and manufacturer, and an
int for released date (year), with each of them called m, n and r,
respectively. So what should this constructor do?
* Almost same function body and declaration as the default constructor
above, except that we need to provide a set of arguments.
* Here, the arguments would be user-specified properties of an object.
In C++, it would look somewhat similar to what we have up there, except:
Car::Car(int r, string n, string m)
{
model = m;
manufacturer = n;
release_date = r;
}
Sound familiar? This is exactly the same thing as the default constructor -
instanciates a car object with values obtained from the users, like:
Car my_car(2008, "Ford", "Mustang");
As we've seen, there are different ways of instanciating a class via
constructors. There are other variations of this concept, namely ones with
different parameter sets, one or more parameters missing (in that case, we
need to "fill in the blank" i.e. provide our own default values for missing
parameters) or copying one object to another (called copy constructor). The
last one will not be seen until much later.
As for declaring an object, in my case, I prefer to use parameters when
instanciating my own object of my own creation. It's really up to you to
invoke either the default or a parameterized constructor when declaring
something of your type. The next topic will be on how to obtain a member
value of an object, as well as setting something to our liking (accessors
and mutators), respectively.
// JL
No comments:
Post a Comment