Topic 6: Overview of C++ Non-OO Features
Synopsis
What is C++
- An object-oriented programming language
- ...invented by Dr Bjarne Stroustrup at AT&T labs
- ...evolved from the C programming language
- ...as a superset of C (so much of the syntax and semantics is the same)
- ...developed and tested by many people over 2 decades
- A "hybrid" language (not purely OO)
- Efficient
- Portable
- Powerful
- Arcane
- In demand
- Difficult to use well!
A Brief History of C++
-
"C With Classes¤"
-
Version 1.0 (classes¤,
inheritance, overloading, polymorphism¤)
-
Version 2.0 (multiple inheritance¤)
-
ANSI/ISO Standardization begins
-
Version 3.0 (genericity¤,
exceptions¤)
-
Version 4.0 (more polymorphism¤,
better modularity¤)
-
First draft ANSI/ISO Standard released
-
ANSI/ISO Standard ratified
OO features of C++
-
Classes¤
-
Single- and multiple-inheritance
-
Generic¤
classes¤
("templates")
-
Polymorphism¤
("virtual functions¤")
-
Function and operator overloading¤
-
Modularity¤
mechanism ("namespaces")
-
No persistence¤
mechanism
Non-OO features of C++
-
New style of comment
-
Strict(er) typing¤
-
New I/O mechanism
-
New dynamic memory mechanism
-
New boolean type
-
References¤
-
Enum types
New style of comment
-
In C, all comments delimited by /*...*/
-
In C++, a second type of comment available: //...
-
// comments are terminated by the end of the current line.
For example:
int coreTemp; // LAST RECORDED REACTOR CORE TEMPERATURE
int critTemp; // CRITICAL CORE TEMPERATURE - DON'T EXCEED!
while (coreTemp < critTemp) // EVERYTHING'S OKAY
{
eatDonuts(); // STANDARD OPERATING PROCEDURE
}
// SHOULD NEVER GET HERE (WELL, NOT TWICE :-)
panic();
flee();
hide();
glow();
Hereafter, C code examples will have only old-style comments, and C++ code
examples will have only new-style comments.
Strict(er) typing¤
-
In C the declaration int f() means
"f is
a function that returns an int and may take zero or more
arguments".
-
In C++ the declaration int f() means
"f
is a function that returns an int and takes exactly zero
arguments".
-
In other words, the parameters of a C++ function must be explicitly declared.
-
To get the "zero or more arguments" behaviour¤
you need to use the '...' symbol:
f(...),
printf(char*,
...),
etc.
New I/O mechanism
-
In C, I/O is function-based: get(), scanf(),
printf(), etc.
-
In C++, I/O is operator-based: << and >>
-
Input to all inbuilt types is via the >> operator:
int i;
double d;
char s[1000];
cin >> i >> d >> s; // GET VALUES FOR i, THEN d, THEN s
Output from all inbuilt types is via the <<
operator:
// PRINT OUT i, COMMA, d, COMMA, s, "\n"
cout << i << ',' << d << ',' << s << endl;
cin and cout are special predefined "stream"
objects (a bit like stdin and stdout).
There's also a cerr object (like stdderr).
endl is a special object that causes a newline to be printed
and the output buffer to be flushed.
How come we don't have to specify the types explicitly (like in printf()
and scanf()?
The overloading mechanism takes care of working out which version of the
'<<' or '>>' operator to call!
New dynamic memory mechanism
-
In C, allocate new memory via the malloc() and calloc()
functions, and deallocated via free():
Node* newnode = (Node*) malloc(sizeof(Node));
char* str = (char*) calloc(100,sizeof(char));
/* ...AND LATER... */
free(newnode);
free(str);
In C++, new memory is allocated via the new and new[]
operators, and deallocated via the delete and delete[]
operators:
Node* newnode = new Node;
char* str = new char[100];
// ...AND LATER...
delete newnode;
delete[] str;
Can't mix scalar and array versions of new and delete
new and delete have other cool features
that we'll see later
New boolean type
-
In C, logical values are represented by zero/not-zero values
-
In C++, there's a proper boolean type (bool) with exactly
two values (true and false)
-
For backwards compatibility with C, there's automatic conversion between
bool and int.
References¤
-
A reference¤
is a name for something.
-
In C, every object has at most 1 reference¤
(i.e. its name).
-
In C++, extra references¤
can be defined for an object:
int myVariableWithALongName = 0; // DEFINE A VARIABLE
int& v = myVariableWithALongName; // DEFINE A REFERENCE
// TO THE PREVIOUS
// VARIABLE
Now any use of v refers to the object declared as myVariableWithALongName:
v = 100;
cout << myVariableWithALongName; // PRINTS: 100
References¤
are particularly useful as function parameters:
void swap(int& a, int& b)
{
int tmp = a;
a = b;
b = tmp;
}
swap(x,y);
Within the swap function, a is a reference¤
to (i.e. another name for) whatever the first argument of swap
was. Likewise b is another name for whatever the second
argument was.
Enum types
-
In C, enums just create new named constant values:
enum {good, bad, ugly};
int face = ugly;
face = 9; /* OOPS! */
In C++, enums can be named and define a new type which only accepts values
in the range specified:
enum FacialType { good, bad, ugly };
FacialType face = ugly;
face = 9; // ERROR: NOT IN RANGE OF FacialType VALUES
Reading
- Lippman & Lajoie
- Some new, some revision: Sections 3.5 to 3.15 and 7.5 to 7.9, Chapter 8.
- Stroustrup
- Some new, some revision: Chapters 4 and 5.