4. Background: History of C
◦ In 1970, Bell Labs needed to port Unix from PDP-7 to PDP-11
◦ Unix, the OS, was written in assembly until those days
◦ The developers wanted to use a language, but there was no suitable one
◦ So Ritchie made one and named it C in 1972, and wrote K&R C book in 1978
◦ The language feature has been largely fixed since then
◦ C became the language of choice for system programming
◦ People made OS using C (DOS → Windows, BSD → MacOS → iOS, Linux → Android)
◦ Because it is so simple and old, it is so fast
Ken Thompson and Dennis Ritchie PDP-7 (1965) PDP-11 (1970) K&R C (1978)
12. C++ as a Modernization of C
Created by Bjarne Stroustrup in 1983, ideas originating from his PhD thesis
◦ Support Object-Oriented Programming for large-scale software development
◦ Have to be fast in speed, to be used for practical purposes
◦ Mostly a superset of C, because general-purpose, fast, portable, and widely used
Philosophy
◦ Driven by real-world problems and should be useful immediately
◦ User-create types need to have the same performance as built-in types
◦ Should fully support any programming style that programmers want
◦ Allowing a useful feature is more important than preventing misuse of it
C++ actively adds support for new features and paradigms
◦ While C remains an almost unchanging, minimal, portable, and efficient toolset
◦ A new Draft International Standard in March 2017, to become C++17
17. Hello World
◦ C++ Headers are without the .h extension
◦ using namespace std;
◦ A namespace that contains all features of the C++ standard library
◦ No need to prefix functions with library names
◦ <iostream> provides console input/output via cin and cout
◦ Also endl, which outputs a newline and flushes the stream
◦ No need to use the format string
◦ The shift operator << has a different meaning here – Operator Overloading
39. struct vs class
The only difference is the default access level
◦ Members of struct are public by default
◦ Members of class are private by default
In C, struct was just a data bundle
◦ That can be manipulated from anywhere
C++ added methods to struct for class
◦ struct members have to be public by default
◦ To retain compatibility with C
◦ But C++ liked encapsulation so much!
◦ So they made class members private by default
Rule of thumb
◦ Use struct for simple data storage
◦ May have minimal logic on data validation, etc.
◦ Use class for anything more than that
41. Inheritance – Pet Example
talk() inherited to Dog and Cat:
sound() overridden in Dog and Cat
If base class has a constructor
◦ Must call when constructing derived
◦ Unless base constructor is zero-argument
Use virtual method if inheriting
◦ Required for polymorphism
42. Polymorphism
The pets example continued:
◦ void print(Pet &) does not know anything
about the two derived classes
◦ Compiler does not consider the derived classes at all,
while compiling this function
◦ Still the corresponding sounds gets printed.
◦ Because the exact method to be called is determined
during the run-time
◦ virtual keyword is required for this process
◦ Single interface talk() can invoke many different
implementations depending on the type
◦ Thus called “polymorphic”
43. Pure Virtual Methods
◦ We didn’t need the function returning “???”, and we can just put =0 instead
◦ This method has no definition, and is called a pure virtual method
◦ A class with a pure virtual method is called an abstract class, as opposed to a concrete class
◦ An abstract class cannot be instantiated directly, and derived types must be used
◦ A derived type must override all pure virtual methods, otherwise it becomes abstract too
45. Destructor
Class may manage resources
◦ e.g. files, connections to web
◦ malloc’ed memory buffer
◦ Resource handles, like SNDFILE *
◦ that have to be cleaned up / closed
Destructor is responsible of
◦ Safely closing any open resources
Called when object’s lifetime ends
◦ A local variable gets out of scope
◦ An object is delete’ed
◦ Program exits (global variables)
51. Containers
There are a lot of data structures made for storing collection of data
◦ Sequence containers
◦ vector, array, list, forward_list
◦ Each has different efficiency for random access, insertion, and deletion
◦ Sequence container adapters
◦ stack, queue, priority_queue
◦ Ordered associative containers
◦ set, map, multiset, multimap
◦ Unordered associative (hash) containers
◦ unordered_set, unordered_map, unordered_multiset, unordered_multimap
In C, had to go with own implementation always
◦ There is no de-facto standard containers library or something close to that
◦ Partly due to the lack of templates