Arrays and stringsTopC++Why C++:

Why C++:

  1. It is a useful language that is still quite popular and generally provides faster execution time than Java. In particular it is used in several advanced CS courses in Claremont.
  2. Because it is a lower-level language than Java, it requires you to have a better understanding of the underlying implementation of the language and memory management. While this will be painful (and very error-prone), it will help you better understand what is happening while your program is executing.

C++ was originally designed by Bjorne Stroustrup in the early to mid-80's as an extension of C with features for object-oriented programming (he learned Simula 67 as a college students and wanted to keep using it at Bell Labs).

C was designed as a high-level (relatively) portable assembly language which would make it easy to write operating systems (e.g., UNIX). It had very few protections (e.g. type checking was very weak).

The C++ language design goals were:

Explicitly hybrid language - C w/abstraction as well as OO language.

Still has lots of security holes.

C makes it easy to shoot yourself in the foot. In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg. Stroustrup

A VERY LARGE language. Most programmers learn a subset. I will try to teach you a subset that allows you to do Java programming in C++. In particular we will try not to worry about procedural programming in C++.

Note that Java benefitted from being defined 10 years after C++. It provides major advancements in safety (and simplicity). I will mention these many times as we talk about C++.

Here is your first program:

#include <iostream>  // provides access to cin and cout
using namespace std;  // like import java.lang.*;

int main() {
  cout << "Hello world" << endl;
  int x;
  cout << "Enter a value of x" << endl;
  cin >> x;
  cout << "Your value was " << x << endl;
  return 0;     // correct termination
}

A slightly more interesting program is average. It includes a method with a parameter and a for loop. Notice the cast to double. You can also write double(sum), but the other form is preferred to make it stand out.

Note that #define provides macro expansion at compile time. That is, it copies the code into the source file before compiling. It works fine with simple things like constants, but can cause serious problems with fancier expressions. Use it with caution!

Unfortunately the program does not do error checking as it will treat characters input as though they were ints. In fact, just typing the character "a" will make it think you have typed in 3 numbers and it wil terminate the loop, printing out a very strange answer, without warning that there was a problem. This is a "feature" of C++ that you will have to get used to.

The following program illustrates some of the error-handling capabilities of C++ if you make the effort. Notice that all parts are necessary. cin.fail() returns true if something goes wrong with the input, cin.clear() clears the error condition (which would otherwise stay on), while cin >> junk throws away any excess garbage still on the line (that would otherwise be read during the next read).

Unfortunately, that still does not take care of all errors as typing in 3abc results in reading "3" and then getting an error with "abc".

Other warnings:

  1. bool (not boolean!): beware that zero can be used as false with any non-zero value representing true. Thus,
        if (x=0){...}else{...}
    
    will always execute the "else" portion.
  2. C++ will not check to make sure you initialize variables before using them.
  3. C++ will allow you to neglect returning a value from a non-void method and will just return junk.
  4. C++ processes functions/methods in the order they are written. Thus if main() occurred before getScoresAndAverage(...) in the above examples, you would get a compiler error. You can tell the system that a definition is coming by providing a prototype, which looks like the definition of a method in an interface:
        double getScoresAndAverage(int numScores);
        
    The prototype should occur before any uses of the function. The actual function definition may then come anywhere later in the code. This is necessary for mutually recursive functions.

New C++ feature: C++ allows default parameters.

    int find(char ch, int start = 0);

Can just call find(`a') and it will assume value of start is 0.


Arrays and stringsTopC++Why C++: