+ 13
May you please explain three things in C++?
What are Generic Types and how do I use them? What is the #define used for and how do I use it? What is std :: cout , std :: endl etc, used for?
6 odpowiedzi
+ 13
@Kinshuk Vasisht Thank you!
+ 6
1) #define Preprocessor directive
The #define preprocessor directive allows one to declare global scoped constants or macros that are substituted at the preprocessor level.
Basically, define helps declare constants.
Eg :
#define Pi 3.14159
int main() { cout<<Pi<<endl; }
These constants are not variables, as they are substituted at the preprocessor level. The preprocessor first substitutes the values of the define variables and then passes a modified code to the compiler.
Thus, the compiler was passed :
int main() { cout<<3.14159<<endl; }
Similarly, define can be used to declare a macro, which are special functions that are literally substituted like define constants before compilation.
Eg:
#define Square(x) x*x
int main() { cout<<Square(5)<<endl; }
No semicolons are required at the end of a define command and no types are to be specified for the variable x. So they are generic in their own way.
But macros are treated literally and so, you cannot use return inside them. The preprocessed code is:
int main() { cout<<5*5<<endl; }
Macros are used for smaller functions and to make the code neater.
Also, it is advised to use brackets inside a macro, like this :
#define Square(x) ((x)*(x))
This is as if you accidentally passed a complex statement to the earlier version, you get a weird result.
Eg - Square(5+4) // Evaluated as 5+4*5+4
// Returns 29, not 81.
+ 4
0) Generic Types
The concept of generic types allows the user to declare a type that can be substituted later into a different type as per the need of the program.
Thus, a generic type allows the user to declare a function or class for integers, character, etc.
Eg -
Suppose you make a simple max function:
int max(int a, int b) { return a>b?a:b; }
Now this may work when you even supply this:
cout<<max('a','b');
But not when you try :
cout<<max(3.1,3.01);
So you create a new max overload for floats. But as your demand grows, you will have to overload it again and again, and that can get tedious.
So, a solution exists in C++ to use templates (C++ does not have the concept of generic types).
Now to declare a max function for any type (fundamental or a class, union, struct, etc), you do:
template<typename T>
T max(T a, T b) { return a>b?a:b; }
Now at compilation, the type T is substituted by the type of the supplied arguments.
Eg - max('a','b'); max(3.10,3.01); etc.
+ 3
2) std:: namespace scope specfier
The std:: before standard functions is used to access these functions from the standard namespace.
In 1998, C++ introduced namespaces, and as a result, all headers and their functions were incorporated into the standard namespace - std;
So, since all these functions belong to the standard namespace, they must be scoped so that the compiler can use the standard version and not a custom one.
Eg :
std::cout<<"Hello"<<std::endl;
An alternative to avoid typing std:: before the names is to simply add this line in your program:
using namespace std;
This notifies the compiler that all the functions being used are from the std namespace only.
This is a neat approach, but raises ambiguity errors in cases when you overload the functions of the std::namespace by using the same names.
Eg - Declaring a new max() function with same number of arguments.
+ 2
cin is input, cout is output, endl is end line.
+ 1
just use -using namespace std;