In this article of the cpp programming, we will discuss template, its functions, and its classes.
In generic programming, it is a very powerful and important part of the cpp language.
Why template used?
For generic programming,this allows the programmer to use one code for different types rather than write the same code for the different data types. This feature makes it powerful, the code is reusable and flexible.
Let's understand the concept:
Template function:
The function is not different from the regular function but only one difference. It works for different data types.
The regular function works with only a specific type. For different data types, regular function again define for different data types.
The template function defines only one time and uses for different data types.you can do the same work with less code.
How to declare the function template?
Keyword:
Template
The keyword used at the start, then followed by parameters inside the ( ) with the class keyword.
The syntax of a template function
Typename keyword also used in the placed of the class keyword. From the above syntax T accept the arguments of different data types such as int, float, and char, etc.
Examples of the Template functions:
// function
#include <iostream>
using namespace std;
template <class T>//create function
T GetMin (T num1, T num2) {//function declaration and definition
T res;
if(num1<num2)
{
res=num1;
}
else
{
res=num2;
}
return (res);
}
int main () {
int i=5, j=6, firstnum;//declare and define integers
long l=18, m=8, secondnum;//declare and define long variables
firstnum=GetMin<int>(i,j);//calling the function
secondnum=GetMin<long>(l,m);//calling the function
cout << firstnum << endl;
cout << secondnum << endl;
return 0;
}
Template class:
Likewise, function we can also make the generic class in the cpp.
Regular class work with only specific data type but template work with the different data types.
In the class case, the class template is more important than a function because it is hard to write again the whole class code for the different data types.
How to declare the template class?
template<class T>
Class classname{
private:
T variable;
public:
T function(T argu);
};
The T is a template argument that is a place holder of the data type. Inside the class where you write T instead of data type then data type work for all data types. In the above code snippet, T is used for the variable data type, Function return data type, and data type of argument.In these places, we use different data types.
How to create the object of the template class?
For the creation of the object of the class, write the data type inside the < > brackets before the object name.
In this example, two different data types tested on the same class. The circle class has two parameters with two member function diameter and area. Declare and define the one object by passing int in the brackets.
second object with the float data type. Automatically, the output of int arguments and float arguments in respective data types. The reusability is increased in this way.we use the different data type with one class
Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.
HOW TO COUT IN C++ In this article, you will learn how to cout in CPP and use of cout in the program with the help of Examples. In the CPP language displaying things on the console is not a difficult task. First of all build-in library add on the top of the program. The library is used for cout and cin in the cpp language is iostream library. The syntax of the cout in cpp: cout<<" your message "; The universal problem about to print hello world in c++.Here is an example of how to print hello world in cpp. Print hello world #include<iostream> using namespace std; int main() { cout<<" hello world"<<endl; } output: hello world
Cin in CPP In this post, we shall learn about how to take input with the help of in the cin and uses it in the CPP language. Taking input in cpp is not a difficult task. The object which is used for taking input in cpp is the cin . The library which is allowed to use the cin object is the iostream. Method for including the iostream library in the program. The syntax is here: #include<iostream> How to take input in c++? CIN is used for taking any kind of data from the user. The data may be int, float, and char.cin is also used to taking string in the CPP. If someone learns CPP then cin is important to use because it allows to user to interact with your program. The program will be user friendly and generic by using a cin object.
In this blog post, we will explore some of the key concepts and techniques used in Flutter layout and provide examples of how to build a layout for a Flutter application. Widgets and Elements Widgets are the building blocks of a Flutter UI. A widget can be thought of as a self-contained unit of UI, such as a button or a text field. Elements are the instances of widgets that are displayed on the screen. For example, if you have a button widget in your Flutter UI, there will be an element that represents that button on the screen. Layout Widgets Layout widgets are widgets that are specifically designed to control the positioning and sizing of other widgets in a Flutter UI. The most commonly used layout widgets in Flutter are: Container: The Container widget is used to create a rectangular box in a Flutter application. It can be used to add padding, margins, and borders to the widget it contains. Row: The Row widget is used to display a horizontal row of widgets in a Flutter application.
Comments
Post a Comment