Sei sulla pagina 1di 25

Intro to C++ Programming:

Basic Terms and Concepts



Table of Contents
Introduction ..................................................................................................................................... 5
The Structure of a Program ............................................................................................................. 7
Line 1 .......................................................................................................................................... 7
Line 2 .......................................................................................................................................... 7
Line 3 .......................................................................................................................................... 7
Line 4 .......................................................................................................................................... 7
Line 5 .......................................................................................................................................... 8
Line 6 .......................................................................................................................................... 8
Line 7 .......................................................................................................................................... 8
Variables and Constant Types ...................................................................................................... 12
Identifiers .................................................................................................................................. 12
Character Types ........................................................................................................................ 12
Numerical Integer Types........................................................................................................... 12
Floating Types .......................................................................................................................... 12
Double Types ............................................................................................................................ 12
Boolean (Flag) Types ................................................................................................................ 12
Compound Types ...................................................................................................................... 12
Operators Used In C++ ................................................................................................................. 14
Assignment Operators ............................................................................................................... 14
(I)........................................................................................................................................... 14
(II) ......................................................................................................................................... 14
Arithmetic Operators ................................................................................................................ 15
(III) ........................................................................................................................................ 15
Increment and Decrement ......................................................................................................... 16
Logical Operators...................................................................................................................... 16
Comma Operator (,) .................................................................................................................. 16
Basic Inputs and Outputs in C++ .................................................................................................. 18
Cout: The Standard Output Statement ...................................................................................... 18
(IV) ........................................................................................................................................ 18
(V) ......................................................................................................................................... 18
(VI) ........................................................................................................................................ 19
Cin: The Standard Input Statement ........................................................................................... 19
(VII) ...................................................................................................................................... 19
Cin and Strings .......................................................................................................................... 19
Frequently Asked Questions ......................................................................................................... 21
1) What is C++? .................................................................................................................. 21
2) Do I need to know another programming language before I learn C++? ...................... 21
3) Who owns the rights to the C++ language? ................................................................... 21
Glossary ........................................................................................................................................ 22
Index ............................................................................................................................................. 24


Introduction

The C++ programming language is one of the most popular languages used in the programming
world because it is seen as one of the most efficient ways create programs. It is used in several
mediums, ranging from systems and application software to high performance servers and
entertainment software. But before you head off to program the next Call of Duty video game,
you'll need to learn the basics first. This manual will help aid you in understanding these
concepts:
The Structure of a Program
The Different Variables And Types Used in C++ Programming
Operators Used in C++
Basic Inputs/Outputs of C++






The Structure of a Program
Line 1
The first line that contains the slashes (//) is what's known as the programmer's comment.
It states what the purpose of your program is; but other than that, it has absolutely no
effect on the overall program's functions. The slashes must be at the front of your
comment in order for it to be interpreted as a comment and not as a lingering data
component.





Line 2
The second line contains an example of a preprocessor. Any directive that contains a hash
sign (#) in front of it is always interpreted as such. #include <iostream> is the
preprocessor that you will use the most as you begin learning the C++ language, so it
would be best to remember that without this directive, your entire program will never
come to pass. In regards to the example program in Figure 1, #include <iostream>
instructs the preprocessor to include a piece of code in the program in order for it to
perform basic input and output operations, such as printing out the Hello World!
statement, which we will get to later. This piece of code is more commonly known as a
header iostream.


Line 3
The third line is simply a blank that helps the programmer distinguish their introductory
statements from their actual program. It has no effect on the program.

Line 4
The fourth line is the declaration of a function (a group of code statements). Functions are
always introduced starting with a classification of their type, an assigned name given to
the group of code statements, and a pair of parentheses (()). In the above example, you
can see that this program is of type int (An integer), the name of the group of code
statements is "main" and the statement is followed by a pair of parentheses.



Line 5
The open braces at the ends of lines 4 and 8 ({ }) indicate the beginning and end of the
main function. Everything in between is the body of the program and it is, essentially,
what makes the program unique. In other words, if the beginning and ending brackets are
the first and last slices of bread, everything in between is the meat, cheese, and veggies of
the program.



Line 6
Line six is what's known as a C++ statement; the first slice of meat in your program that
actually defines and specifies how your program is supposed to behave. The statement
above, in particular contains 4 parts:
The cout statement
The insertion operator (<<)
A sentence within quotation marks ("")
And an end line statement followed by a semicolon (;)



The cout (character output) statement identifies the output of the program, the insertion
operator (<<) shows that what follows after it is what is supposed to be included in the
cout statement, the sentence within the quotation marks ("") is the statement inserted into
the character output (in other words, what your cout statement prints out); and finally, the
end line statement followed by a semicolon (;) marks the end of your statement, just like
a period at the end of a regular English statement.



Line 7
Finally, line seven is known as your return 0 statement. In C++, your program is initially
designed to take a number (of value=0 or greater) of input variables and return a number
(of value=1 or greater) of result variables. Your program takes a number of input

variables (zero) and returns a number of result variables (1). The line return 0 satisfies
the functions desire to return a result variable.





Variables and Constant Types

Identifiers
An identifier is sequence of one or more letters, numbers, and/or underscores (_).
However, spaces, punctuation, and/or symbols apart from the ones mentioned above
cannot be used as identifiers and attempts to do so will result in your program return an
ERROR message. Some examples of acceptable identifiers include, but are not limited to:
alignas
alignof
char32_t
xor_eq
_notequal

Character Types
Character types can represent a single character, such as b" or "&". The most basic
character type used in C++ is char.

Numerical Integer Types
Numerical integer types can contain whole number values, ranging from "8" to "56789".

Floating Types
Floating integer types (or simply, floats for short) represent values containing decimal
points. Floats can only hold a numerical value that is 7 digits long.

Double Types
Double Integer Types (doubles) also represent values containing decimal points, but
doubles can hold numerical values that are 14-15 digits long.

Boolean (Flag) Types
Boolean Types (also known as bool in C++ or flags) can only represent one of two
statements, true or false.

Compound Types
Finally, compound types are one of C++ programming's best assets. They are composed
of several different elements that are, themselves, made up of smaller, more elementary
values, much like the ones discussed above. One of the more common examples of a
compound type is called a string. String variables are able to store sequences of
characters, such as words and full sentences.





Operators Used In C++

Operators are what programmers use to operate already introduced variables and constants. The
following is a complete list of said operators and all that you need to know about them.

Assignment Operators
Assignment operators assign variables (x, y, z, etc.) values
(I)


Assignment operators can also be used when writing out functions that you will use in your
programs.

(II)














Arithmetic Operators
The five main arithmetical operators that you will use in C++ are:

The addition through multiplication operators correspond to their regular mathematical functions
while division and modulo work a bit differently. The division operator, when declared, prints
out the whole number value of the two numbers that you are dividing

(III)

The example above would print out "2" as the value for a because 4.5 divided by 2 produces a
whole number of 2. The modula (%) would print out the remainder of the two numbers that you
are dividing.






Increment and Decrement
These are just fancy ways of saying adding and subtracting. The increase operator and the
decrease operator both increase or reduce the value of a variable by one.


Logical Operators
Logical Operators are used whenever the programmer needs to evaluate two separate expressions
in order to obtain a single, related result. The operator "!" is the C++ equivalent of saying "not".

The && and || operators are used to say "and" or "or", repectively, in C++.



Comma Operator (,)
This operator is used to separtate two, or more, expressions from each other. When using a
comma operator; the program is designed to read the expressions from left to right, just like how
you and I would read a book in real life.

In the example above, the program would read the "b=3" expression first and, thus, assign the v
alue of 3 to b. Then, it would plug that value into the "b+2" expression and, finally, the program
would print out a value of 5 for a.

Statement Description
cin Standard input statement
Cout Standard output statement



Basic Inputs and Outputs in C++

In this section of the manual, we will go more in depth into the concepts of cin, cout, and
string.


Cout: The Standard Output Statement
As we discussed earlier, cout is used to identify the output statement of your program
through the use of the insertion operators (<<). For example:





Inserting text after the cout statement without the use of quotation marks ("") will result
in your program interpreting that text as a variable rather than printing it literally.
(IV)


In the example above, the cout statement containing "Hello" literally prints out the
statement Hello and nothing more. In the cout statement with just 'Hello', the program
interprets that as "The user wants me to print out the value of the variable 'Hello' for
them." But if you never assigned a value to the variable 'Hello' or if you actually meant to
type "Hello", your program will print out an ERROR message.

You can also use multiple insertion statements to form a single statement.
(V)


The cout statement above prints out the phrase "This is a single C++ statement" and is the
equivalent of doing 'cout<<"This is a single C++ statement"<<endl;
Whichever method you use while programming is a matter of personal preference and
has no overall effect on your program.


While chaining together multiple insertion statements might seem like a waste of time to
some people when you seek to print out a single C++ statement, they are very helpful
when you need to combine literal texts and variables.

(VI)

In the above example, the statement above would print out the phrase "I am age years old
and my zip code is zip code, but in place of age and zip code, it would print out the values
of those variables, provided you assigned values to those variables in the beginning of
your program. If the value of age is 19 and the value of zip code is 76203, your printed
statement would read as " I am 19 years old and my zip code is 76203.

Cin: The Standard Input Statement
Cin has an insertion operator of its own; however, here it is called an extractor operator
and is denoted as a pair of "greater than" signs (>>). cin works with its extractor
operators to extract stored data from previously declared variables.

(VII)

In the above example, the programmer has declared a variable named "age" and is
prompting users to input a value for what they want age to be. The programmer doesn't
need to enter a value for age themselves, because age is a variable that will change from
user to user, as opposed to a constant that will remain the same across different
platforms.

Cin and Strings
Extractor operators and cin statements can be used to store strings of characters the same
way that they can be used to store basic data types.


In the example above, the programmer assigns a string value of characters to the variable
"mystring" while prompting the user of said program to input a value for "mystring".
CAUTION: Cin statements always interprets spaces, tabs, and breaks in
string statements as a termination in the variable's value. Make sure to always
make your cin strings single words instead of phrases or entire sentences.

























Frequently Asked Questions
1) What is C++?
C++ is a programming language that literally means increasing C, which reflects C++s
role as an evolved form of the C language.
2) Do I need to know another programming language before I learn C++?
Not at all. Because C++ is such a simple language that has a fairly straightforward
input/output system, learning C++ programming is actually more beneficial to you if you
learn it before any other programming language.
3) Who owns the rights to the C++ language?
No one ones the C++ language. Anyone can use it, royalty-free



















Glossary
Term Definition
C++ Programming General purpose programming that
is implemented on a wide variety of
hardware and software platforms
Insertion Operator <<; associated with cout statements
Programmer's Comment Used by programmers to briefly
explain the purpose or intent of their
program. Always listed at the
beginning
Preprocessor Directives included at the beginning
of a program and begin with a hash
sign (#)
Identifier Used for any variable, function, or
data definition.
Floats Integers with a decimal point that are
typically 7-8 characters long
Double Integers with a decimal point that are
typically 14-15 characters long
Boolean Flag Statements that represent "True" or
"False"
Modula An operation that finds the
remainder of a division function
Increment Increasing function
Decrement Decreasing function
Input Data entered into the program by the
user
Output What the program prints out
Extractor operator >>; associated with cin statements




Index
1. "", 8, 18
2. #, 7, 22
3. &&, 16
4. //, 7
5. { }, 8
6. ||, 16
7. blank, 7
8. Caution, 20
9. character, 8, 12
10. code, 7, 19
11. function, 7, 8, 22, 23
12. hash sign, 7, 22
13. input, 7, 9, 17, 19, 20, 21
14. language, 5, 7, 21
15. mediums, 5
16. open braces, 8
17. operator, 8, 15, 16, 19, 23
18. operators, 14, 15, 16, 18, 19
19. output, 7, 8, 17, 18, 21
20. quotation marks, 8, 18
21. sequence, 12
22. slashes, 7
23. symbols, 12

Potrebbero piacerti anche