C++ Basics

C++Install Compiler → C++ Basics

Welcome to the C++ Basics. Write your first C++ program and run it through C++ Compiler.

Write your first C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "I have been studying C++ for 4 weeks!" << endl;
return 0;
}

In our first C++ program, we see that we can write letters, numbers and special characters (+, !) on the screen.

How to change line (How to start new line)?
For example, Print the following two lines:
My name is Bilal.
I am learning C++ programming.

#include <iostream>
using namespace std;
int main()
{
cout << "My name is Bilal.\nI am learning C++ programming." << endl;
return 0;
}

\n breaks the line. The red color in the above code was just to highlight \n.

Another way to write the above program is

#include <iostream>
using namespace std;
int main()
{
cout << "My name is Bilal." << endl;
cout << "I am learning C++ programming." << endl;
return 0;
}

String
String is the name of variable, and it could be anything letters, numbers and special characters. We can use string for numbers also.

String Concatenation
We can add two strings, called string concatenation. For example, we are adding first name and second name to get full name.

Also check the difference between the following two programs.

#include <iostream>
using namespace std;
int main()
{
string a = "Bilal ";
string b = "Aslam";
string c = a + b;
cout << c << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
string a = "Bilal ";
string b = "Aslam";
string c = a + b;
cout << "c" << endl;
return 0;
}
Output: Bilal Aslam
cout << c << endl;
Output: c
cout << “c” << endl;

c without ” ” will print the value of c, and “c” will print c.

String Concatenation (with numbers)

#include <iostream>
using namespace std;
int main()
{
string a = "4 ";
string b = "8";
string c = a + b;
cout << c << endl;
return 0;
}

The above program only shows 4 8, and does not 12. Because strings does NOT perform arithmetical operations. For arithmetical operations we can use integers, decimals etc. as variables.

NEXT: C++ Arithmetical Operations