C++ is a general-purpose programming language created by Bjarne Stroustrup as an extention of the C programming language. The language has expanded significantly over time, and modern C++ now has object-oriented, generic, and functional features in addition to facilities for low-level memory manupulation.
- It gives programmers a high level of control over system resources and memory.
- The language was updated 3 major times in 2011, 2014 and 2017 to C++14 and C++17.
C++ is a must for students and working professionals to become a great Software Engineer. The following are the reasons why one should learn 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 aand allows code to be reused, lowering development costs.
- C++ is portable and can be used to develop applications thta can be adapted to multiple platforms.
- C++ is fun and easy to learn!
- As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice versa.
Variables are containers for storing data values. in C++, there are different types of variables, for example:
- int - stores integers(whole numbers), without decimals, such as 123 or -123
- double - stores floating point numbers with decimals, such as 12.99 or -12.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, you must specify the type and assign it a value. Syntax is as follows:
type variable = value;
Following are few examples of declaring a variable:
Create a variable called myNum of type int and assign it the value 15:
int myNum = 15;
cout << myNum;
You can also declare a variable without assigning the value, and assign the value later:
int myNum;
myNum = 15;
cout << myNum;
A Data Type specifies the size and type of information the variable will store. The example is as follows:
int myNum = 5;
float myFloatNum = 5.99;
double myDouble = 9.98;
char myLetter = 'D';
string myText = "Hello";
Operators are used to perform operation on variables and values.
In the example below, we use the + operator to add together two values:
int x = 100 + 50;
C++ divides the operators into the following groups:
- Arithmetic Operators
- Assignment Operators
- Comparision Operators
- Logical Operators
- Bitwise Operators
C++ Conditions and If Statements
C++ supports the usual logical conditions from mathematics:
- Less than : a < b
- Less than r equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to: a == b
- Not equal to: a != b
You can use these conditions to perform different actions of different decisions.
C++ has the following conditional statements:
- Use if to specify a block of code to be executed, if a specified condition is true
- Use else to specify a block of code to be executed, if the same condition is false
- Use else if to specify a new condition to test, if the first condition is false
- Use switch to specify many alternative blocks of code to be executed
The If Statement
if(20 < 18) {
cout << "20 is greater than 18";
}
In the example above we use two variables x and y, to test whether x is greater than y. As x is 20 and y is 18, and we know that 20 is greater han 18, we print to the screen that "x is greater than y".
The else Statement
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
In the example above, time (20) is greater than 18, so the condition is false. Because of this, we move on to the else condition and print to the screen "Good evening.". If the time was less than 18, the program would print "Good day".
The else if Statement
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
In the example above, time (22) is greater than 10, so the first condition is false. The next condition, in the else if statement, is also false, so we move on to the else condition since condition1 and condition2 is both false - and print to the screen "Good evening".
Use the switch statement to select one of many code blocks to be executed.
The example below uses the weekday number to calculate the weekday name:
int day = 4;
switch (day) {
case 1:
cout << "monday";
break;
case 2:
cout << "tuesday";
break;
case 3:
cout << "wednesday";
break;
case 4:
cout << "thursday";
break;
case 5:
cout << "friday";
break;
case 6:
cout << "saturday";
break;
case 7:
cout << "sunday";
}
This is how it works:
- The switch expression is evaluated once
- The value of the expression is compared with the values of each case
- If there is a match, the associated block of code is executed
- The break and default keywords are optional
Arrays are used to store multiple values in a single variable, instead of declaring seperate variables for each value.
To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store:
string cars[4] = {"Volvo", "BMW"};
Access the Elements of an Array
You can access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
string cars[4] = {"Volvo", "BMW"};
cout << cars[0];
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
Create a Function
C++ provides some pre-defined functions, such as main(), which is used to execute code. But you can also create your own functions to perform certain actions. To create (often referred to as declare) a function, specify the name of the function, followed by parentheses ():
void myFunction() {
// code to be executed
}
- myFunction() is the name of the function
- void means that the function does not have a return value.
- Inside the function, add code that defines what the function should do
Function Declaration and Definition
A C++ funtion consists of two parts:
- Declaration: the function's name, return type, and parameters (if any)
- Definition: the body of the function (code to be executed)
void myFunction() { // declaration
// the body of the function (definition)
}
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.
Object-oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time
A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects.
Create a Class
To create a class, use the class keyword:
class MyClass {
public:
int myNum;
string myString;
}
- The class keyword is used to create a class called MyClass.
- The public keyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class. You will learn more about access specifiers later.
- Inside the class, there is an integer variable myNum and a string variable myString. When variables are declared within a class, they are called attributes.
- At last, end the class definition with a semicolon ;.
Create an Object
In C++, an object is created from a class. We have already created the class named MyClass, so now we can use this to create objects.
To create an object of MyClass, specify the class name, followed by the object name. To access the class attributes (myNum and myString), use the dot syntax (.) on the object:
class MyClass {
public:
int myNum;
string myString;
int main() {
MyClass my
myObj.myNum = 15;
myObj.myString = "some text";
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}