WELCOME TO MAJOKOTA SOCIETY
C++ Basics Elements
A C++ program is a collection of one or more subprograms, called functions. Roughly
speaking, a subprogram or a function is a collection of statements, and when it is
activated, or executed, it accomplishes something. Some functions, called predefined or
standard functions, are already written and are provided as part of the system. But to
accomplish most tasks, programmers must learn to write their own functions.
Every C++ program has a function called main . Thus, if a C++ program has only one
function, it must be the function main . Until Chapter 6, other than using some of the
predefined functions, you will mainly deal with the function main . By the end of this
chapter, you shall have learned how to write the function main .
The following is a sample C++ program.
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.
using namespace std;
cout << "Welcome to C++ Programming." << endl;
If you execute this program, it will print the following line on the screen:
Welcome to C++ Programming.
If you have never seen a program written in a programming language, the C++
program in Example 01 may look like a foreign language. To make meaningful sentences
in a foreign language, you must learn its alphabet, words, and grammar. The same is true
of a programming language. To write meaningful programs, you must learn the pro-
gramming language’s special symbols, words, and syntax rules. The syntax rules tell you
which statements (instructions) are legal, or accepted by the programming language,
and which are not. You must also learn semantic rules, which determine the meaning
of the instructions. The programming language’s rules, symbols, and special words enable
you to write programs to solve problems. The syntax rules determine which instructions
Programming language: A set of rules, symbols, and special words.
Let us begin with the real C++ programming rules.
The program that you write should be clear not only to you, but also to the reader of
Comments are for the reader, not for the compiler. So when a compiler compiles a
program to check for the syntax errors, it completely ignores comments. Throughout this
chapter, comments are shown in green.
There are two common types of comments in a C++ program—single line comments
and multiple line comments.
Single line comments begin with // and can be placed anywhere in the line. Everything
encountered in that line after // is ignored by the compiler.
Consider the following statement:
cout << "7 + 8 = " << 7 + 8 << endl;
You can put comments at the end of this line as follows:
cout << "7 + 8 = " << 7 + 8 << endl; //prints: 7 + 8 = 15
Multiple line comments are enclosed between /* and */. The compiler ignores anything
that appears between /* and */.
For example, the following is an example :
You can include comments that can
occupy several lines.
A C++ identifier consists of letters, digits, and the underscore character ( _ )
and must begin with a letter or underscore.
Identifiers can be made of only letters, digits, and the underscore character; no other
symbols are permitted to form an identifier.
C++ is case sensitive—uppercase and lowercase letters are considered different. Thus,
the identifier NUMBER is not the same as the identifier number . Similarly, the
identifiers X and x are different.
In C++, identifiers can be of any length.
The objective of a C++ program is to manipulate data. Different programs manipulate
different data. A program designed to calculate an employee’s paycheck will add, subtract,
multiply, and divide numbers, and some of the numbers might represent hours worked and
pay rate. Similarly, a program designed to alphabetize a class list will manipulate names. You
wouldn’t expect a cherry pie recipe to help you bake cookies. Similarly, you wouldn’t use a
program designed to perform arithmetic calculations to manipulate alphabetic characters.
Furthermore, you wouldn’t multiply or subtract names. Reflecting these kinds of underlying
differences, C++ categorizes data into different types, and only certain operations can be
performed on particular types of data.
They’ll never be happy together. He’s not her type.
Overheard at a Cocktail Party
int DATA TYPE
Integers in C++, as in mathematics, are numbers such as the following: -6728, -67, 0, 78, 36782, +763.
A C++ variable can hold a number or data of other types.
Every variable in a C++ program must be declared. When you declare a
variable you are telling the compiler—and, ultimately, the computer—what
kind of data you will be storing in the variable.
double one_weight, total_weight;
When there is more than one variable in a declaration, the variables are
separated by commas. Also, note that each declaration ends with a semicolon.
Copyright 2016 Majokota Society.