OOPortal OOPortal
Programming C++  «Prev  Next»
Lesson 13

Getting started with C++ Summary and Conclusion

In this module you learned some of the differences and similarities between C and C++.
In addition, you started getting familiar with writing C++ code. You learned that:
  1. C++ uses the variable cout and the put to operator << for standard output. Standard input is handled by using the variable cin with the get from operator >>.
  2. C++ uses a preprocessor to handle a set of directives, just like C. However, rather than relying on the #define macro for creating constants, C++ uses the keyword const.
  3. Relational, equality, and logical expressions evaluate as bool expressions, which yield either the bool value false or the bool value true.
  4. C++ allows a safer form of cast than C. You should use static_cast instead of the older C style of casts.
  5. C++ allows declarations to be intermixed with executable statements. The scope of the declaration is the innermost block within which it is found, and the identifier is visible starting at the point at which it is declared. Otherwise, the scope rules are the same as in C.
  6. C++, like C, treats char* as a form of string type. C++ provides the cstring package to assist in the standardization and re-use of string code.
  7. While still allowing the uses of the bracket-pair comment symbols /* */, the preferred comment style of C++ programs is the rest-of-line comment symbol //.

Progress of Abstraction

All programming languages provide abstractions. It can be argued that the complexity of the problems you are able to solve is directly related to the kind and quality of abstraction.
"What is it that you are abstracting?" Assembly language is a small abstraction of the underlying machine. Many so-called imperative[1] languages that followed (such as Fortran, BASIC, and C) were abstractions of assembly language. These languages are big improvements over assembly language, but their primary abstraction still requires you to think in terms of the structure of the computer rather than the structure of the problem you are trying to solve. The programmer must establish the association between the machine model in the solution space[2], and the model of the problem that is actually being solved (in the "problem space", which is the place where the problem exists). The effort required to perform this mapping, and the fact that it is extrinsic to the programming language, produces programs that are difficult to write and expensive to maintain, and as a side effect created the entire "programming methods" industry.

Model the Problem

The alternative to modeling the machine is to model the problem you are trying to solve. Early languages such as LISP and APL chose particular views of the world
  1. All problems are ultimately lists
  2. All problems are algorithmic
PROLOG casts all problems into chains of decisions. Languages have been created for constraintbased programming and for programming exclusively by manipulating graphical symbols. (The latter proved to be too restrictive.) Each of these approaches is a good solution to the particular class of problem they’re designed to solve, but when you step outside of that domain they become awkward.
The object-oriented approach goes a step farther by providing tools for the programmer to represent elements in the problem space. This representation is general enough that the programmer is not constrained to any particular type of problem. We refer to the elements in the problem space and their representations in the solution space as “objects.” (Of course, you will also need other objects that don’t have problem-space analogs.) The idea is that the program is allowed to adapt itself to the lingo of the problem by adding new types of objects, so when you read the code describing the solution, you’re reading words that also express the problem. This is a more flexible and powerful language abstraction than what we’ve had before. Thus, OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run. There’s still a connection back to the computer, though. Each object looks quite a bit like a little computer; it has a state, and it has operations that you can ask it to perform. However, this doesn’t seem like such a bad analogy to objects in the real world; they all have characteristics and behaviors.
#include <iostream>
using namespace std;
int main(){
 cout << "Brown Bear handshake!\n";
 return 0;
}

The first line,
#include <iostream>

tells the compiler to read the file iostream. That file contains the definition for the stream input/output package. Your program performs output onto the screen and therefore requires the services provided in iostream. You must include this file in all programs that read or write text.
By the way, you will see a slightly different syntax, #include <iostream.h>, in many C++ programs. The next line,
using namespace std;

tells the compiler to locate names such as cout in the standard name space. In large programs, it is quite common that different programmers will use the same names to denote different things. They can avoid name conflicts by using separate name spaces. However, for the simple programs that you will be writing in this book, separate name spaces are not necessary.
You will always use the standard name space, and you can simply add the directive using namespace std; at the top of every program that you write, just below the #include directives. Name spaces are a standard feature of C++, and your compiler should support them.
The construction
int main(){
...
return 0;
}

defines a function called main. A function is a collection of programming instructions that carry out a particular task. Every C++ program must have a main function. The instructions or statements in the body of the main function, the statements inside the curly braces {} are executed one by one. Note that each statement ends in a semicolon.
cout << "Hello, World!\n";
return 0;

A sequence of characters enclosed in quotation marks
"Hello, World!\n"

is called a string.

Basic Cplus Quiz

Click the Quiz link below to take a multiple-choice quiz covering the topics presented in this module.
Basic Cplus - Quiz

[1] imperative language: In computer science, an imperative language is a programming language that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates.

[2]solution space: i) Represents the place where you are modeling the problem, such as a computer. ii) The space of all potential solutions for a problem.