TOPIC 1: INTRODUCTION TO C++ AND BASIC
CONCEPTS
C++ HISTORY
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of
AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as
the founder of C++ language. It was develop for adding a feature of OOP (Object Oriented
Programming) in C without significantly changing the C component.
C++ programming is "relative" (called a superset) of C, it means any valid C program is also a
valid C++ program. C++ programs are fast and efficient, actualities which helped make C an
extremely popular programming language. It sacrifices some flexibility in order to remain
efficient.
However, C++ uses compile-time binding, which means that the programmer must specify the
specific class of an object, or at the very least, the most general class that an object can belong to.
This makes for high run-time efficiency and small code size, but it trades off some of the power to
reuse classes.
C++ has become so popular that most new C compilers are actually C/C++ compilers (for example
in the CODEBLOCKS we are using). However, to take full advantage of object- oriented
programming, one must program (and think!) in C++, not C. This can often be a major problem
for experienced C programmers.
Why Use C++
C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User Interfaces, and embedded
systems.
C++ is an object-oriented programming language which gives a clear structure to
programs and allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple
platforms.
C++ is fun and easy to learn!
As C++ is close to C, C# and Java, it makes it easy for programmers to switch to C++ or
vice versa.
Difference between C and C++
C++ was developed as an extension of C, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ support classes and objects, while C does
not.
C++ Syntax
#include<iostream>
using namespace std;
int main() {
cout << "Wamaitha Mbugua!";
return 0; }
Program explained
Line 1: #include <iostream> is a header file library that lets us work with input and output
objects, such as cout (used in line 5). Header files add functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
Don't worry if you don't understand how #include <iostream> and using namespace std
works. Just think of it as something that (almost) always appears in your program.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a
function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<)
to output/print text. In our example it will output "Hello World".
Note: Every C++ statement ends with a semicolon ;.
Note: The body of int main() could also been written as:
int main () { cout << "St. Mark’s High School "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines makes the code more
readable.
Line 6: return 0 ends the main function.
Line 7: Do not forget to add the closing curly bracket } to actually end the main function.
Omitting Namespace
You might see some C++ programs that runs without the standard namespace library. The using
namespace std line can be omitted and replaced with the std keyword, followed by the ::
operator for some objects:
Example
#include <iostream>
int main() {
std::cout << "Mbugua Wamaitha!";
return 0; }
C++ Output (Print Text)
The cout object, together with the << operator, is used to output values or print text:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Zetech University";
return 0;
}
You can add as many cout objects as you want. However, note that it does not insert a new line
at the end of the output:
Example
#include <iostream>
using namespace std;
int main() {
cout << "St.Mark’s High School";
cout << "Zetech University";
return 0;
}
New Lines
To insert a new line, you can use the \n character:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Gitwe High School \n";
cout << "St. Lucie Secondary School";
return 0;
}
Tip: Two \n characters after each other will create a blank line:
Example
#include <iostream>
using namespace std;
int main() {
cout << "St. Mark’s High School \n\n";
cout << "Zetech University";
return 0;
}
NB: Another way to insert a new line, is with the endl manipulator:
The newline character (\n) is called an escape sequence, and it forces the cursor to change its
position to the beginning of the next line on the screen. This results in a new line.
Examples of valid escape sequences are:
Escape sequence Description
\n Creates a new line
\t Creates a horizontal tab
\\ Inserts a backslash character (\)
\" Inserts a double quote character
C++ Comments
The C++ comments are statements that are not executed by the compiler. The comments in C++
programming can be used to provide explanation of the code, variable, method or class. By the
help of comments, you can hide the program code also.
There are two types of comments in C++.
o Single Line comment
o Multi Line comment
C++ Single Line Comment
The single line comment starts with // (double slash). Let's see an example of single line
comment in C++.
#include <iostream>
using namespace std;
int main()
int x = 11; // x is a variable
cout<<x<<"\n";
Output:
11
C++ Multi Line Comment
The C++ multi line comment is used to comment multiple lines of code. It is surrounded by slash
and asterisk (/∗ ..... ∗/). Let's see an example of multi line comment in C++.
#include <oistream>
using namespace std;
int main()
/* declare and
print variable in C++. */
int x = 35;
cout<<x<<"\n";
Output:
35
NB: Normally, we use // for short comments, and i/* */ for longer.
C++ Variables
Variables are containers for storing data values. Variables are the fundamental building blocks of
data manipulation and storage in programming, acting as dynamic containers for data in the C++
programming language. A variable is more than just a memory label. It serves as a link between
abstract ideas and concrete data storage, allowing programmers to deftly manipulate data.
With the help of C++ variables, developers may complete a wide range of jobs, from
simple arithmetic operations to complex algorithmic designs. These programmable containers
can take on a variety of shapes, such as integers, floating-point numbers, characters, and user-
defined structures, each of which has a distinctive impact on the operation of the program.
Programmers follow a set of guidelines when generating variables, creating names that combine
alphanumeric letters and underscores while avoiding reserved keywords. More than just
placeholders, variables are what drive user input, intermediary calculations, and the dynamic
interactions that shape the program environment.
A variable is a name of memory location. It is used to store data. Its value can be changed and it
can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
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.99
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
To create a variable, specify the type and assign it a value:
Syntax
type variableName = value;
Where type is one of C++ types (such as int), and variableName is the name of the variable
(such as x or myName). The equal sign is used to assign values to the variable.
To create a variable that should store a number, look at the following example:
Example
Create a variable called myNumber of type int and assign it the value 15:
#include <iostream>
using namespace std;
main() {
int myNumber = 15;
cout << myNumber;
return 0;
You can also declare a variable without assigning the value, and assign the value later:
Example
#include <iostream>
using namespace std;
main() {
int myNumber;
myNumber = 15;
cout << myNumber;
return 0;
}
NB: that if you assign a new value to an existing variable, it will overwrite the previous
value:
Example
#include <iostream>
using namespace std;
main() {
int myNumber = 15; // myNumber is 15
myNumber = 10; // Now myNumber is 10
cout << myNumber; // Outputs 10
return 0;
}
Other Types
A demonstration of other data types:
Example
int myNumber = 5; // Integer (whole number without decimals)
double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
Display Variables
The cout object is used together with the << operator to display variables.
To combine both text and a variable, separate them with the << operator:
Example
#include <iostream>
using namespace std;
main() {
int myAge = 35;
cout << "I am " << myAge << " years old.";
return 0;
Add Variables Together
To add a variable to another variable, you can use the + operator:
Example
#include <iostream>
using namespace std;
main() {
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
return 0;
Declare Many Variables
To declare more than one variable of the same type, use a comma-separated list:
Example
#include <iostream>
using namespace std;
main() {
int x = 5, y = 6, z = 50;
cout << x + y + z;
return 0;
}
One Value to Multiple Variables
You can also assign the same value to multiple variables in one line:
Example
#include <iostream>
using namespace std;
main() {
int x, y, z;
x = y = z = 50;
cout << x + y + z;
return 0;
}
C++ Identifiers
All C++ variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and
maintainable code:
Example
// Good
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
The general rules for naming variables are:
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case sensitive (myVar and myvar are different variables)
Names cannot contain whitespaces or special characters like !, #, %, etc.
Reserved words (like C++ keywords, such as int) cannot be used as names