Skip to content

Instantly share code, notes, and snippets.

@gatlin
Created April 26, 2013 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatlin/5469581 to your computer and use it in GitHub Desktop.
Save gatlin/5469581 to your computer and use it in GitHub Desktop.
A quick overview of creating simple classes in C++, with example usage of abstract base classes and the iostream library.
#include <iostream>
using namespace std;
/***
* Humanoid
* An abstract base class for other lifeforms
*/
class Humanoid {
protected:
string name; // protected entities all Humanoids have
int age; // unless otherwise specified, all members are private
// public -> anything can access it
// protected -> accessible by its own methods and the methods of children
// private -> accessible *only* by its own methods
public:
// a "pure virtual function" is a function specified by the following
// pattern: virtual <type> <name>() = 0;
// The reason why doesn't fit into a comment :)
// http://is.gd/OFk5pU is a good reference
virtual string getName() = 0;
int getAge() { return this->age; }
Humanoid (string n, int a)
:name(n), age(a) {}
};
/***
* Vulcan
* A sub-class of Humanoid. Exercise: why "public Humanoid" ?
*/
class Vulcan : public Humanoid {
public:
string getName();
Vulcan (string n, int a)
: Humanoid(n, a) {}
};
/***
* Human
* Another sub-class of Humanoid.
*/
class Human : public Humanoid {
public:
string getName();
Human (string n, int a)
: Humanoid(n, a) {}
};
// Vulcan's implementation of getName()
string Vulcan::getName() {
return "unpronounceable by humans";
}
string Human::getName() {
return this->name;
}
int main (int argc, char **argv) {
Human h("kirk",31);
Vulcan v("spock",34);
cout << "The human's name is "
<< h.getName() << " and it is "
<< h.getAge() << " years old." << endl;
cout << "The vulcan's name is "
<< v.getName() << " and it is "
<< v.getAge() << " years old." << endl;
return 0;
}
@gatlin
Copy link
Author

gatlin commented Apr 26, 2013

To compile with g++:

$> g++ humanoid.cpp
$> ./a.out
The human's name is kirk and it is 31 years old.
The vulcan's name is unpronounceable by humans and it is 34 years old.

@gatlin
Copy link
Author

gatlin commented Apr 26, 2013

If you ask anyone but hardware engineers, C++ is a low-level language. Its primary concern is efficiency and control over the hardware and is very much an "engineering" language; fancy-pants blue-blooded high level programming constructs don't map optimally to current computer architectures so while of course they'll run, they'll do extra work.

C++ is a machine-control language. It happens to borrow ideas from computer science, obviously, because it is a language that can control a specific type of Turing machine. Most of the following points apply to most languages but C++ is especially complex.

In CS we talk about types and values in terms of sets (types), elements (values), and relations; in C++ the "type" just tells the OS how much RAM to reserve (for the most part).

In CS we talk about data structures like lists and trees and optimal ways to sort them and search through them mathematically as they elegantly represent many problems in the abstract; C++ has arrays which are just contiguous blocks of RAM, and there are language features that let you implement lists and trees on top of them ... haphazardly.

CS will inform you how to approach a computation whether you have a mainframe, an iPhone, an abacus, or just your own mind; C++ kind of only makes sense on von Neumann architectures.

In CS "function" means what it does in mathematics: a mapping from one set of values to another such that an input always maps to the same output (among other things); in C++ a function to retrieve, eg Twitter streams can return different tweets every time it's called. That's not a function!

And what C++ calls "object oriented programming" bears little resemblance to what Alan kay meant when he invented the idea.

So learn it but you should bear in mind that CS and programming are not always the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment