Welcome to my new series on C++ for beginners. In this post, I will give you an introduction to the C++ language, compilation stages, a simple hello world program, and what it all means. In addition, I will give an overview of what are variables, what are the built-in types and how one can use them in the program with examples.
Table of Contents
What is C++?
C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language. It follows the object-oriented paradigm. In other words, it includes encapsulation, polymorphism, and inheritance behaviors.
Compilation & Execution
When a C++ program compiles and executes it goes through various phases.
Firstly, The source file is converted into an object file. Before the first step, If there are preprocessor directives then those are processed first. Secondly, the object file is generated the linker will combine the object file and resolve all the dependencies by including any external libraries which might be included in the program. Thirdly, the executable is created after this process. Fourthly, When the user runs the executable the program will reside in memory.
A simple hello world program
For instance, A hello world program consists of a return type of the function called ‘main’. The main function like you see below is the entry point of our program. It may or may not take any arguments. It can take input arguments from the user which is indicated as int argc and char *argv[]. argc is the total number of arguments that is present in char * argv[] array where the user has given the input in the form of char array (also can be considered a string).
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World"<<endl;
return 0; //0 means success
}
As you can see the return type of main is always an int and the program returns 0 on successful execution. I use cout to print “Hello World” to the console of the C++ program. A namespace called std defines cout, endl and many more features. Thus in the beginning we use this namespace to indicate that we will be calling those features below.
Variables and built-in datatypes in C++
We define a variable as a name that is given to a data type to store a value. It is a variable because it can change during the lifetime of the program.
For instance, an example below where x can be considered as a variable of type integer and containing a value 190.
int x = 190;
Built in datatypes
In this paragraph, I will discuss built-in datatypes in C++. A built-in data types are some of the basic datatypes whose storage units are defined by the C++ system.
An int and float consume 4 bytes of memory in the system. A char consumes 1 byte and a string is an array of characters. Similarly, a double datatype will consume 8 bytes of memory.
For instance, I will give you an example of various data types that can be used.
#include <iostream>
#include <string>
using namespace std;
/* This is a function called Introductory Session,
which describes the usage of various data types in C++*/
void introductorySession()
{
int a = 100;
char myfirstChar = '$'; //pair of single quote
string number = "8";
//string to an integer
int res = a + stoi(number); //100 +8 = 108
cout << "Res : " << res << endl;
int b =89, c =-1, d;
b = b + 1; //90
float myfirstfloat = 12.56;
double myfirstdouble = 67.8900000;
//boolean datatype
bool isValid = true;
cout << "Hello World!" << endl;
cout << " I'm Learning C++ ";
cout << " I am printing my first variable " << a << endl;
cout << "My first character is " << myfirstChar << endl;
cout << "My first float is " << myfirstfloat << endl;
cout << "My first double is " << myfirstdouble << endl;
cout << "My first bool is " << isValid << endl;
//Change the value of isValid boolean variable to false
isValid = false;
cout << "Is it valid? " << isValid << endl;
//auto implies a data type based on the value assigned to it.
//myname is implied as a string and x is an integer
auto myname = "Telestreak, A streak of communication";
auto x = 9;
//Intro to strings
string firstString = "Good ";
string secondString = " Afternoon";
int time = 4;
cout << " My first String is " << firstString << endl;
cout << " My second String is " << secondString << endl;
string result = firstString + secondString; //Good Afternoon
cout << " My result string: " << result << endl;
//int to string : to_string, stoi: string to int
result = firstString + secondString + " The time now is " + to_string(time) + " o clock";
cout << result << endl;
}
//**********************OUTPUT******************
Res : 108
Hello World!
I'm Learning C++ I am printing my first variable 100
My first character is $
My first float is 12.56
My first double is 67.89
My first bool is 1
Is it valid? 0
My first String is Good
My second String is Afternoon
My result string: Good Afternoon
Good Afternoon The time now is 4 o clock
There are a lot of details in the above-mentioned program, moreover, look carefully and understand how the program works. The output is added at the end. In addition, I have also included another type called auto which automatically deduces the data type based on the value assigned. Try the program yourself, However, if you need please feel free to ask me questions or leave a comment. Lastly, I have added comments with a single line being two double slashes(//) and multi-line comments being /* and ending with */.
Conclusion
In conclusion, we went through the following topics. Most importantly, these are the basic foundations of the C++ language. The user-defined data types are equally important. I will discuss more later.
- Brief of C++
- phases of C++ program
- A simple hello world program
- Variables
- Datatypes like string, int, double, float, bool and many more
- auto
- conversion of string to integer and int to string
- printing out statements in C++ using cout.
- single-line(//) and multiline comments(/**/)
After this, The next topic is Conditionals and loops.