Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Template in C++

Date- Dec 09,2023

3194

Templates in C++ allow you to construct generic code that can deal with multiple data types without having to replicate the code for each one. In C++, templates are fundamental for constructing flexible and reusable classes and functions. In C++, there are two types of templates: function templates and class templates.

1. Function Templates:

Function templates allow you to create generic functions that can operate on different data types. You define a function template by specifying a placeholder for the data type. Here's a basic example of a function template that swaps two values of any data type:



#include<iostream.h>

#include<conio.h>

template<class T>

void display(T x,T y)

{

T result;

result=x+y;

cout<<"Addition is\n"<<result<<endl;

}

void main()

{

clrscr();

display(4,5);

display(5.7,6.7);

getch();

}




2. Class Templates:

Class templates allow you to define generic classes. You can create a class template with one or more template parameters. Here's a simple example of a class template for a stack data structure:

#include<iostream.h>

#include<conio.h>

template<class T>

class A

{

T a;T b;

public:

void display();

void print();

};

template<class T>

void A<T>::display()

{

cout<<"enter the value of a\n";

cin>>a;

cout<<"Enter the value of b\n";

cin>>b;

}

template<class T>

void A<T>::print()

{

cout<<a<<endl<<b;

}

void main()

{

clrscr();

A<int> a1;

a1.display();

a1.print();

getch();

}








S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page