C++

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

*cout << "text";*

First, *cout* identifies the standard *c*haracter *out*put device (usually the computer screen). Second, the insertion operator (*<<*), which indicates that what follows is inserted into cout. Finally, a sentence within quotes (*"text"*), is the content inserted into the standard output. All C++ statements must end with a semicolon character (*;*). It marks the end of the statement, just as the period ends a sentence in English.

int a = 30; int b = 15; *int sum = a + b;*

A variable can be assigned a value, and can be used to perform operations. For example, we can create an additional variable called *sum*, and add two variables together.

What is C++

C++ is a general-purpose programming language used to create computer programs. Anything from art applications, music players and even video games! C++ was derived from C, and is largely based on it.

*#include <iostream>*

C++ offers various headers, each of which contains information needed for programs to work properly. This particular line calls for the header <iostream>. The *hash sign (#)* at the beginning of a line targets the compiler's pre-processor. In this case, *#include* tells the pre-processor to include the *<iostream>* header. The <iostream> header defines the standard stream objects that input and output data.

// line comment /* block comment */

Comments do not affect the operation of the program. The compiler ignores everything that appears in the comment, so none of that information shows in the result. However, they provide an important tool to document directly within the source code what the program does and how it operates. A comment beginning with two slashes (//) is called a *line comment*. The slashes tell the compiler to ignore everything that follows, until the end of the line. A *block comment* discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including multiple lines.

Variables

Creating a variable reserves a memory location, or a space in memory for storing values. C++ requires that you specify the *type* (data type) and the *identifier* (name) for each variable defined. An identifier starts with a letter (A-Z or a-z) or an underscore (_), followed by additional letters, underscores, and digits (0 to 9). All variables must be defined with a name and a data type before they can be used.

*{ }*

Curly brackets *{ }* indicate the beginning and end of a function, which can also be called the function's body. The information inside the brackets indicates what the function does when executed. Statements are executed in the same order that they appear within a function's body.

int num; *cin >> num;*

To enable the user to input a value, use *cin* in combination with the extraction operator (*>>*). The variable containing the extracted data follows the operator. The example shows how to accept user input and store it in the num variable. As with cout, extractions on cin can be chained to request more than one input in a single statement: cin >> a >> b;

cout << "text *\n\n*";

Two newline characters placed together result in a blank line.

int x = 40 + 60; cout << x; // Outputs 100

The addition operator adds its operands together.

The else Statement *if* (condition) { //statements } *else* { //statements }

The compiler will test the condition: - If it evaluates to true, then the code inside the if statement will be executed. - If it evaluates to false, then the code inside the else statement will be executed. When only one statement is used inside the if/else, then the curly braces can be omitted. However, including the curly braces anyway is a good practice, as they clarify the code and make it easier to read.

Decrement Operator *--x*; // prefix *x--*; // postfix

The decrement operator (--) works in much the same way as the increment operator, but instead of increasing the value, it decreases it by one.

int x = 10 / 3; cout << x; // Outputs 3

The division operator divides the first operand by the second. Any remainder is dropped in order to return an integer value. If one or both of the operands are floating point values, the division operator performs floating point division. Dividing by 0 will crash your program.

The if Statement *if* (condition) { //statements }

The if statement is used to execute some code if a condition is true. If the condition is true, block of statements in the curly brackets are executed. If the condition is false, the block of statements is simply skipped. A condition specified in an if statement does not require a semicolon. *Relational operators* (>, <, >=, <=, == equal to, != not equal to) are used to evaluate conditions.

*Prefix example:* x = 5; y = *++x*; // x is 6, y is 6 *Postfix example:* x = 5; y = *x++*; // x is 6, y is 5

The increment operator has two forms, prefix and postfix. The prefix example increments the value of x, and then assigns it to y. The postfix example assigns the value of x to y, and then increments x.

Increment Operator *x++*; //equivalent to x = x + 1

The increment operator is used to increase an integer's value by one, and is a commonly used C++ operator.

int x = 25 % 7; cout << x; // Outputs 4

The modulus operator (%) is informally known as the remainder operator because it returns the remainder after an integer division.

int x = 5 * 6; cout << x; //Outputs 30

The multiplication operator multiplies its operands.

cout << "text *\n*";

The new line character *\n* can be used as an alternative to endl. The backslash (*\*) is called an escape character, and indicates a "special" character.

Assignment Operators int x = 10; x += 4; // equivalent to x = x + 4 x -= 5; // equivalent to x = x - 5 x *= 3; // equivalent to x = x * 3 x /= 2; // equivalent to x = x / 2 x %= 4; // equivalent to x = x % 4

The simple assignment operator (=) assigns the right side to the left side. C++ provides shorthand operators that have the capability of performing an operation and an assignment at the same time.

int x = 100 - 60; cout << x; //Outputs 40

The subtraction operator subtracts one operand from the other.

cout << "text *\n* text *\n* text *\n* text ";

Using a single cout statement with as many instances of \n as your program requires will print out multiple lines of text.

int a, b; cout << "Enter a number \n"; cin >> a; cout << "Enter another number \n"; cin >> b;

You can accept user input multiple times throughout the program.

cout << "This " << "is " << "awesome!";

You can add multiple insertion operators after cout.

int mark = 100; if (mark >= 50) { cout << "You passed." << endl; if (mark == 100) { cout <<"Perfect!" << endl; } } else { cout << "You failed." << endl; }

You can also include, or nest, if statements within another if statement. C++ provides the option of nesting an unlimited number of if/else statements.

int a; int b = 42; a = 10; b = 3;

You have the option to assign a value to the variable at the time you declare the variable or to declare it and assign a value later. You can also change the value of a variable.

blank lines, spaces, tabs, and newlines

are ignored by the compiler, but serve to improve the code's readability and structure.

*int myVariable = 10;*

defines a variable (*myVariable*) (identifier) which is an integer (*int*) and assigns (*=*) to it a value of 10. The C++ programming language is case-sensitive, so myVariable and myvariable are two different identifiers.

*int main()*

program execution begins with the main function, *int main()*. Functions are introduced with a succession of a type (*int*)(integer), a name(*main*) and a pair of parentheses (*()*), optionally including parameters. The execution of all C++ programs begins with the main function, regardless of where the function is actually located within the code.

Console programs

programs that use text to communicate with the user and the environment, such as printing text to the screen or reading input from a keyboard.

Compiler

special software that translates written programming language into the code the computer understands (machine code; ones & zeros).

directives

Lines beginning with a hash sign (#) are directives read and interpreted by the compiler's pre-processor.

Integrated Development Environment (IDE)

An IDE generally integrates several development tools, including a text editor and tools to compile programs directly from it.

What is a C++ program

A C++ program is a collection of commands or statements.

Operator Precedence

If none of the expressions are in parentheses, *multiplicative* (multiplication, division, modulus) operators will be evaluated before *additive* (addition, subtraction) operators. Parentheses force the operations to have higher precedence. If there are parenthetical expressions nested within one another, the expression within the innermost parentheses is evaluated first.

*int a, b;*

In cases in which you have multiple variables of the same type, it's possible to define them in one declaration, separating them with commas. *int a, b;* defines two variables of type int.

cout << "text" *<< endl;*

the *endl* manipulator will put in a line break.

*using namespace std;*

the line *using namespace std;* tells the compiler to use the std (standard) namespace. The std namespace includes features of the C++ Standard Library.


Ensembles d'études connexes

Wonderlic 25 Question Quick Practice Test

View Set

Chapter 14 New Beginnings: Single-Parent Families, Remarriages, and Blended Families

View Set