What is the using Keyword in C++?
In C++, we can use the using keywords for the aliasing of data types and namespaces, inheritance, and declaration. Here I have discussed them one by one in the following C++ code examples.
Example 1: Data Type Alias with using Keyword
The keyword using is utilized in C++ to create an alias for data types, member functions, or classes. The following example shows the alias of my_int for data type int:
using my_int = int;
int main() {
my_int num = 5;
std::cout << "num = " << num << std::endl;
return 0;
}
This code first declared a variable num of type my_int which is an alias of int and initialized it with the value 5 in the main() method. The value of num is then printed to the console using std::cout as below:
Example 2: Namespace Alias with using Keyword
In C++ a namespace alias can be created with the using keyword. Now below is the code implementation which creates a namespace alias my_alias for my_namespace with the function my_method:
namespace my_namespace {
class my_class {
public:
void my_method() {
std::cout << "Linux-Hint" << std::endl;
}
};
}
using my_alias = my_namespace::my_class;
int main() {
my_alias myInstance;
myInstance.my_method();
return 0;
}
In the Main() method, we instantiate my_alias (which is my_class) and call the my_method on it. The “Linux-Hint” will be the output:
Example 3: Inheritance with using Keyword
The using keyword in C++ can be used to bring base class members into a derived class, essentially inheriting those members. This is known as name reuse inheritance or utilizing inheritance. Let’s see it with the implementation of C++:
class Base {
public:
void my_function2() {
std::cout << "my_function2 from Base Class" << std::endl;
}
};
class Derived: public Base {
public:
using Base::my_function2;
void my_function1() {
std::cout << "my_function1 from Derived Class" << std::endl;
my_function2();
}
};
int main() {
Derived d;
d.my_function1();
return 0;
}
Derived's my_function1 member function uses the inherited my_function2 member to call my_function2 from Base. This shows how to use the using keyword to bring members from a base class into a derived class for reuse. When the main function is executed, it calls the my_function1 that prints the “my_function1 from derived class” and calls the my_function2 which is from the base class, and printed the message as “my_function2 from Base Class” as shown below:
Example 4: Declaration with using Keyword
To include a specific name via a namespace into the present scope, use the using keyword. The below code example takes the std::cout object defined in the current scope:
using std::cout;
int main() {
cout << "Hi, How are you" << std::endl;
return 0;
}
Here, the using keyword is declared before the main function, which makes the cout object visible from the std namespace. It makes the cout directly used to print the message “Hi, How are you” without the prefix std::. But endl can only use with the prefix std:: as it is not declared before the main function with the using keyword. As the below-given output is simply to print out the message on the console using the cout:
Conclusion
The using keyword is a useful feature that enables more compact and understandable code, particularly when interacting with lengthy and complicated type names. In the above article, we have seen C++ program examples that used the “using” keyword in different ways.