C++ Programming: Understanding Elements - Variables, Data Types, Input/Output, Schemes and Mind Maps of Computer science

A module from a Computer Programming course at Isabela State University, focusing on the Elements of C++. It covers topics such as C++ program structure, namespaces, header files, variable declaration, data types, and basic input/output statements. Students will learn about different types of variables, their declaration, and the use of input and output statements.

Typology: Schemes and Mind Maps

2019/2020

Uploaded on 12/02/2021

iphone-gmail
iphone-gmail 🇵🇭

4 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MODULE No. 4: ELEMENTS OF C++
EE122 Computer Programming
ISABELA STATE UNIVERSITY ILAGAN CAMPUS
Page | 1
Topics
1. C++ Program Structure
2. Libraries and Namespace
3. Variable and Literals
4. Data Types
5. Basic Input and Output Statements
6. Comments
7. Constants
8. Operators
Objectives
1. Understand the structure of C++ program.
2. Understand libraries and Namespace in writing a C++ program.
3. Understand and demonstrate how to declare variables and
literals in C++.
4. Apply and Demonstrate how to use input and output statement,
comments, constants, and operators of C++.
Content
1. C++ Program Structure
A simple code that would print the words Hello World.
Test this code online: http://tpcg.io/n4BVuS
The various parts of the above program:
The C++ language defines several headers, which contain
information that is either necessary or useful to your
program. For this program, the header <iostream> is needed.
The line using namespace std; tells the compiler to use the
std namespace. Namespaces are a relatively recent addition
to C++.
The next line '// main() is where program execution
begins.' is a single-line comment available in C++. Single-
line comments begin with // and stop at the end of the
line.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Programming: Understanding Elements - Variables, Data Types, Input/Output and more Schemes and Mind Maps Computer science in PDF only on Docsity!

EE122 – Computer Programming

Page | 1

Topics

  1. C++ Program Structure
  2. Libraries and Namespace
  3. Variable and Literals
  4. Data Types
  5. Basic Input and Output Statements
  6. Comments
  7. Constants
  8. Operators

Objectives

  1. Understand the structure of C++ program.
  2. Understand libraries and Namespace in writing a C++ program.
  3. Understand and demonstrate how to declare variables and literals in C++.
  4. Apply and Demonstrate how to use input and output statement, comments, constants, and operators of C++.

Content

1. C++ Program Structure

A simple code that would print the words Hello World.

Test this code online: http://tpcg.io/n4BVuS

The various parts of the above program:

 The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header is needed.  The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++.  The next line ' // main() is where program execution begins. ' is a single-line comment available in C++. Single- line comments begin with // and stop at the end of the line.

EE122 – Computer Programming

Page | 2

 The line int main() is the main function where program execution begins.  The next line cout << "Hello World"; causes the message "Hello World" to be displayed on the screen.  The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process.

2. Libraries and Namespace

Library

In programming, a library is a collection of precompiled routines that a program can use. The routines, sometimes called modules, are stored in object format.

Namespace

A namespace is a declarative region that provides a scope to the identifiers ( the names of types, functions, variables, etc ) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

Header Files

Header files contain definitions of Functions and Variables, which is imported or used into any C++ program by using the pre-processor #include statement. Header file have an extension " .h " which contains C++ function declaration and macro definition.

3. Variable and Literals

Variables are containers for storing data values.

In C++, there are different types of variables (defined with different keywords), for example:

 int - stores integers (whole numbers), without decimals, such as 123 or - 123  double - stores floating point numbers, with decimals, such as 19.99 or - 19.  char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes  string - stores text, such as "Hello World". String values are surrounded by double quotes  bool - stores values with two states: true or false

EE122 – Computer Programming

Page | 4

4. Data Types

As explained in the Variables, a variable in C++ must have a specified data type:

Basic Data Types

The data type specifies the size and type of information the variable will store:

Numeric Types

Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515.

EE122 – Computer Programming

Page | 5

Boolean Types

A boolean data type is declared with the bool keyword and can only take the values true or false. When the value is returned, true = 1 and false = 0.

Character Types The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':

String Types The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its most basic usage. String values must be surrounded by double quotes:

EE122 – Computer Programming

Page | 7

The << operator inserts the data that follows it into the stream that precedes it. In the examples above, it inserted the literal string Output sentence, the number 120 , and the value of variable x into the standard output stream cout. Notice that the sentence in the first statement is enclosed in double quotes (") because it is a string literal, while in the last one, x is not. The double quoting is what makes the difference; when the text is enclosed between them, the text is printed literally; when they are not, the text is interpreted as the identifier of a variable, and its value is printed instead.

For example, these two sentences have very different results:

Multiple insertion operations (<<) may be chained in a single statement:

This last statement would print the text This is a single C++ statement. Chaining insertions is especially useful to mix literals and variables in a single statement:

Assuming the age variable contains the value 24 and the zipcode variable contains 90064, the output of the previous statement would be:

I am 24 years old and my zipcode is 90064

What cout does not do automatically is add line breaks at the end, unless instructed to do so. For example, take the following two statements inserting into cout:

cout << "This is a sentence."; cout << "This is another sentence.";

The output would be in a single line, without any line breaks in between. Something like:

EE122 – Computer Programming

Page | 8

This is a sentence.This is another sentence.

To insert a line break, a new-line character shall be inserted at the exact position the line should be broken. In C++, a new-line character can be specified as \n (i.e., a backslash character followed by a lowercase n). For example:

This produces the following output:

First sentence. Second sentence. Third sentence.

Alternatively, the endl manipulator can also be used to break lines. For example:

This would print:

First sentence. Second sentence.

Standard input (cin)

For formatted input operations, cin is used together with the extraction operator, which is written as >> (i.e., two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For example:

The first statement declares a variable of type int called age, and the second extracts from cin a value to be stored in it. This operation makes the program wait for input from cin; generally, this means that the program will wait for the user to enter some sequence with the keyboard. In this case, note that the characters introduced using the keyboard are only transmitted to the program when the ENTER (or RETURN) key is pressed. Once the statement with

EE122 – Computer Programming

Page | 10

The example above a single-line comment at the end of a line of code:

C++ Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:

7. Constants

Constants refer to fixed values that the program may not alter and they are called literals. Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values. Again, constants are treated just like regular variables except that their values cannot be modified after their definition.

The const Keyword

You can use const prefix to declare constants with a specific type as follows − const type variable = value;

8. Operators

C++ divides the operators into the following groups:

Arithmetic operator

Operators are used to perform operations on variables and values.

EE122 – Computer Programming

Page | 11

Assignment Operators

Assignment operators are used to assign values to variables.

Comparison Operators

Comparison operators are used to compare two values.