CtoC++
CtoC++: Upgrading to Object Oriented C
Introduction
This tutorial carries on where StartingC left off.
To get the material, cut and paste the contents of the box below onto your command line.
svn co http://source.ggy.bris.ac.uk/subversion-open/CtoC++/trunk ./CtoC++
In this tutorial we will assume basic linux skills as outlined in Linux1.
Cutting to the Chase: Classes and Encapsulation
So, he we are contemplating C++. We've got to grips with most of the C language in StartingC and it looked alright. Definitely serviceable. What's all the fuss about C++? Well, I believe that most of the fuss is about encapsulation. We saw the benefit of collecting together related variables into structures in C, true? Well, C++ goes further and allows us to collect together not only related variables, but also functions which use those variables too. An instance of a class is called an object and it comes preloaded with all the variables and functions (aka methods) that you'll need when considering said object.
What may have seemed like the relatively small enhancement of adding methods to the encapsulation has, in fact, resulted in a sea-change. No longer are we thinking about a program in terms of the variables and the functions, but instead we're thinking about objects (planets, radios, payrolls and the like) and how they interact with other objects. The whole thing has become far more modular, and so easier to work with. Indeed, this is no accident as object oriented programming (OOP) arose in response to programs written in the functional style getting larger and unwieldy and hard to work with. In short it arose to swap spaghetti for lego.
OK, enough of the spiel, let's get going with an example:
cd CtoC++/examples/example1 make
The first chunk of code to greet you inside class.cc (we'll use .cc to denote C++ source code files) is:
// // This is a C++ comment line // #include <iostream> // A useful C++ library #include <cmath> // The standard C math library // declare a namespace in which to keep // some handy scientific constants namespace scientific { const double pi = 3.14159265; // note the use of 'const' const double grav_constant = 6.673e-11; // uinversal graviational constant (m3 kg-1 s-2) const int sec_per_day = 86400; // number of seconds in 24 hours } // avail ourselves of a couple of namespaces // via the 'using' directive using namespace std; // allows us to use 'cout', for example using namespace scientific;
What's new? Well, first up, we see that the comment syntax has changed and that we can use just a leading double forward slash (//) to signal a note from the author. #include is familiar, except that we've dropped the .hs from inside the angle brackets.
The next block is a namespace declaration. The concept of a namespace is common to a number of programming languages and here we're setting one up called scientific and using it to store some handy constants. We can enclose anything we like in a namespace. We access the contents of a namespace via the using directive. In this case we're accessing an intrinsic one called std (standard)--we'll be doing that a lot!--and also our scientific one. The idea behind namespaces is to reduce the risk of a clash of names when programs get large. They're handy.
Weighing the Earth example
public, private and interfaces
classes as building blocks, glue code and lego bricks
More on Methods
Constructors, destructors, overloading
Inheritance
template classes, virtual etc.