Posts

Showing posts from October, 2019

C++ Program to Print Number Entered by User

Example: Print Number Entered by User #include <iostream> using namespace std ; int main () { int number ; cout << "Enter an integer: " ; cin >> number ; cout << "You entered " << number ; return 0 ; } Output Enter an integer: 23 You entered 23 This program asks user to enter a number. When user enters an integer, it is stored in variable  number  using  cin . Then it is displayed in the screen using  cout

C++ Templates ?

Image
C++ Templates Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates. Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs. The concept of templates can be used in two different ways: Function Templates Class Templates Function Templates A function template works in a similar to a normal  function , with one key difference. A single function template can work with different data types at once but, a single normal function can only work with one set of data types. Normally, if you need to perform identical operations on two or more types of data, you use function overloading to create two functions with the required function declaration. However, a better approach would be to use function templates because you can perform the same task writing less and maintainable co