C++ tutorial: Step-by-Step Complete Guide
Updated on Feb 17, 2025 | 6 min read | 5.6k views
Share:
For working professionals
For fresh graduates
More
Updated on Feb 17, 2025 | 6 min read | 5.6k views
Share:
Table of Contents
C++ is a general-purpose programming language that is the successor of the C language and was developed for enhancing it by including an object-oriented paradigm. It is a compiled and imperative mid-level language. This gives it an added advantage of effective and easier low-level programming for drivers, kernels, and the like to high-level applications such as games, desktop applications, etc. The basic code structure and syntax of both C++ and C are the same.
The key advantages of using C++ programming are as follows:-
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Many libraries in the C language have functions predefined to make programming much easier. Header files are the files containing the set of standard predefined library functions. To use a header file in programming, you must include it with the C preprocessing directive “#include”. Header files have a ‘.h’ extension In C++. However, unlike C, not all header files need to end with the “.h” extension.
The Syntax for using header files in C++ and C are:-
#include <filename.h>
or
#include “filename.h”
Here is an example of how to save a file with .h extension:-
// Function to find the sum of two
// numbers passed
int sumOfTwoNumbers(int a, int b)
{
return (a + b);
}
To include your header file with the syntax “#include”, you can follow the below example:-
// C++ program to find the sum of two
// numbers using function declared in
// header file
#include “iostream”
// Including header file
#include “sum.h”
using namespace std;
// Driver Code
int main()
{
// Given two numbers
int a = 15, b = 45;
// Function declared in header
// file to find the sum
cout << “Sum is: “
<< sumOfTwoNumbers(a, b)
<< endl;
}
Output:-
Sum is: 60
Libraries, on the other hand, have an object code linked to an end-user application, after which they become a part of an executable. In Windows .lib extension is used, and for macOS, the .a extension is used. Dynamic libraries end with the .lib or .dll extensions.
Named storage that programs can manipulate is a variable, and they have a specific type in C++ that can be distinguished by the memory size, the layout, the range of values stored, and the set of operations applied to the variable.
The variable name can comprise digits, letters, and the underscore. It has to begin with either an underscore or a letter. C++ is case-sensitive. Down below are the basic types of variables in C++:-
Type & Description
Conditional statements specify if a different statement or block of statements should or should not be executed. They are often called “selection constructs”. The two types of general conditionals are the “switch… case” and “if…then” construct. The usual logical conditions used in C++ from mathematics can be used for performing various actions for various decisions.
The following conditional statements are used in C++:
“If” is used for specifying a code block for execution if a specified condition is true. The syntax used is:
if (condition) {
// block of code to be executed if the condition is true
}
“Else” is used for specifying a code block for execution, if the condition is false. The syntax used is:-
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
“Else if” is used for specifying a new condition to test, if the first condition is false. The syntax used is:-
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Control flow or flow of control is the sequential order of instructions, function calls, and statements executed or evaluated while running a program. Inside your code, while using C++, the statements are sequentially executed from top to bottom, in the order.
Array elements in C++ can be initialized one at a time or by using a single statement like so:-
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
In C++, functions are used to calculate anything from a particular input and can be either predefined or user-defined. There is a code block in a function that performs specific tasks or functions. Unlike functions, a particular set of instructions or commands, on the other hand, is called a procedure.
C++ is an extremely powerful mid-level programming language that makes it much closer to the system hardware and system resources than other compiled languages. Thus, C++ can be used to build IT architecture, advanced computational tools, foundational software, database software, gaming, etc. When it comes to career prospects, having extensive knowledge about C++ will give you the leverage you will need. The Executive PG Program in Software Development from upGrad is a great place to start if you are looking for a reliable course.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources