Recently, I decided to learn C++. I already know JavaScript and TypeScript and use them in my day job extensively. So to learn a new language from scratch doesn’t make sense, that to C++. A language that is hardly used in Web Development.
My first coding language was JavaScript. Although it holds many coding concepts, it still lacks a whole lot of them. A language like C++ is perfect for understanding some of those programming concepts. Then, there’s the use of language in solving data structures. So it makes sense to learn either C++ or Java to understand those concepts properly.
First Program
So let’s start with writing the “Hello World”
program in C++.
#include<iostream>
#include<string>
using namespace std;
int main() {
string first_name = "John ";
string last_name = "Doe";
string full_name = first_name.append(last_name);
cout << full_name.length() << endl; // 8
cout << full_name.size() << endl; // 8
}#include<iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 1;
}
This is the first program anyone learning to code in C++ would write. So, let’s understand, each and every term inside this program.
#include<iostream>
is a header file library, which lets us use the input, and output-related functionalities inside our program.
using namespace std
does what its name suggests. here are two things that you need to concern yourself with. namespace is a declarative region, that provides us a way to group the related name. std stands for the standard and it is a part of the C++ standard library.
When we include a library, we get a lot of names that we can assess through the C++ standard library using the std namespace. That would generally look something like this std:: cout
but since we are doing using namespace std;
we don’t need to do that anymore.
main()
is our primary function. It is basically a special function that serves as the entry point for program execution. When you try to execute a C++ program, main()
a function is executed first.
int
before the main function is the return type of the main function. In C++ the main function is required to have the return type of int
. The returned value serves as an indication of the program’s execution status.
cout
pronounced as see-out is an output object provided by iostream
the header library file. it allows us to print something. It is generally used with insertion operators <<
. Here we are passing Hello World, as a string.
In C++ a char or character is enclosed in single quotes, whereas a string is enclosed in double quotes. “Hello World”
is a string here. endl
is just marks the end of a line or to say it breaks the line and forces anything we may print after the first cout
into the new line.
So that is your first, C++ program.
Variables
A variable is a container or in the case of C++ is an identifier that is used to find some stored value. Here are some rules to define a variable:
- Names can contain letters, digits, and underscored.
- Names must begin with a letter or underscore.
- Names are case sensitive ( mandatory, Mandatory are two different names)
- Names can not contain white space or special symbols.
- Reserved words can’t be used as names in C++.
Datatypes
Datatype is the type of data, that we are storing in a variable
.
Int in C++ numbers are represented by int
datatype. It includes all positive and negative numbers. But not the numbers with decimals.
Float and Double in C++ we have float
and double
datatypes to store numbers with decimals. the accuracy of float
is not that great when compared to double
datatype. So it’s best to use double
for most of the decimal-related calculations.
char in C++ characters are represented by char datatype. It is enclosed in Single Quotes and generally is good for holding only one character at a time.
String in C++ string is not an inbuilt datatype, it is provided to us by <string> header library file. All strings are enclosed in double-quotes.
bool in C++ bool is used to represent a variable with only two states, those states can either be, Yes-No, 1–0, or true-false.
Input and Output
For output, we have cout (pronounced as see-out) in C++, which is an Object that is used with the insertion operator ( << ).bFor taking input, from the user, we have something called cin which is also an object that is used with the extraction operator ( >> ).
More on Strings
Strings in C++ are Objects, which gives us some extra functionalities to work with.
#include<iostream>
#include<string>
using namespace std;
int main() {
string first_name = "John ";
string last_name = "Doe";
string full_name = first_name + last_name;
cout << full_name << endl; // John Doe
}
This is a simple string concatenation in C++. Now let’s see another way to do this, which is by using append
method.
#include<iostream>#include<iostream>
#include<string>
using namespace std;
int main() {
string first_name = "John ";
string last_name = "Doe";
string full_name = first_name.append(last_name);
cout << full_name.length() << endl; // 8
cout << full_name.size() << endl; // 8
}
#include<string>
using namespace std;
int main() {
string first_name = "John ";
string last_name = "Doe";
string full_name = first_name.append(last_name);
cout << full_name << endl; // John Doe
}
furthermore, we have length or size methods as well. You can use any of the two.
#include<iostream>
#include<string>
using namespace std;
int main() {
string first_name = "John ";
string last_name = "Doe";
string full_name = first_name.append(last_name);
cout << full_name.length() << endl; // 8
cout << full_name.size() << endl; // 8
}
well, that’s it for the day, will see you in the next one.