2
Posted September 28, 2009 by Spyros in C/C++ Programming
 
 

Introduction to C++ and Object Oriented Programming

The C++ Programming Language
The C++ Programming Language

If you are just starting up with C++, you probably need to know some important things in order to properly use the language. I’ll be trying to explain some of the important aspects of C++ so that you get a decent view of its whereabouts.

I
s C++ a better C ?

Well, not really. I could just say that C++ is a new language. You may argue that the keywords and structures of those languages are 80% the same, but the real difference comes from the fact that C++ is an object oriented language while C was not designed for that purpose.

What is object oriented programming ?

If you have never used OOP i can just say to you that understanding it is not easy and needs practise. I will try to simplify it for you as much as i can using simple examples. OOP is in fact an effective way to organize your coding into reusable modules. Object oriented comes from the fact that each component is considered to be an object that has separate behaviour from the other modules.

I suspect that this is clearly confusing to you, so i’ll give an example. Suppose that we need to create a program that appears a geometrical schema. This could be a triangle, a rectangle or a circle. On way to program this using C would be to just have a switch or multiple ifs, check which schema we want and print it. It could be something like:

if (schema == 'rectangle')
doRectangle();
else if (schema == 'triangle')
doTriangle();
else if (schema == 'circle')
doCircle();

While this is effective, there lie some problems. Each and every schema does some things that all others do. For instance, every schema could need to initialise the GDI interface or just draw a line. These operations are either inside the functions doRectangle(), doTriangle() etc or just lie in somewhere in the code. Moreover, if something changes in the way a rectangle is plotted, there could be some heavy problems to change the code to reflect the new changes due to the fact that it’s used everywhere in the same way.

Imagine what would happen if every schema was its own commander. Then, changes would be easier to make, others would not be affected and code would be much cleaner. The OOP way to code something like that would be using classes (which i will thoroughly describe in a later tutorial) and create a model like that shown below:

SCHEMA => Triangle
SCHEMA => Circle
SCHEMA => Rectangle

This would mean that a schema can provide ways to build a triangle, a circle or a rectangle. In programming, Schema would be an abstract class, which means a class from which an object is never initialised by the user but used as a template for deriving classes. This is confusing i know, so i’ll elaborate.

Imagine that SCHEMA is a class that just contains ways to draw simple lines, or dots or whatever we need. It also provides a method named printSchema(), which is initialized as a VIRTUAL. This means that if the new class Triangle uses this function, it can change it in a way so that it can now draw a triangle. I do not want to enter into more details as i will be describing classes and polymorphism in a later article. However, i guess that you get the idea. A SCHEMA is the general representation and  when a Circle for example uses that SCHEMA code, it “alters” it so that it fits its needs. Bear in mind, that Circle alters its OWN CODE through a SCHEMA filter, which means that SCHEMA’s actual code is not changed but overriden inside the Circle code.

C++ also has some new features that make it even more wrapped around the object, one of which is templates and operator overloading. I won’t go into details at this stage but bear in mind that templates are a way to write code that can be used with multiple objects(for instance, to create a linked list for chars and integers without needing any changes to the code). Operator overloading is a way to execute a function like Add() using a command like x + y where x,y could be of type char and this x+y in fact means append the character of y on x.

In this first tutorial i tried to show you why C++ is much different than C and hopefully enlarge your appetite to learn more about the language. I’ll be soon publishing more tutorials on C++ and based more on the practical programming side, but i felt a simple introduction was necessary.


Spyros