Monday, August 2, 2010

C++: Introduction to classes

Until now, we have used built-in data types or data types that C++ comes
with. Starting from this post, I'll dive into ways in which we can have our
own data types for solving various problems. Along the way, we'll
investigate how to manipulate classes, pointers and other more advanced
topics.

First of all, as this is just an introductory post, let's ask ourselves,
"what kind of data can a computer store?" Obviously it needs to work with
numbers, so an integer data type is provided. Next, we want to have a
computer work with decimals, so we use floating point (decimal) numbers, in
our case, a double number (technically called double precision floating
point numbers). However, sometimes, we want computers to store our names, so
we use characters and strings to help us out with that task. Finally
(almost), we want computers to make decisions based on user input, so we use
Boolean (tru/false) values. There is one more built-in data type called
pointer - an address of where data is stored, but we will not meet its true
form until later.

But how about if the problem we wish to tackle cannot be solved using the
above data types above? How about if we need to work with various game
scenes, a list of rooms or counting number of cars at a parking lot? What if
we want to create our own programmer-defined data types, or custom data type
that we can use for solving other kinds of issues? The answer is a perfect
"yes, it is possible to create our own data types". C++ and other
programming languages allows us to create and work with our own data types,
known as classes and objects. Any data that a computer can work with -
numbers, names, and even files - are called objects - hence, the phrase
object-oriented programming (OOP) came out, which means we can manipulate
these "objects" to help us solve problems.

So what other kinds of data we can "create"? There are millions of them,
including a desk object, a chair, a video game character or even the stars
on the solar system. Each of these data types are not defined in C++, so we
(the engineers) need to create them, or define them. For example, a "video
game character" object would have properties such as its name, various
actions it can perform and sounds it can make, if any. Similarly, a desk
would have descriptive values such as its length and width, its uses (dining
table, study desk, etc.) and who made the actual desk (the manufacturer).
All these are stored as an object, or class as private member variables
within it (we'll come back to the meaning of that statement when we visit a
"lab" i.e. exercises and examples for creating a class). Also, the various
actions that can be performed on it can be dealt with via creating member
functions (we've seen this phrase before, but we'll examine the very meaning
of that phrase shortly when we create an example class).

Now, can you think of any other objects we can create using this method? If
you have any, please leave a comment... We'll examine how it can be done, as
well as some terminology used when talking about classes starting from a few
posts later...
// JL P.S. Need to study for a homework at UCR.

No comments:

Post a Comment