Introduction to C++ for iOS Developers: Part 1

In part 1 of this introduction to C++ for iOS developers, you will learn about C++ syntax, inheritance, namespaces, memory management, and more. By Matt Galloway.

Leave a rating/review
Save for later
Share

Hello, C++!

Hello, C++!

Hello, C++!

Are you a master of Objective-C and looking for that next cool thing to learn? Try this article on for size; it introduces you to C++ for iOS developers.

As I’ll explain later, Objective-C works seamlessly with C and C++ code. Therefore, is very helpful for iOS developers to understand C++ for several reasons:

  • Sometimes you might want to use a library that was originally written in C++ in your app.
  • You might want to write part of your app’s code in C++, so it is more easily portable across platforms.
  • Having a good grounding of other languages helps you better understand programming in general.

This article is written for iOS developers who already understand Objective-C. It’s assumed that you already understand how to write Objective-C code and are familiar with basic C concepts such as types, pointers and functions.

Ready to learn some C++? Then dive right in!

Getting Started: A Brief History of Languages

C++ and Objective-C share some common ancestry: they both have their roots firmly planted in good old-fashioned C. This means that they are both “supersets” of the C language. Therefore, in both languages you can use C features alongside the additional features contained in each language.

If you’re familiar with Objective-C, you will likely have a rough understanding of the C++ code you encounter. For example, the scalar types such as int, float and char are present and behave in exactly the same way in both languages.

Both Objective-C and C++ add object-oriented features to C. If you’re unfamiliar with the term “object-oriented”, all you really need to understand is that it means data is represented by objects, which in turn are instances of classes. In fact, C++ was originally called “C with Classes”, which shows the underlying desire to make C++ object-oriented.

“So what about the differences, then?” I hear you ask. Well, the main difference is the approach to the object-oriented features. In C++, a lot of the action happens at compile time, whereas in Objective-C, much more happens at run time. You may already have dabbled with the Objective-C runtime to perform such trickery as method swizzling. In C++ this simply isn’t possible.

C++ also doesn’t have the plethora of introspection and reflection methods that Objective-C has. There’s no way to obtain the class of a C++ object as you would in Objective-C where you simply call the “class” method on an instance. Similarly there is no equivalent of isMemberOfClass or isKindOfClass in C++.

That’s a rough-and-ready introduction to C++ that shows the history and key differences between it and Obejctive-C. The history lesson is done — time to continue with some C++ language features! :]

C++ Classes

The first thing you need to know in any object-oriented language is how to define a class. In Objective-C, you create a header file and an implementation file to define a class. The exact same thing happens in C++; the syntax is also quite familar.

Here’s an example. The following is an Objective-C class:

// MyClass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject
@end

// MyClass.m

#import “MyClass.h”

@implementation MyClass
@end

That should be clear to you as a seasoned iOS developer. But look at the equivalent in C++:

// MyClass.h

class MyClass {
};

// MyClass.cpp

#include “MyClass.h”

/* Nothing else in here */

There are a few distinct differences here. The first thing is that the implementation file in C++ doesn’t have anything in it. That’s because you’ve not declared any methods on the class. As well, an empty class doesn’t even need an @implemenation / @end block like Objective-C does.

In Objective-C, every class will almost always inherit from NSObject. You could create your own root class, which would mean that your class would have no superclass. But it’s unlikely you’ll ever do this unless you’re playing around with the runtime just for fun! This is in contrast to C++ where it’s quite common to create a class with no superclass as in the example above.

Another slight difference is #include versus #import. Objective-C adds the #import preprocessor directive to C. There is no equivalent in C++, so the standard, C-style, #include is used instead. Objective-C’s #import ensures that a file is only included once, but in C++ you must perform this check yourself.

Class Member Variables and Functions

Of course, there’s much more to a class than just declaring it. Just as in Objective-C, in C++ you can add instance variables and methods to a class. You might hear them termed differently in C++ though; they are more commonly called member variables and member functions.

Note: The term “method” is not really used in C++. The distinction is only used in Objective-C where a method is something called via a message dispatch. A function, on the other hand, is something called via a static C-style function call. I’ll explain more about static versus dynamic later in this article.

Note: The term “method” is not really used in C++. The distinction is only used in Objective-C where a method is something called via a message dispatch. A function, on the other hand, is something called via a static C-style function call. I’ll explain more about static versus dynamic later in this article.

So how do you declare member variables and member functions then? Well, here’s an example:

class MyClass {
    int x;
    int y;
    float z;

    void foo();
    void bar();
};

Here there are three member variables and two member functions. But there’s a bit more to it than this in C++, as you can limit the scope of member variables and member functions in C++ and declare them as publicly or privately accessible. This can be used to limit what code can access each variable or function.

Consider the following example:

class MyClass {
  public:
    int x;
    int y;
    void foo();

  private:
    float z;
    void bar();
}

Here, x, y and foo are publicly accessible.This means they can be used outside of the MyClass class. However, z and bar are private. This means they can only be used from within MyClass itself. Member variables are private by default.

While this distinction does exist in Objective-C for instance variables, it’s rarely used. On the other hand, it is not possible in Objective-C to limit a method’s scope. Even if you only declare a method inside the implementation of a class, and don’t expose it in the interface, you technically could still call that method externally.

Methods in Objective-C are public or private only by convention. This is why a lot of developers choose to prefix a private method with something such as “p_” to document the distinction. This is in contrast to C++ where the compiler will throw an error if you try to call a private method from outside the class.

So how do you use a class? It’s very similar to Objective-C, really. You create an instance like so:

MyClass m;
m.x = 10;
m.y = 20;
m.foo();

Simple as that! This creates an instance of MyClass, sets x and y to 10 and 20 respectively and then calls foo on it.

Contributors

Over 300 content creators. Join our team.