It was developed by Bjarne Stroustrup in Bell Labs, 1979
It is an extension of the C language
It has a number of features compared to older programming language
Such features are object-oriented programming, generic programming, and exception handling
It became one of the most popular programming languages.
Structure
Core Language:
foundation of C++
includes all essential features of languages:
data types
classes
variables
functions
Standard Library
provides a wide range of functions and classes already embedded in the language
some examples are:
input/output
string manipulation
math operations
Third-party Libraries
provides additional functionalities to C++ not included in the language itself
some examples include game development, scientific computing, and machine learning.
Preprocessor Directives
used to control the preprocessor.
a program that runs before the compiler
can be used to include header files, macros, and conditionally compile code.
Global Declaration
defines variables, functions, or classes accessible to all parts of the program
Function Definition
defines a code that is executed when a function is called
Main Function
the entry point for all C++ programs
the first function to be called when the program is executed.
Below is an example of C++ program.
intro.cpp
#include <iostream>using namespace std;int main() { int a, b, c; cout << "Enter two numbers: "; cin >> a >> b; c = a + b; cout << "The sum of " << a << " and " << b << " is " << c << endl; return 0}
Each line or set of instruction we use inside a program is called a statement.
Converting Flowcharts into C++ Code
Suppose that we have the following sequential flowchart below:
flowchart TD
start(["Start"])
init{{"a = 0\nb = 0\nsum = 0"}}
input[\"Input a, b"\]
process["sum = a + b"]
output[\"Output sum"\]
stop(["End"])
start --> init
init --> input
input --> process
process --> output
output --> stop
This flowchart shows us that by inputting two numbersa and b, we output its resulting sum.
But, how can we turn this into working C++ code?
We do this by writing our code based on how our flowchart is organized.
Step 1: Start the code by writing our necessary syntax.
flowchart TD
start(["Start"])
Here, we often introduce header files, namespaces, and even the main function itself.
Take note that there are some differences when writing code for Turbo C++ and some modern compilers.
Modern C++
#include <iostream>using namespace std;int main () {}
Depending on the compiler we use, there may be some changes with the syntax we use to start our code.
Step 2: Initialize the variables.
flowchart TD
init{{"a = 0\nb = 0\nsum = 0"}}
To initialize our variables, we declare a variable inside the main function definition.
We simply write int then followed by our variable, for example, a, then the initial value for that variable.
We simply follow what our flowchart says and we set the initial value for each variable as 0.
Remember, that as we put an another statement inside main, we end each statement with a semicolon ;
Modern C++
#include <iostream>using namespace std;int main () { int a = 0; int b = 0; int sum = 0;}
Tip Declaring Multiple Variables
When we declare multiple variables such as in this case, we can instead simplify them into a single statement.
int main () { int a = 0, b = 0, sum = 0;}
Here, each variable assignment is separated by a comma.
We can do this to make our code more compact.
Step 3: Use cout and cin for user input/output.
flowchart TD
input[\"Input a, b"\]
The next thing in our flowchart is to input numbers to our code.
First, we let the user know that we need to input a value.
We do this by writing cout << "Enter the first number: ";.
cout means to display something to the user, as in for output.
When we want to use actual text in our code, we enclose them in double quotes"".
Modern C++
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: ";}
Now that the user knows that we need to input something, we now use cin >> a.
This allows us to assign a value to a variable to our first number a.
Modern C++
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: "; cin >> a;}
We then do the exact same thing, this time for the second number b.
Modern C++
#include <iostream>using namespace stdint main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b;}
Step 4: Perform the necessary operations.
flowchart TD
process["sum = a + b"]
In this step, we perform the operations that we need.
In this case, we are asked to add a and b, then we assign its value to the variable sum.
Modern C++
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = a + b;}
Step 5: Output the resulting value.
flowchart TD
output[\"Output sum"\]
Now, what we want to do is to output the sum.
We do this by using the cout command again, then output the contents of the variable sum.
Modern C++
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = a + b; cout << sum;}
Step 6: Terminating the code.
flowchart TD
stop(["End"])
To end our code, we add that statement return 0; to tell the compiler that this is the end of our code.
Modern C++
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = a + b; cout << sum; return 0;}
Finetuning
We can also use some information that may allow us to clean our code!
Detailed Outputs
Aside from simply displaying the sum, we can add more information by explicitly letting the user know that the resulting value is a sum.
We can do this by writing them like text then we join them with <<
- For example, the statement cout << "The sum of " << a << " and " << b << " is " << sum; will give us The sum of a and b is sum replacing whatever a or b or their sum sum be.
sum-detail.cpp
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = a + b; cout << "The sum of " << a << " and " << b << " is " << sum; return 0;}
Newlines:
A newline simply says that we are telling the computer that we want to output this section in the next line in the terminal.
For instance, we went to insert a newline between the outputs Enter the second number: and The sum of ...
We would instead put \n before "The sum of ..." in line 11.
sum-detail.cpp
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = a + b; cout << "\nThe sum of " << a << " and " << b << " is " << sum; return 0;}
endlmanipulators
Speaking of newlines, we use endl to add a newline at the end of an output.
We usually do this so that the outputs get more organized.
To show this, we modify the code to output the average of the average of the two numbers as well.
avg-no-endl.cpp
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0, avg = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = a + b; avg = sum / 2; cout << "\nThe sum of " << a << " and " << b << " is " << sum; cout << "\nThe average of " << a << " and " << b << " is " << avg; return 0;}
avg-with-endl.cpp
#include <iostream>using namespace std;int main () { int a = 0, b = 0, sum = 0, avg = 0; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = a + b; avg = sum / 2; cout << "\nThe sum of " << a << " and " << b << " is " << sum << endl; cout << "\nThe average of " << a << " and " << b << " is " << avg; return 0;}
Exercises
Convert the following flowcharts and programs into executable C++ code.
Input two numbers n1 and n2.
Display the sum, difference, product, and the quotient of the two numbers.
#include <iostream>using namespace std;int main () { int length = 0, width = 0, area = 0, perimeter = 0; cout << "Enter the length of the rectangle";cin >> length; cout << "Enter the width of the rectangle"; cin >> width; area = length * width; perimeter = 2 * (length + width); cout << "The area of a " << length << " by " << width << " rectangle is " << area << endl; cout << "The perimeter of a " << length << " by " << width << " rectangle is " << perimeter << endl; return 0;}
Input the radius of a circle.
Display its diameter, area, and circumference.
#include <iostream>using namespace std;int main () { int radius = 0, diameter = 0, circumference = 0, area = 0; cout << "Please enter the radius of the circle: "; cin >> radius; diameter = radius * 2; circumference = 3.14159 * diameter; area = 3.14159 * radius * radius; cout << "The diameter of a circle with radius " << radius << "is " << diameter << endl; cout << "The circumference of a circle with radius " << radius << "is " << circumference << endl; cout << "The area of a circle with radius " << radius << "is " << area << endl; return 0;}
(From ComProg Q1 Activity (09-19-24)) modified
Input the employee number, rate per hour, and the number of hours worked. Compute and display the basic salary by multiplying the rate per hour and number of hours worked. Compute and display also the SSS deduction which is 20% of the basic salary, PAG-IBIG deduction amounting to 200 pesos, PhilHealth deduction which is 5% of the basic salary, tax deduction which is 16% of the basic salary. Compute and display also the total deductions by adding all deductions (SSS, PAG-IBIG, PhilHealth, and tax). Compute and display the net pay by subtracting total deductions from basic salary.