CS107 Study Guide

Unit 1: Introduction and Setup

1a. Describe the basic history of C++

  • What is C++?
  • How did C++ emerge as a programming language?
  • What are some distinguishing features of the C++ programming language?

The history of computers is generally traced back to Charles Babbage who designed a mechanical computer called the Analytical Engine around 1837 (although it was never actually finished). It had its own set of computer instructions and the first known computer program was written for this system. The history of modern programming languages dates back to the 1940s when the first electronic digital computer, ENIAC was constructed. Over the next decade computation gained popularity and the first commercially available high level computer language FORTRAN was developed in 1956. Many programming paradigms such as procedural programming and object oriented programming also arose as computation became an integral part of society. Procedural programming uses procedures and functions in order to accomplish a given task. In object oriented programming, classes are defined using data attributes and methods that describe aspects of the problem being solved. Objects that apply class methods from the class definitions are then instantiated within a program.

C++ is a general-purpose programming language that supports procedural, object-oriented, and functional programming paradigms. Bjarne Stroustrup developed C++ during the 1980s to enhance the C programming language which was originally designed in order to develop the Unix operating system. Specifically, the C programming is a high level language that can interact with a computer system's hardware. Bjarne Stroustrup was inspired by the object oriented aspects of the language Simula. He therefore created C++ by adding object oriented capabilities similar to Simula such as class definitions, methods, object instantiation, and operator overloading to the C programming language.

C++ retains a lot of syntax from the C programming language and therefore can be used to interact at the low level with system hardware. On the object oriented side of programming, C++ has much in common with the Java programming language. This makes C++ a very versatile programming language with a range of applications from low level hardware implementations to higher level software application development.

To review, see Introduction to C++.


1b. Set up and create a simple C++ project using Eclipse CDT

  • What is Eclipse?
  • What is the most common form of a filename for a C++ source file?
  • What does it mean to build a project?

The Eclipse C++ Development Tool (CDT) is an integrated development environment (IDE) for writing, compiling, linking, building, and running C++ programs. The first level to Eclipse is simply a text editor that allows you to type C++ instructions into a source file. This text file is most commonly named with the file extension '.cpp' to indicate it is a C++ program. Within the IDE, you construct a project which houses C++ source code file(s) relevant to your project.

Once you are confident that you have completed writing the program within your source file, you can use Eclipse to compile the text file to low level object code that your computer can recognize. If there are several components to your project, Eclipse can link them together to form a complete executable file. This process of compiling and linking project components together to form an executable program is called 'building'. Once the build process is successful, the program can be run within the Eclipse CDT.

To review, see


1c. Explain the meaning of simple C++ commands

  • What does the include directive accomplish?
  • What is a namespace?
  • What is main?

Certain features are found in most C++ programs and will be applied over and over again. For instance, there are directives that tell a compiler where to find certain components referred to, but not contained, within your program. Under these circumstances the '#include' directive refers the compiler to libraries outside of your program. When invoked, the compiler will take code from the library and place it directly into your program. For example,

#include <iostream>

refers to a component of the C++ standard library necessary for basic input and output. Namespaces are used in C++ to avoid confusion when referring to functions with the same name from different sources. The using command then helps you to avoid having to use a namespace prefix to refer to a component of a library each time you want to apply the component. For example,

using namespace std;

avoids having to constantly use the prefix std::cout from the standard library when using the cout instruction for screen output.

The starting point of all C++ programs is the main function, a special function in all C++ programs. This function is called when a program is run. The execution of all C++ programs begins with the main function, independent of its location within the code. Typical syntax one sees for starting a C++ program might look like

int main()
{
#your program code goes here
}

The syntax for functions is covered in Unit 2. For now, you should just understand what syntax should be in place when starting to write a C++ program. Pay close attention to curly brackets { } which are used to enclose blocks of code.

To review, see Understanding the "Hello World" Program.


1d. Use cout and cin objects effectively

  • Why are the cout and cin commands important?
  • What syntax is used for invoking cout?
  • What syntax is used for invoking cin?

Fundamental to every programming language is the ability to perform input and output. The most basic form of input is keyboard input. Output usually comes in the form of screen output. In order to allow for keyboard input and screen output, the computer system must provide for interactions between various levels of hardware and the operating system. A high level language must simplify all these interactions for the user. C++ does this through the cout and cin commands.

With iostream included and the std namespace invoked, the syntax for input and output is greatly simplified. Screen output can be accomplished with a command such as

cout << "hello" ;

Keyboard input can be accomplished with a command such as

int age;
cin >> age ;

Given the vastness and object oriented nature of the standard library, there are many variations on this theme explored throughout the course.

To review, see Taking User Input.


1e. Declare and use variables

  • What is a variable?
  • What are some common variable types?
  • What is the difference between variable declaration and variable initialization?

A variable in a programming language holds and remembers a piece of data intended to be used within a program. The variable value can be changed and updated to a new value anytime a programmer chooses to do so. C++ requires that all variables be declared as a specific type so that the compiler knows how much memory to allocate to hold the variable data. Some common variable types applied at the introductory level are int (integer), float (single precision floating point), double (double precision floating point), and char (character data).

A variable can be declared, but not initialized to a specific value OR can be simultaneously declared and initialized. For example, the following commands separately declare and then initialize the variable x as an integer

int x;
x=5;

On the other hand, for the sake of brevity and flexibility, it is also possible to perform these operations with one command

int x=5;

The declaration tells the compiler to set aside the correct number of bytes to hold an integer. The initialization step places data defined by the programmer into the variable.

To review, see C++ Variables and Data Types.


1f. Use C++ operators

  • List some common C++ arithmetic operators.
  • How are arithmetic, relational and logical operators used?
  • What is the effect of the assignment operator?

One of the most basic applications of a computer is to perform arithmetic. All programming languages provide syntax similar to that found on most calculators. The most fundamental arithmetic operations are addition (+), subtraction (-), multiplication (*), and division (/). In addition, the modulus operator (%) is used for computing the remainder after integer division.

Upon making the following declarations/initializations:

float a=5.0;
float b=10.0;
int c = 5;
int d = 10;
float e,f;
int g;

You can immediately perform calculations similar to those of a simple calculator such as:

e = b/a;
f = a*b;
g = d % c;

The assignment operator (=) assigns whatever is computed on the right side of the equal sign to the variable on the left side of the equal sign. It is not an arithmetic operator, but it is used in tandem with arithmetic operators in order to assign results to a variable.

Relational and logical operators are of the utmost importance when applying programming control structures discussed in Unit 2. It is imperative to understand relational operator syntax (==, !=, >, >=, <, <=) in order to allow for comparisons between values. In addition, logical boolean operations (!, &&, ||) are necessary for constructing more complex logical expressions. The logical and (&&) between two boolean expressions can only evaluate to a value of 1 (true) when both expressions are true. The logical or (||) between two boolean expressions can only evaluate to a value of 0 (false) when both expressions are true. These two rules are instrumental when evaluating logical expressions containing several terms. For example, executing the following lines of code:

#include <iostream>
int main() {
int a=1;
int b=2;
int c=3;
int d=4;
bool j = (a>c) || (d<b);
std::cout << j << "\n";
}

would result in a screen output of 0. It is a good idea to practice with code structures like this one and try to predict the output when various relational and logical operators are mixed together.

To review, see Arithmetic Operators in C and C++ and C operators.


Unit 1 Vocabulary

This vocabulary list includes terms that students need to know to successfully complete the final exam for the course.

  • arithmetic operators
  • C++
  • high-level language
  • object-oriented programming
  • procedural programming
  • variable
  • char
  • cin
  • cout
  • float
  • include
  • int
  • main
  • namespace