In this blog, I will go through some important C++ concepts.
Declaration and Initialisation
Variables can be declared & initialized at once or separately as
int z = 100; // declaration & initializationint y; // declaration only
y = 120; // initialization onlyint x = 12;
int x = 23; // not allowed
The declaration can happen ONLY once, while initialization or assignment of values can happen multiple times.
Namespaces
Namespaces are used to group or define a scope for functions, variables, classes, etc.
It is also used to resolve name conflicts, i.e there can be variables with the same name in two different namespaces. (This happens more often than not, coming up with unique, and meaningful variable names can be difficult 😅 )
Find more about namespaces here
namespace space_one{
void print(){ // has a function print()
std::cout<<" This is space one \n";
}
}namespace space_two{
void print(){ //the same name print() is used here too
std::cout<<" This is space two \n";
}
}int main() {
space_one::print(); //Uses the function from space_one // output is : This is space one
space_two::print(); //Uses the function from space_two
// output is : This is space two return 0;
}
It is a good practice to reference the name of the namespace using ::
rather than declaring the use of a namespace at the start of a program.
cout and cin
cout is an object of the ostream class, and cin is an object of the istream class
Header files, Implementation files, Main file (optional)
As programs grow in size, it is practically impossible to maintain the entire code in a single file, hence a common approach followed is :
- Maintain header files with a .h extension, that contains the declaration of the functions/classes used
- Implementation files, with a .c++ extension, that provide the implementation of the declarations in the header files
- Main file, with a .c++ extension, is used optionally when an executable needs to be created, where the entire flow of the program is written, using utilities from the header files. However, if a library is to be created, the Main file may not be necessary.
Consider an example, where a header file including the addition and subtraction operations are defined, an implementation file is used to declare these functions, and finally the main file to invoke the addition and subtraction operations
utilities.h
int add(int, int);
int subtract(int, int);
utilities.c++
#include "utilities.h" //include the user-defined header fileint add(int x, int y) {
return x + y;
}int subtract(int x, int y) {
return x - y;
}
main.c++
#include<iostream>
#include "utilities.h"int main(){ std::cout << "Addition of 3, 5 is " << add(3, 5) << std::endl;
std::cout << "Subtraction of 3, 5 is " << subtract(3, 5) << std::endl;
return 0;}
#include<iostream> VS #include “utilities.h”
Both of these are header files, yet both of these are included in a different way, the reason being the source code for these header files are present at different locations
#include<iostream> is a system header, found at /usr/include
While #include “utilities.h” is located in the scope of the project
Linking multiple files
An obvious question that may arise is, how are these various files linked together?
Earlier when the entire code was in a single file, there was no dependency on other files, however now there is a dependency on other files, an intelligent way is to use makefiles, I have covered more about make files here
References:
- https://www.youtube.com/watch?v=_bYFu9mBnr4&list=PL_c9BZzLwBRJVJsIfe97ey45V4LP_HXiG (One of the best, covering a variety of topics)