CodeMusings

Kick back and get started at your own pace.

Skip to Content

Overview

C++ was developed by Bjarne Stroustrup in 1979. It is a multi-paradigm, weakly typed, general-purpose programming language. C++ does add in facilities for stronger typing than C. C++ has grown in popularity over the years, and is used in about as many environments as C [Stroustrup].

Hello, world!

C++ provides a richer set of facilities for performing tasks than C. Here, the left-shift operator is overloaded to write data to an output stream.

hello.cpp

  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std::cout << "Hello, world!\n";
  6. return 0;
  7. }

History

Stroustrup maxin' and relaxin'

And on the seventh day, Stroustrup finished the work that he had done, and he rested on the seventh day from all the work he had done.

When Stroustrup first began development on what would become C++ in 1979, it was initially known as "C with Classes." It was developed internally at AT&T Bell Labs [Stroustrup, FAQ].

Stroustrup intended to write efficient systems code in the styles encouraged by Simula67 [Stroustrup]. As C was a solid base from which to start, he added the facilities for stronger type checking, data abstraction features, and object-oriented design to the language.

Design

Though C and C++ share a large subset, the two are different languages. C++ is at its best when you utilize the power the language affords, rather than as C with, well, classes.

Stroustrup used his Ph.D. experience to formulate a set of guidelines to aid in the design and "spirit" of the C++ language as it grew [Stroustrup, 3].

Type System

Stroustrup intended to make the typing system stronger in C++. This means the compiler is able to catch more errors as it parses the code. However, in order to meet the needs of systems programmers, there still exists the facilities for explicit type conversions, or casts.

C++ offers a template system to build more reusable interfaces. It allows the programmer to pass types as a parameter when instantiating the class.

Consider building an interface for an array. You ideally want to allow the array to contain data of any arbitrary type. The abstract idea of an array has the same interface, regardless of what's inside it.

C++ Template Example

  1. template<typename T>
  2. class Array {
  3. // these are internal data fields that represent our array data
  4. T* data;
  5. size_t size_;
  6.  
  7. // private functions to handle memory allocation
  8. void alloc(size_t size);
  9. void dealloc();
  10.  
  11. // this is the start of the public interface of the class, or the part
  12. // that other programmers will use to control the behavior of an
  13. // "Array" object
  14. public:
  15. // These are constructors; they're called as an object is created.
  16. // They're responsible for establishing reasonable default values
  17. // for data fields, allocating memory, etc.
  18. Array() : data(NULL), size_(0);
  19. Array(T* data, size_t len) : data(NULL), size_(len);
  20.  
  21. // C++ supports destructors, which are functions that
  22. // automatically get called as an object goes out of scope.
  23. ~Array();
  24.  
  25. // we have an accessor method here to allow other programmers to
  26. // access data
  27. size_t length() const;
  28.  
  29. // C++ allows creating custom behavior for certain operators.
  30. // Here, we allow code like "some_array[index]" to work
  31. // intuitively.
  32. T& operator[] (size_t i);
  33. };

Good for Beginners?

The C++ programming language is a general-purpose programming language, and you will generally find implementations anywhere you go. It has the benefit of including many convenient libraries and algorithms within the standard library, so it can be easier to get started when you don't have to manually do bookkeeping for dynamically allocated memory.

If learning object-oriented design is either a requirement for you, or something that you just find interesting, C++ provides the facilities to write code in that paradigm, as well as others.

The C++ programming language also has similar learning requirements to C, in that you should have a good idea about how to use the tools involved in building the programs on your system. Also like C, it's a good idea to keep in mind that the C++ language is separate from the implementations of it.

References

  1. ^ - Stroustrup, Bjarne. Bjarne Stroustrup FAQ. When was C++ Invented? Accessed: 3 Oct 2014.
  2. ^ - Stroustrup, Bjarne. Evolving a Language in and for the Real World: C++ 1991-2006. Accessed: 3 Oct 2014.