What is C++ ?
C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup at Bell labs in 1979, as an extension to the C language.
C++ 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++11, C++14, and C++17.
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 a general purpose coding 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# and Java, it makes it easy for programmers to switch to C++ or vice versa
A simple C++ program to display "Hello, World!" on the screen. Since, it's a very simple program, it is often used to illustrate the syntax of a programming language.
#include < iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}
Output:
Hello, World!
All variables use data-type during declaration to restrict the type of data to be stored.
Therefore, we can say that data types are used to tell the variables the type of data it can store.
Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory.
Primitive Datatypes in C++
Integer
Keyword used for integer data types is int.
Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647.
Character
Character data type is used for storing characters. Keyword used for character data type is char.
Boolean
Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false. Keyword used for boolean data type is bool.
Floating Point
Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used for floating point data type is float.
Double Floating Point
Double Floating Point data type is used for storing double precision floating point values or decimal values. Keyword used for double floating point data type is double.
void
Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which does not returns a value.
Wide Character
Wide character data type is also a character data type but this data type has size greater than the normal 8-bit datatype. Represented by wchar_t.
Variables are containers for storing data values.
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
Syntax:
type variable = value;
Example:
int myNum = 15;
cout << myNum;
Output
15
In general, the scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can we accessed or declared or worked with.
There are mainly two types of variable scopes:
Local Variables
Variables defined within a function or block are said to be local to those functions.
- Anything between ‘{‘ and ‘}’ is said to inside a block.
- Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.
- Declaring local variables: Local variables are declared inside a block.
-
Global Variables
As the name suggests, Global Variables can be accessed from any part of the program.
- They are available through out the life time of a program.
- They are declared at the top of the program outside all of the functions or blocks.
- Declaring global variables: Global variables are usually declared outside of all of the functions and blocks, at the top of the program. They can be accessed from any portion of the program.