Professional Documents
Culture Documents
11 CPP
11 CPP
A bit of C++
P2 — A bit of C++
Roadmap
> C++ vs C
> C++ vs Java
> References vs pointers
> C++ classes: Orthodox Canonical Form
> A quick look at STL — The Standard Template Library
© O. Nierstrasz 2
P2 — A bit of C++
Roadmap
> C++ vs C
> C++ vs Java
> References vs pointers
> C++ classes: Orthodox Canonical Form
> A quick look at STL — The Standard Template Library
© O. Nierstrasz 3
P2 — A bit of C++
© O. Nierstrasz 4
P2 — A bit of C++
© O. Nierstrasz 11.5
P2 — A bit of C++
What is C?
© O. Nierstrasz 6
P2 — A bit of C++
My first C Program
Include standard io
declarations
A preprocessor directive
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
Write to return 0;
standard
output
} char array
Indicate correct termination
© O. Nierstrasz 7
P2 — A bit of C++
What is C++?
A “better C” (http://www.research.att.com/~bs/C++.html)
that supports:
> Systems programming
> Object-oriented programming (classes & inheritance)
> Programming-in-the-large (namespaces, exceptions)
> Generic programming (templates)
> Reuse (large class & template libraries)
© O. Nierstrasz 8
P2 — A bit of C++
C++ vs C
© O. Nierstrasz 9
P2 — A bit of C++
Roadmap
> C++ vs C
> C++ vs Java
> References vs pointers
> C++ classes: Orthodox Canonical Form
> A quick look at STL — The Standard Template Library
© O. Nierstrasz 10
P2 — A bit of C++
package p2;
// My first Java program!
public class HelloMain {
public static void main(String[] args) {
System.out.println("hello world!");
return 0;
}
}
© O. Nierstrasz 11
P2 — A bit of C++
© O. Nierstrasz 13
P2 — A bit of C++
© O. Nierstrasz 14
P2 — A bit of C++
C++ Features
Classes as structs
C with Classes Inheritance; virtual functions
Inline functions
Strong typing; function prototypes
C++ 1.0 (1985)
new and delete operators
Local classes; protected members
C++ 2.0
Multiple inheritance
Templates
C++ 3.0
Exception handling
Namespaces
ANSI C++ (1998)
RTTI (Runtime Type Information)
© O. Nierstrasz 15
P2 — A bit of C++
© O. Nierstrasz 16
P2 — A bit of C++
© O. Nierstrasz 17
P2 — A bit of C++
New Keywords
© O. Nierstrasz
(see http://www.glenmccl.com/glos.htm)
18
P2 — A bit of C++
Roadmap
> C++ vs C
> C++ vs Java
> References vs pointers
> C++ classes: Orthodox Canonical Form
> A quick look at STL — The Standard Template Library
© O. Nierstrasz 19
P2 — A bit of C++
Memory Layout
© O. Nierstrasz 20
P2 — A bit of C++
Pointers in C++
int i;
int *iPtr; // a pointer to an integer
© O. Nierstrasz 21
P2 — A bit of C++
References
References vs Pointers
© O. Nierstrasz 23
P2 — A bit of C++
C++ Classes
© O. Nierstrasz 24
P2 — A bit of C++
© O. Nierstrasz 25
P2 — A bit of C++
void finish(MyClass& b) {
delete &b; // need pointer to b
}
#include "MyClass.h”
finish(start()); create d
} create a
return 0; create b
destroy a
using namespace std; destroy b
int main (int argc, char **argv) { destroy d
MyClass aClass("d");
© O. Nierstrasz 26
P2 — A bit of C++
Roadmap
> C++ vs C
> C++ vs Java
> References vs pointers
> C++ classes: Orthodox Canonical Form
> A quick look at STL — The Standard Template Library
© O. Nierstrasz 27
P2 — A bit of C++
class myClass {
public:
myClass(void); // default constructor
myClass(const myClass& copy); // copy constructor
... // other constructors
~myClass(void); // destructor
myClass& operator=(const myClass&); // assignment
... // other public member functions
private:
...
};
© O. Nierstrasz 28
P2 — A bit of C++
Why OCF?
If you don’t define these four member functions, C++ will generate
them:
© O. Nierstrasz 29
P2 — A bit of C++
© O. Nierstrasz 30
P2 — A bit of C++
A Simple String.h
© O. Nierstrasz 31
P2 — A bit of C++
Default Constructors
© O. Nierstrasz 32
P2 — A bit of C++
Destructors
© O. Nierstrasz 33
P2 — A bit of C++
Copy Constructors
> The abstraction boundary is a class, not an object. Within a class, all
private members are visible (as is copy._s)
© O. Nierstrasz 35
P2 — A bit of C++
Other Constructors
String::String(const char* s)
{
become(s);
}
© O. Nierstrasz 36
P2 — A bit of C++
Assignment Operators
> Return String& rather than void so the result can be used in an
expression
> Return String& rather than String so the result won’t be copied!
> this is a pseudo-variable whose value is a pointer to the current
object
— so *this is the value of the current object, which is returned by reference
© O. Nierstrasz 37
P2 — A bit of C++
Implicit Conversion
© O. Nierstrasz 39
P2 — A bit of C++
Overloadable Operators
+ - * / % ^ & |
+= -= /= %= ^= &= |= *=
<<= >>= [] () -> ->* new delete
© O. Nierstrasz 40
P2 — A bit of C++
Friends
But:
— It can’t be a member function of ostream, since we can’t
extend the standard library.
— It can’t be a member function of String since the target is cout.
— But it must have access to String’s private data
© O. Nierstrasz 41
P2 — A bit of C++
Friends ...
ostream&
operator<<(ostream& outStream, const String& s)
And define: {
return outStream << s._s;
}
© O. Nierstrasz 42
P2 — A bit of C++
Roadmap
> C++ vs C
> C++ vs Java
> References vs pointers
> C++ classes: Orthodox Canonical Form
> A quick look at STL — The Standard Template
Library
© O. Nierstrasz 43
P2 — A bit of C++
© O. Nierstrasz 44
P2 — A bit of C++
#include <iostream>
#include <stack> // STL stacks
#include <string> // Standard strings
void rev(void)
{
typedef stack<string> IOStack; // instantiate the template
IOStack ioStack; // instantiate the template class
string buf;
© O. Nierstrasz 45
P2 — A bit of C++
© O. Nierstrasz 46
P2 — A bit of C++
© O. Nierstrasz 47
P2 — A bit of C++
© O. Nierstrasz 48
P2 — A bit of C++
License
http://creativecommons.org/licenses/by-sa/2.5/
Attribution-ShareAlike 2.5
You are free:
• to copy, distribute, display, and perform the work
• to make derivative works
• to make commercial use of the work
Attribution. You must attribute the work in the manner specified by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting
work only under a license identical to this one.
• For any reuse or distribution, you must make clear to others the license terms of this work.
• Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.
© O. Nierstrasz 49