Session 1: Introduction

This module: more object-oriented programming, in C++

Assuming that you are currently a reasonably skillful Java programmer, by the end of this course you should be able to

This session: non-OO programming in C++

This session introduces the philosophy of C++, and some simple non-OO programs.

We will touch on the following features of C++:

All will be explored in greater detail later.

A bit of language history

1960
Algol 60: block structure, static typing

1967
Simula: Algol plus object-orientation (for simulation)

1970
C: statically typed procedural language with low-level features

1972
Smalltalk: object-orientation (for graphical interfaces), no static types

1985
C++: C plus object-oriented features and (later) genericity

1995
Java: C++ greatly simplified

Java design criteria

C++ design criteria

A small C++ program

    #include <iostream>

    using namespace std;

    int main() {
            cout << "Hello world!\n";
            return 0;
    }

Accessing names from standard libraries

Text output

    cout << "Hello world!\n";

Input and output

    int i;
    cout << "Type a number: ";
    cin >> i;
    cout << i << " times 3 is " << (i*3) << '\n';

Strings

    #include <string>
The standard library provides a string type:
    string s = "fred";
    cout << s;
    cin >> s;    // reads a word
The + operator is overloaded on strings:
    s = s + " and bill";
    s = s + ',';
So are +=, ==, <, etc.

Unlike in Java, strings are modifiable:

    s.erase();   // makes s empty

Breaking the input into words

    #include <string>
    #include <iostream>
    using namespace std;

    int main() {
            string s;
            while (cin >> s)
                    cout << s << '\n';
            return 0;
    }

Vectors

    #include <vector>
C++ has arrays, but we'll use vectors instead:
    vector<int> vi(5);  // vector of 5 ints
    vector<string> si;  // empty vector of strings
Vectors can be accessed just like arrays:
    vi[1] = x;
    vi[2] = vi[1] + 3;
Vectors can also be extended:
    si.push_back(s);
The current length of si is si.size()

Language notes

Note: the syntax looks like Java, but the meaning is very different.

Initialization vs. assignment

Initialization of variables:
    string s1;
    string s2 = "bill";
Objects are always initialized; variables of primitive type aren't.

Assignment replaces an existing value:

    s1 = s2;
Initialization defines a new variable:
    string s3 = s2;

Passing parameters by value

Formal parameters are new variables, initialized from actual parameters.

        void f(int i) {
                i = i + 5;
        }

        void g() {
                int j = 3;
                f(j);   // no effect on j
        }

Parameter passing in Java

Limitations of value parameters

Passing parameters by reference

A reference formal parameter is another name (an alias) for the actual parameter.

    void f(int &i) {
            i = i + 5;
    }

    void g() {
            int j = 3;
            f(j);   // j is updated
    }
Note: There is no relationship to Java's references.

Passing large values by reference

Reference parameters are also used to avoid copying large values:

    int last(vector<int> &v) {
            return v[v.size() - 1];
    }

    void g() {
            vector<int> x(100);
            ...
            int n = last(x);   // don't copy x
    }

Constant parameters

We can indicate that the function doesn't change the parameter with the keyword const:

    int last(const vector<int> &v) {
            return v[v.size() - 1];
    }

    void g() {
            vector<int> x(100);
            ...
            int n = last(x);   // don't copy x
    }
This makes programs safer, and can help the compiler.

Constants

References

An example function (from iostream)

istream& getline(istream& in, string &s) {
        s.erase();
        char c;
        while (in.get(c) && c != '\n')
                s += c;
        return in;
}
Note that

Prefixing lines with their lengths

#include <iostream>
#include <string>

using namespace std;

int main() {
        string s;
        while (getline(cin, s))
                cout << s.size() << '\t' << s << '\n';
        return 0;
}

Next session