Docsity

Understanding the Basics of a C++ Program: Data Types and Identifiers - Part 1, Exercises of Electromagnetism and Electromagnetic Fields Theory

Partial preview of the text

Download Understanding the Basics of a C++ Program: Data Types and Identifiers - Part 1 and more Exercises Electromagnetism and Electromagnetic Fields Theory in PDF only on Docsity!

Dissecting a C++ - part 1

Dissecting a C++ - part 1

1

Learning Objectives

Learning Objectives

Describe the basic elements in a

C++ program

Define various data types

2

The Basics of a C++

The Basics of a C++

Program

Program

4

The Basics of a C++

The Basics of a C++

Program

Program

A C++ program is a collection of

functions

Function: set of statements whose

objective is to accomplish something

Preceding program consists of

function main

◦ Heading: int main()

#include allows us to

use cout and endl

5

The Basics of a C++

The Basics of a C++

Program

Program

// a program that add two numbers

#include

using namespace std;

// program to add numbers

int main(){

int firstNumber ;

int secodNumber ;

int sum;

firstNumber = 5;

secondNumber = 10;

sum = firstNumber +

secondNumber;

cout << sum ;

system (“pause”);

}

Comments

Comments

Comments are for the reader, not

the compiler

Two types

Single line

// This is a C++ program. It displays three lines

// of text, including the sum of two numbers.

Multiple line

/*

You can include comments that can

occupy several lines.

*/

Identifiers

Identifiers

Identifiers are names of things in a program. Things

in programs are variables, constants, and functions

Identifiers in C++ are case sensitive

◦ NUMBER is not the same as number

Rules to name identifiers are as follow:

◦ Named identifiers cannot be the same as reserved word

◦ Identifier consists of letters, digits and underscores character

(_)

◦ Identifier cannot begin with a digit

◦ Identifier cannot have any symbols except underscore

◦ Identifiers cannot have between the name

Identifiers (continued)

Identifiers (continued)

The following are legal identifiers

in C++:

first

conversion

payRate

Data Types

Data Types

Data type: the type of the data

Simple Data Types

Simple Data Types

Three categories of simple data

Integral: integers

Floating-point: decimal numbers

Enumeration type: user-defined data

type

bool

bool

Data Type

Data Type

bool type

Two values: true and false

Manipulate logical (Boolean)

expressions

true and false are called logical

values

bool, true, and false are

reserved words

char

char

Data Type

Data Type

The smallest integral data type

Used for characters: letters, digits,

and special symbols

Each character is enclosed in single

quotes

◦ 'A', 'a', '0', '*', '+', '$', '&'

A blank space is a character and is

written ' ', with a space left

between the single quotes

Data types – storage space

Data types – storage space

Data type Values Storage (in bytes)

int -2147483648 to 2147483647 4

bool true and false 1

char -128 to 127 1

float -3.4E+38 to 3.4E+38 4

double -1.7E+308 to 1.7E+308 8

string

string

Type

Type

String: sequence of zero or more

characters

Enclosed in double quotation marks