Docsity

Basic C++ learning for starter, Summaries of Computer Programming

Partial preview of the text

Download Basic C++ learning for starter and more Summaries Computer Programming in PDF only on Docsity!

Introduction to Computer

Science

Chapter 2

BASIC ELEMENTS IN C++

Chapter 2

Program structures

Data types and operators

Variables and declaration statements

Function

Each function must have a name.
Names or identifiers in C++ can made up of any
combination of letters, digits, or underscores selected
according to the following rules:
  • Identifiers must begin within an uppercase or lowercase ASCII

letter or an underscore (_).

  • You can use digits in an identifier, but not as the first character.

You are not allowed to use special characters such as $, &, * or %.

  • Reserved words cannot be used for variable names.
Example:
DegToRad intersect addNums
FindMax1 _density slope

Pascal: ThisIsAFunction

Camel: thisIsAFunction

The main () function

The main () function is a special function that runs

automatically when a program first executes.

All C++ programs must include one main() function. All other

functions in a C++ program are executed from the main().

The first line of the function, in this case int main() is called a

function header line.

The function header line contains three pieces of information:

  1. What type of data, if any, is returned from the function.

2.The name of the function

  1. What type of data, if any, is sent into the function.

int main()

int getNumber(void)

int totalScore(int score1, int score2)

The cout Object

The cout object is an output object that sends data

given to it to the standard output display device.

To send a message to the cout object, you use the

following pattern:

cout << “text”;

The insertion operator , <<, is used for sending text

to an output device.

The text portion of cout statement is called a text

string.

cout << “Hello “ << “C++”;

A simple program

Example 2.1.

#include

using namespace std;

int main()

cout << "Hello world!";

return 0;

header file

A header file is a file with an extension of .h that is included as part

of a program. It notifies the compiler that a program uses run-time

libraries.

All statements in C++ must end with a semicolon.

The iostream classes are used for giving C++ programs input and

output capabilities.

The header file for the iostream class is iostream.h.

Preprocessor directives

The preprocessor is a program that runs before the

compiler.

When the preprocessor encounters an #include

statement, it places the entire contents of the

designated file into the current file.

Preprocessor directives and include statements

allow the current file to use any of the classes,

functions, variables , and other code contained

within the included file.

i/o manipulator

An i/o manipulator is a special function that can be

used with an i/o statement.

The endl i/o manipulator is part of iostream classes

and represents a new line character.

Example:

cout << “Program type: console application” << endl;

cout << “Create with: Visual C++ “<< endl;

cout << “Programmer: Don Gesselin” << endl;

2. DATA TYPES AND OPERATORS

Data Types

A data type is the specific category of information

that a variable contains.

There are three basic data types used in C++:

integers, floating point numbers and characters.

Integer Data Type

An integer is a positive or negative number with no

decimal places.

Examples:

  • 259 -13 0 200

Floating Point Numbers

A floating point number contains decimal places or

is written using exponential notations.

90.1e3 0.89E8 -1.01e-

Note: 90.1e3 means 90.1×

3

Arithmetic Operators

Arithmetic operators are used to perform mathematical

calculations, such as addition, subtraction, multiplication, and

division.

Operator Description

  • Add two operands
  • Subtracts one operand from another operand
  • Multiplies one operand by another operand

/ Divides one operand by another operand

% Divides two operands and returns the remainder

A simple arithmetic expression consists of an arithmetic

operator connecting two operands in the form:

operand1 operator operand

Examples:

Example 2.2.

#include

using namespace std;

int main()

cout << "15.0 plus 2.0 equals " << (15.0 + 2.0) << '\n'

<< "15.0 minus 2.0 equals " << (15.0 - 2.0) << '\n'

<< "15.0 times 2.0 equals " << (15.0 * 2.0) << '\n'

<< "15.0 divided by 2.0 equals " << (15.0 / 2.0) << '\n';

return 0;

The output of the program:

15.0 plus 2.0 equals 17

15.0 minus 2.0 equals 13

15.0 times 2.0 equals 30

15.0 divided by 2.0 equals 7.

Operator Precedence and Associativity

Expressions containing multiple operators are evaluated by the

priority, or precedence , of the operators.

Operator Associativity

unary - Right to left

  • / % Left to right
    • Left to right

Example:

3. VARIABLES

One of the most important aspects of programming

is storing and manipulating the values stored in

variables.

Variable names are also selected according to the

rules of identifiers:

  • Identifiers must begin with an uppercase or

lowercase ASCII letter or an underscore (_).

  • You can use digits in an identifier, but not as the first

character. You are not allowed to use special characters

such as $, &, * or %.

  • Reserved words cannot be used for variable

names.