TweetFollow Us on Twitter

C++ Overview
Volume Number:5
Issue Number:9
Column Tag:Jörg's Folder

C++ Overview

By Jörg Langowski, MacTutor Editorial Staff

“An Overview Of C++”

MacHack ’89 brought me not only a colorful set of screwdrivers from APDA, but also a new assignment: yours truly is supposed to run a tutorial column on C++. C++ is a very interesting programming language. Supposedly, the new Finder was written in it. Also, it exists on a couple of Unix systems. In fact, shopping for a Unix system, we recently met a representative who assured us that C++ would be delivered with the system. Jordan Matthews, and other people from Apple, spoke very highly about C++ at the MacHack, and assured us that we would get our fingers on a pre-release of Apple’s C++ for MPW, supposedly to be delivered by the end of this year.

As you might have guessed, Apple hasn’t sent us the pre-release yet, and I have yet to use a working C++ compiler. So far my only ‘hands-on’ experience is Bjarne Stroustrup’s book, The C++ Programming Language (Addison-Wesley 1987), which I highly recommend.

The style of the book is rather terse, and you have to work your way through. A good example is that after the introduction, not much is said about ‘object-oriented’ programming, until you hit page 213:

“A list specified in terms of pointers to a class can hold objects of any class derived from that class. That is, it may be heterogeneous. This is probably the single most important and useful aspect of derived classes, and it is essential in the style of programming presented in the following example. That style of programming is often called object based or object oriented; it relies on operations applied in a uniform manner to objects on heterogeneous lists.”

So now you know what you’ve really done when you used MacApp The fact that Stroustrup refers to object-oriented programming in this rather abstract way made me dig out an old introduction to Simula 67, which was the first language to introduce object-oriented concepts. There, too (the book dates from 1973) no reference is made to OOP as we know it today. All the important constructs - classes, instances, methods, overriding - are already there, and one could have implemented today’s programming style in Simula; only computers were much smaller, and most programs did not demand OOP concepts.

The C++ Design

Stroustrup’s team designed C++ for dealing with simulation problems not unlike those that Simula was developed for. However, C++ is a much broader concept than simply a set of ‘object-oriented’ extensions to C; it is a redesign of the C language. To use another quotation from Stroustrup’s book, “C++ was designed to enable larger programs to be structured in a rational way so that it would not be unreasonable for a single person to deal with 25,000 lines of code”. To achieve such an ambitious goal, the most important point is to allow the user to extend the language to accommodate new ‘shorthand notations’ for things that have to be done over and over again. For instance, in a program that uses matrix algebra, given the 25-row by 35-column matrix C and 25-row by 15-column matrix B,

A = ^B*C

is much easier to read than

matmul (A,transpose(B),C,15,25,35).

To be able to use such a shorthand for matrix multiplication, we would need two features built into the language: a. data structures that carry additional information, such as row and column size for a matrix, but which is normally hidden to the user; and b. the capability to redefine operators - like ‘*’ or ‘^’ - depending on the context in which they are used. The latter feature would then cause a ‘*’ to behave differently depending on whether it is used to multiply two integers, reals, vectors or matrices. Some of this behavior is already built into most compilers: integer and real multiply generate different code. But this behavior cannot be modified. C++ allows you to modify your operators in any odd way.


Let’s assume we wanted to define an array structure, matrix, whose size is not defined at compile time and for which space will be dynamically allocated at run time. In C, one might write

typedef struct matrix
    int rows, cols;
    int *m;  /* pointer to matrix  data */
matrix a

and then write an indexing function elem(i,j) which refers to the (i,j)th element of the matrix a by

*(a.m + 2*(i*a.cols + j)).

Of course, it would be much easier to simply define a two-dimensional array and write a[i][j], but let’s stay with this definition for a while; unlike the usual array definition, this matrix is resizeable and space is allocated dynamically at run time. We would have to find a block of memory to hold the matrix data and put a pointer to it in m.

When we access an array, we are often not interested in its actual dimensions, as long as the indices are not out of range. In C++ we can define the matrix type in such a way that only certain functions have access to information ‘private’ to the array (such as its dimensions), and all access to the array’s data is done through these access functions, called methods. Data structures that may carry private information are called classes in C++. (According to the manual, classes are ‘user-defined types’ - the most general definition that one might imagine!). A class is just like a struct in which some of the fields cannot be ‘seen from the outside’, and in which the interface to these private fields is defined through method declarations. The C++ class definition for the matrix type would look very similar to a struct declaration, with some additions. The syntax of the class declaration is:

/* 1 */

class matrix
     int rows, cols;
     int *m;  /* pointer to matrix  data */
     int rowsize() { return rows}
     int colsize() { return cols }
     void set_size(int,int);
     int& elem(int,int);
matrix a

Those of you who have had some experience with NEON [let’s make the point again that it is a shame that NEON has disappeared ] might recall that its class definition looked similar:

:class matrix <super object
     2 <indexed
     int rows
     int cols
   :M rowsize    ;M

NEON, however, did not have the label public: for separating the private and public parts of the class declaration. In NEON, all variables were private and all the methods were public.

The C++ class declaration is similar to a C struct declaration, with the possibility to include functions and to hide parts of the declaration from the outside. The public functions in a class that constitute the interface to the outside world are called methods.

There are two principal ways to define a method. One can write the method code inside the class declaration (as for rowsize and colsize in the example above), or one can just declare the method and write the method code later, as for set_size or elem. elem returns the reference to an integer that is the (i,j)th element of the matrix and might be defined as follows:

/* 3 */

int& matrix::elem(int i, int j)  { return m[i*cols + j] };

There is a fundamental difference between methods defined inside and outside of a class declaration. The methods defined outside will be called through a subroutine call, while inside-defined methods are inline-expanded by the compiler. Writing a.rowsize will not generate a JSR to the function code, but code that will directly reference the hidden field a.row. However, any method that is defined outside a class declaration may also be defined as an inline method by prefixing it with the keyword inline:

/* 4 */

inline int& matrix::elem(int i, int j)  { return m[i*cols + j] };

There are two more special methods in the class declaration which carry the name of the class, or respectively the class name prefixed with a tilde (~). These are the so-called constructor and destructor methods; they are called when a new object is declared (as in matrix a;) or deleted (when one leaves the block that the object was declared in). Constructors and destructors are important when heap space has to be allocated for an object (our matrix will need it) and deallocated when the object is no longer defined.


Our dynamically sized matrix might be defined in a slightly different way which allows to access the elements in the usual way, writing a[i][j] instead of a.elem(i,j). One first defines a one-dimensional array class (as in Stroustrup’s book):

/* 5 */

class vector 
     int* v;
     int sz;
     vector(int); ~vector();
     int size () { return sz; }
     void set_size(int);
     int& operator[](int);
     int& elem(int i) { return v[i] };

and then builds the two-dimensional class on top of it:

/* 6 */

 class matrix : vector
     vector*& mv;
     int rows, cols;
     matrix(int,int); ~matrix;
     int rowsize () { return rows; }
     int colsize () { return cols; }
     void set_size(int,int);
     vector*& operator[](int);
     int& elem(int i, int j) { return mv[i][j] };

(I hope this is approximately correct while I’m waiting for the C++ system to try this out and get ready for your embarrassing remarks). In the program, one would declare matrix a(10,20) and access the (i,j)th element by writing a[i][j]. The array indexing operator [] has been re-declared in the class declaration, and will now support checking of index bounds, if we wish so.

The actual implementation of the operators has of course to be done separately. We would write

/* 6 */

 int& vector::operator[](int i) { /* body of code */ }


/* 7 */

 vector*& matrix::operator[](int i) { /* body of code */ }

to implement the new definitions.

The matrix multiplication operator may now be defined easily. We write

/* 8 */

matrix operator*(matrix& a, matrix& b)
     matrix c(a.colsize,b.rowsize);
     if (a.rowsize != b.colsize) error “index mismatch”;
     for (int i=1 ; i<a.colsize ; i++)
          for (int j=1 ; j<b.rowsize ; j++)
               int sum = 0;
               for (int k=1 ; k<a.rowsize ; k++)  
                    sum = sum + a[i][k]*b[k][j];
               c[i][j] = sum;
     return c;

Again, I hope this would work in an actual example. It is not the most efficient way to program the matrix multiplication; the good way to do it would be using friend definitions. This concept is explained in Stroustrup’s book, and I’m going to come back to it in the next column, where I can supply some examples.

The expression a*b, where a and b are of type matrix, would return a pointer to another object of class matrix, which contains the product of a and b. To make sense of the expression c = ^a*b, we would also have to define the transpose operator, ‘^’, and the assignment operator, ‘=’. I won’t write these definitions down here; you might try to work them out, or better, test them if you have a C++ system available.

Operator redefinition is one of the most important concepts of C++, since it makes the code much more readable. The redefinition of an existing operator (like +, *, etc.) is called operator overloading; when such a redefined operator is used, the compiler will automatically search the existing definitions to find one that works on the data types provided. Thus, even if one redefined * for matrices, integer and real multiplications would still work as before. I have not found out yet whether dynamic binding is possible for operators by declaring them virtual (see below), but I’m sure I’ll soon be able to test that.

Class Hierarchies

We have seen the syntax of a class definition which was derived from another class, class matrix : vector { }. If we define a derived class this way, none of the methods in the superclass will be accessible through an object of the subclass; all subclass methods have to be explicitly defined in the subclass declaration. If we write, on the other hand, class matrix : public vector { }, any method from class vector that is not redefined in class matrix is usable on objects of class matrix as well. This is the way we very often wish objects to behave; methods that are redefined in a subclass should override the superclass definition, but if an object does not ‘know’ about a method it should look for a definition higher up in the hierarchy.

In a class hierarchy we should therefore be able to apply a method to an arbitrary object whose exact type is not known at compile time. If the object’s type is known at compile time, the compiler will simply generate a JSR to the appropriate method code, passing arguments as required. This is known as early binding in object-oriented jargon. If the type is not known, we must check at run time what type of object is given the method call, and see whether the method is defined in the object’s class declaration or somewhere higher up in the hierarchy. This is called late binding; a run time error message will be generated if the method can’t be found for a particular object.

Late binding is important if we have a list of objects to which the same method should be applied, for instance a list of shapes - rectangles, circles, polygons - to be drawn on a screen. If the list is kept in an array shapelist[i], we could then simply write

/* 9 */

for (i=1;i<=N;i++) shapelist[i].draw;

to draw all the objects. This is very similar to Object Pascal, where we would write analogously


for i :=1 to N do shapelist[i].draw;

However, in Object Pascal late binding is always used when early binding can’t be applied. In C++, we have to tell the compiler that a method could be used for late binding by declaring it virtual:

/* 11 */

class TShape {
    TShape* Next, Prev;
    Rect boundRect;
    RgnHandle ShapeRgn;
    virtual void Create(rect *theRect); 
    virtual void Track(rect *oldRect,*newRect);
    virtual void Draw();
    virtual void Erase();
    virtual void Free();

This is the generic definition of a shape for which methods for drawing, erasing, etc. exist, but may or may not be defined in the top class; they may be overridden in the descendant classes, and the actual binding may be known only at run time. The figure illustrates the definition of a class hierarchy of shapes in C++ and in Object Pascal.

This more or less concludes my quick overview of the main characteristics of C++ (of course, all the features of C are still present in the language). Don’t laugh at the mistakes that are probably still in the examples; this happens when one writes programs without a compiler. There are many details I haven’t gone into here; we’ll get to know them in the following columns, with corresponding examples. Forth friends, don’t despair; you’ll get your share soon again, too.


Community Search:
MacTech Search:

Software Updates via MacUpdate

TrailRunner 3.8.834 - Route planning for...
TrailRunner is the perfect companion for runners, bikers, hikers, and all people wandering under the sky. Plan routes on a geographical map. Import GPS or workout recordings and journalize your... Read more
iFFmpeg 6.2.5 - Convert multimedia files...
iFFmpeg is a comprehensive media tool to convert movie, audio and media files between formats. The FFmpeg command line instructions can be very hard to master/understand, so iFFmpeg does all the hard... Read more
DaisyDisk 4.4 - $9.99
DaisyDisk allows you to visualize your disk usage and free up disk space by quickly finding and deleting big unused files. The program scans your disk and displays its content as a sector diagram... Read more
iFFmpeg 6.2.5 - Convert multimedia files...
iFFmpeg is a comprehensive media tool to convert movie, audio and media files between formats. The FFmpeg command line instructions can be very hard to master/understand, so iFFmpeg does all the hard... Read more
DaisyDisk 4.4 - $9.99
DaisyDisk allows you to visualize your disk usage and free up disk space by quickly finding and deleting big unused files. The program scans your disk and displays its content as a sector diagram... Read more
BetterTouchTool 2.07 - Customize Multi-T...
BetterTouchTool adds many new, fully customizable gestures to the Magic Mouse, Multi-Touch MacBook trackpad, and Magic Trackpad. These gestures are customizable: Magic Mouse: Pinch in / out (zoom... Read more
BetterTouchTool 2.071 - Customize Multi-...
BetterTouchTool adds many new, fully customizable gestures to the Magic Mouse, Multi-Touch MacBook trackpad, and Magic Trackpad. These gestures are customizable: Magic Mouse: Pinch in / out (zoom... Read more
BetterTouchTool 2.07 - Customize Multi-T...
BetterTouchTool adds many new, fully customizable gestures to the Magic Mouse, Multi-Touch MacBook trackpad, and Magic Trackpad. These gestures are customizable: Magic Mouse: Pinch in / out (zoom... Read more
PDFpen 8.3.2 - $74.95
PDFpen allows users to easily edit PDF's. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Features... Read more
DiskCatalogMaker 6.5.20 - Catalog your d...
DiskCatalogMaker is a simple disk management tool which catalogs disks. Simple, light-weight, and fast Finder-like intuitive look and feel Super-fast search algorithm Can compress catalog data for... Read more

Latest Forum Discussions

See All

Pokémon GO Generation 2 evolution guide
At long last, Niantic Labs finally unleashed the Generation 2 Pokémon into the wild. Pokémon GO trainers are scrambling to grab up this new set of 80 Pokémon. There are some special new tricks required to catch all of these new beasties, though.... | Read more »
The best new games we played this week
It feels as though the New Year got off to a creaking start as far as mobile games go, but that's changed over the past few weeks. The last few days alone have seen the debut of a number of wonderful games, so we thought we'd take the time to... | Read more »
Recruit more scallywags and discover new...
Get ready to show off your sea legs all over again in Oceans & Empires’ new grand update, which aims to make the act of rising to the role of seven seas ruler even more fresh and appealing, thanks to a richness of new content on both iOS and... | Read more »
Mage the Ascension: Refuge (Games)
Mage the Ascension: Refuge 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: The groundbreaking roleplaying game Mage: The Ascension manifests in our turbulent present with Refuge, an... | Read more »
Vampire: Prelude (Games)
Vampire: Prelude 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: The classic roleplaying game Vampire: The Masquerade returns to digital games with a Prelude of things to come. Experience a... | Read more »
Digby Forever Guide: How to dig to the d...
Digby Forever is a sparkling homage to arcade classics, and while you may be tiring of the number of arcade games being thrown at you, this endless digger finds many ways to stand out from the rest of the pack. The game manages to be challenging... | Read more »
The best sales on the App Store this wee...
It's been quite the week in mobile games, but if the latest releases(there were some pretty darn good ones, in case you missed out) aren't really doing the trick, perhaps some of these discounted games will. Many of these premium games had their... | Read more »
Why the new Fire Emblem Heroes update sh...
It’s exciting to see Nintendo delving into the mobile sphere, regardless of whether it’s to give fans another platform to enjoy their fans or simply a sound business venture. Two of the company's announced mobile games have finally come to... | Read more »
New Fire Emblem Heroes update adds new h...
Fire Emblem Heroes received a sizeable update first thing this morning. The update features a batch of fresh content along with a few updates to the game's systems. [Read more] | Read more »
The Deep Paths (Games)
The Deep Paths 1.0 Device: iOS iPhone Category: Games Price: $3.99, Version: 1.0 (iTunes) Description: 25% off launch sale!!! The Deep Paths: Labyrinth Of Andokost is a first-person, dungeon crawling RPG, with traditional grid-based... | Read more »

Price Scanner via

15-inch Touch Bar MacBook Pros on sale for up...
Amazon has 15″ Touch Bar MacBook Pros in stock today and on sale for up to $150 off MSRP including free shipping: - 15″ 2.7GHz Touch Bar MacBook Pro Space Gray: $2720.38 $79 off MSRP - 15″ 2.7GHz... Read more
Apple’s Education discount saves up to $300 o...
Purchase a new Mac or iPad using Apple’s Education Store and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free: -... Read more
13-inch 1.6GHz/256GB MacBook Air on sale for...
Newegg has the 13″ 1.6GHz/256GB MacBook Air (MMGG2LL/A) on sale for $1029.99 including free shipping. Their price is $170 off MSRP, and it’s the lowest price available for this model. Choose Newegg... Read more
Save up to $600 with Apple refurbished Mac Pr...
Apple has Certified Refurbished Mac Pros available for up to $600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The following... Read more
12-inch 1.1GHz Retina MacBooks on sale for $1...
B&H has 12″ 1.1GHz Retina MacBooks on sale for $150 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 12″ 1.1GHz Space Gray Retina MacBook: $1149 $150 off MSRP - 12″ 1.1GHz... Read more
InTouch Health Expands iOS And Windows Produc...
Specialty telehealth enterprise provider InTouch Health has announced an expanded range of FDA Class I listed medical devices and software solutions for ambulatory, non-acute and non-emergent... Read more
iMobie Airs World’s 1st iCloud Manager with M...
iMobie Inc., an Apple-related software company, announced their newly-updated iPhone manager AnyTrans with exclusive feature to sync and manage contents across multiple iCloud accounts. With it,... Read more
New Proactive Apple Support Professional Cert...
Watchman Monitoring has announced Proactive Support Professional Certification at MacTech Pro. Watchman Monitoring is a premier Proactive Support Software as a Service (SaaS) tool for IT... Read more
13-inch 2.7GHz Retina MacBook Pro on sale for...
B&H Photo has the 2015 13″ 2.7GHz/128GB Retina Apple MacBook Pro on sale for $100 off MSRP. Shipping is free, and B&H charges NY tax only: - 13″ 2.7GHz/128GB Retina MacBook Pro (MF839LL/A): $... Read more
Back in stock: Apple refurbished 13-inch Reti...
Apple has Certified Refurbished 2015 13″ Retina MacBook Pros available for up to $360 off original MSRP, starting at $1099. An Apple one-year warranty is included with each model, and shipping is... Read more

Jobs Board

*Apple* Technician - nfrastructure (United S...
Let’s Work Together Apple Technician This position is based in Portland, ME Life at nfrastructure At nfrastructure, we understand that our success results from our Read more
*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Manager *Apple* Systems Administration - Pu...
Req ID 3315BR Position Title Manager, Apple Systems Administration Job Description The Manager of Apple Systems Administration oversees the administration and Read more
*Apple* Retail - Multiple Positions - Apple,...
SalesSpecialist - Retail Customer Service and SalesTransform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* macOS Systems Integration Administra...
…most exceptional support available in the industry. SCI is seeking an Junior Apple macOS systems integration administrator that will be responsible for providing Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.