- A Brief Intro To C++
- The Timeline Of C++
- Importance Of C++
- Versions Of C++ Language
- Comparison With Other Popular Programming Languages
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Variables In C++?
- Declaration & Definition Of Variables In C++
- Variable Initialization In C++
- Rules & Regulations For Naming Variables In C++ Language
- Different Types Of Variables In C++
- Different Types of Variable Initialization In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Primitive Data Types In C++?
- Derived Data Types In C++
- User-Defined Data Types In C++
- Abstract Data Types In C++
- Data Type Modifiers In C++
- Declaring Variables With Auto Keyword
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Structure Of C++ Program: Components
- Compilation & Execution Of C++ Programs | Step-by-Step Explanation
- Structure Of C++ Program With Example
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What is Typedef in C++?
- The Role & Applications of Typedef in C++
- Basic Syntax for typedef in C++
- How Does typedef Work in C++?
- How to Use Typedef in C++ With Examples? (Multiple Data Types)
- The Difference Between #define & Typedef in C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Strings In C++?
- Types Of Strings In C++
- How To Declare & Initialize C-Style Strings In C++ Programs?
- How To Declare & Initialize Strings In C++ Using String Keyword?
- List Of String Functions In C++
- Operations On Strings Using String Functions In C++
- Concatenation Of Strings In C++
- How To Convert Int To Strings In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is String Concatenation In C++?
- How To Concatenate Two Strings In C++ Using The ‘+' Operator?
- String Concatenation Using The strcat( ) Function
- Concatenation Of Two Strings In C++ Using Loops
- String Concatenation Using The append() Function
- C++ String Concatenation Using The Inheritance Of Class
- Concatenate Two Strings In C++ With The Friend and strcat() Functions
- Why Do We Need To Concatenate Two Strings?
- How To Reverse Concatenation Of Strings In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Find In String C++?
- What Is A Substring?
- How To Find A Substring In A String In C++?
- How To Find A Character In String C++?
- Find All Substrings From A Given String In C++
- Index Substring In String In C++ From A Specific Start To A Specific Length
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Pointers In C++?
- Pointer Declaration In C++
- How To Initialize And Use Pointers In C++?
- Different Types Of Pointers In C++
- References & Pointers In C++
- Arrays And Pointers In C++
- String Literals & Pointers In C++
- Pointers To Pointers In C++ (Double Pointers)
- Arithmetic Operation On Pointers In C++
- Advantages Of Pointers In C++
- Some Common Mistakes To Avoid With Pointers In Cpp
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Understanding Pointers In C++
- What Is Pointer To Object In C++?
- Declaration And Use Of Object Pointers In C++
- Advantages Of Pointer To Object In C++
- Pointer To Objects In C++ With Arrow Operator
- An Array Of Objects Using Pointers In C++
- Base Class Pointer For Derived Class Object In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is 'This' Pointer In C++?
- Defining ‘this’ Pointer In C++
- Example Of 'this' Pointer In C++
- Describing The Constness Of 'this' Pointer In C++
- Important Uses Of 'this' Pointer In C++
- Method Chaining Using 'this' Pointer In C++
- C++ Programs To Show Application Of 'This' Pointer
- How To Delete The ‘this’ Pointer In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What is Reference?
- What is Pointer?
- Comparison Table Of C++ Pointer Vs. Reference
- Differences Between Reference And Pointer: A Detailed Explanation
- Why Are References Less Powerful Than Pointers?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- How To Declare A 2D Array In C++?
- C++ Multi-Dimensional Arrays
- Ways To Initialize A 2D Array In C++
- Methods To Dynamically Allocate A 2D Array In C++
- Accessing/ Referencing Two-Dimensional Array Elements
- How To Initialize A Two-Dimensional Integer Array In C++?
- How To Initialize A Two-Dimensional Character Array?
- How To Enter Data In Two-Dimensional Array In C++?
- Conclusion
- Frequently Asked Questions
- What Are Arrays Of Strings In C++?
- Different Ways To Create String Arrays In C++
- How To Access The Elements Of A String Array In C++?
- How To Convert Char Array To String?
- Conclusion
- Frequently Asked Questions
- What is Memory Allocation in C++?
- The “new" Operator In C++
- The "delete" Operator In C++
- Dynamic Memory Allocation In C++ | Arrays
- Dynamic Memory Allocation In C++ | Objects
- Deallocation Of Dynamic Memory
- Dynamic Memory Allocation In C++ | Uses
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A Substring In C++ (Substr C++)?
- Example For Substr In C++
- Points To Remember For Substr In C++
- Important Applications Of substr() Function
- How to Get a Substring Before a Character?
- Print All Substrings Of A Given String
- Print Sum Of All Substrings Of A String Representing A Number
- Print Minimum Value Of All Substrings Of A String Representing A Number
- Print Maximum Value Of All Substrings Of A String Representing A Number
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Operator In C++?
- Types Of Operators In C++ With Examples
- What Are Arithmetic Operators In C++?
- What Are Assignment Operators In C++?
- What Are Relational Operators In C++?
- What Are Logical Operators In C++?
- What Are Bitwise Operators In C++?
- What Is Ternary/ Conditional Operator In C++?
- Miscellaneous Operators In C++
- Precedence & Associativity Of Operators In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is The New Operator In C++?
- Example To Understand New Operator In C++
- The Grammar Elements Of The New Operator In C++
- Storage Space Allocation
- How Does The C++ New Operator Works?
- What Happens When Enough Memory In The Program Is Not Available?
- Initializing Objects Allocated With New Operator In C++
- Lifetime Of Objects Allocated With The New Operator In C++
- What Is The Delete Operator In C++?
- Difference Between New And Delete Operator In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Types Of Overloading In C++
- What Is Operator Overloading In C++?
- How To Overload An Operator In C++?
- Overloadable & Non-overloadable Operators In C++
- Unary Operator Overloading In C++
- Binary Operator Overloading In C++
- Special Operator Overloading In C++
- Rules For Operator Overloading In C++
- Advantages And Disadvantages Of Operator Overloading In C++
- Function Overloading In C++
- What Is the Difference Between Operator Functions and Normal Functions?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Operators In C++?
- Introduction To Logical Operators In C++
- Types Of Logical Operators In C++ With Example Program
- Logical AND (&&) Operator In C++
- Logical NOT(!) Operator In C++
- Logical Operator Precedence And Associativity In C++
- Relation Between Conditional Statements And Logical Operators In C++
- C++ Relational Operators
- Conclusion
- Frequently Asked Important Interview Questions:
- Test Your Skills: Quiz Time
- Different Type Of C++ Bitwise Operators
- C++ Bitwise AND Operator
- C++ Bitwise OR Operator
- C++ Bitwise XOR Operator
- Bitwise Left Shift Operator In C++
- Bitwise Right Shift Operator In C++
- Bitwise NOT Operator
- What Is The Meaning Of Set Bit In C++?
- What Does Clear Bit Mean?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Types of Comments in C++
- Single Line Comment In C++
- Multi-Line Comment In C++
- How Do Compilers Process Comments In C++?
- C- Style Comments In C++
- How To Use Comment In C++ For Debugging Purposes?
- When To Use Comments While Writing Codes?
- Why Do We Use Comments In Codes?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Storage Classes In Cpp?
- What Is The Scope Of Variables?
- What Are Lifetime And Visibility Of Variables In C++?
- Types of Storage Classes in C++
- Automatic Storage Class In C++
- Register Storage Class In C++
- Static Storage Class In C++
- External Storage Class In C++
- Mutable Storage Class In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Decision Making Statements In C++
- Types Of Conditional Statements In C++
- If-Else Statement In C++
- If-Else-If Ladder Statement In C++
- Nested If Statements In C++
- Alternatives To Conditional If-Else In C++
- Switch Case Statement In C++
- Jump Statements & If-Else In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A Switch Statement/ Switch Case In C++?
- Rules Of Switch Case In C++
- How Does Switch Case In C++ Work?
- The break Keyword In Switch Case C++
- The default Keyword In C++ Switch Case
- Switch Case Without Break And Default
- Advantages & Disadvantages of C++ Switch Case
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A For Loop In C++?
- Syntax Of For Loop In C++
- How Does A For Loop In C++ Work?
- Examples Of For Loop Program In C++
- Ranged Based For Loop In C++
- Nested For Loop In C++
- Infinite For Loop In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A While Loop In C++?
- Parts Of The While Loop In C++
- C++ While Loop Program Example
- How Does A While Loop In C++ Work?
- What Is Pre-checking Process Or Entry-controlled Loop?
- When Are While Loops In C++ Useful?
- Example C++ While Loop Program
- What Are Nested While Loops In C++?
- Infinite While Loop In C++
- Alternatives To While Loop In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Loops & Its Types In C++?
- What Is A Do-While Loop In C++?
- Do-While Loop Example In C++ To Print Numbers
- How Does A Do-While Loop In C++ Work?
- Various Components Of The Do-While Loop In C++
- Example 2: Adding User-Input Positive Numbers With Do-While Loop
- C++ Nested Do-While Loop
- C++ Infinitive Do-while Loop
- What is the Difference Between While Loop and Do While Loop in C++?
- When To Use A Do-While Loop?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are 2D Vectors In C++?
- How To Declare 2D Vector In C++?
- How To Initialize 2D Vector In C++?
- C++ Program Examples For 2D Vectors
- How To Access & Modify 2D Vector Elements In C++?
- Methods To Traverse, Manipulate & Print 2D Vectors In C++
- Adding Elements To 2-D Vector Using push_back() Function
- Removing Elements From Vector In C++ Using pop_back() Function
- Creating 2D Vector In C++ With User Input For Size Of Column & Row
- Advantages of 2D Vectors Over Traditional Arrays
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- How To Print A Vector In C++ By Overloading Left Shift (<<) Operator?
- How To Print Vector In C++ Using Range-Based For-Loop?
- Print Vector In C++ With Comma Separator
- Printing Vector In C++ Using Indices (Square Brackets/ Double Brackets & at() Function)
- How To Print A Vector In C++ Using std::copy?
- How To Print A Vector In C++ Using for_each() Function?
- Printing C++ Vector Using The Lambda Function
- How To Print Vector In C++ Using Iterators?
- Conclusion
- Frequently Asked Questions
- Definition Of C++ Find In Vector
- Using The std::find() Function
- How Does find() In Vector C++ Function Work?
- Finding An Element By Custom Comparator Using std::find_if() Function
- Use std::find_if() With std::distance()
- Element Find In Vector C++ Using For Loop
- Using The find_if_not Function
- Find Elements With The Linear Search Approach
- Conclusion
- Frequently Asked Questions
- What Is Sort() Function In C++?
- Sort() Function In C++ From Standard Template Library
- Exceptions Of Sort() Function/ Algorithm In C++
- The Stable Sort() Function In C++
- Partial Sort() Function In C++
- Sorting In Ascending Order With Sort() Function In C++
- Sorting In Descending Order With Sort Function In C++
- Sorting In Desired Order With Custom Comparator Function & Sort Function In C++
- Sorting Elements In Desired Order Using Lambda Expression & Sort Function In C++
- Types of Sorting Algorithms In C++
- Advanced Sorting Algorithms In C++
- How Does the Sort() Function Algorithm Work In C++?
- Conclusion
- Frequently Asked Questions
- What Is Function Overloading In C++?
- Ways Of Function Overloading In C++
- Function Overloading In C++ Using Different Types Of Parameters
- Function Overloading In C++ With Different Number Of Parameters
- Function Overloading In C++ Using Different Sequence Of Parameters
- How Does Function Overloading In C++ Work?
- Rules Of Function Overloading In C++
- Why Is Function Overloading Used?
- Types Of Function Overloading Based On Time Of Resolution
- Causes Of Function Overloading In C++
- Ambiguity & Function Overloading In C++
- Advantages Of Function Overloading In C++
- Disadvantages Of Function Overloading In C++
- Operator Overloading In C++
- Function Overriding In C++
- Difference Between Function Overriding & Function Overloading In C++
- Conclusion
- Frequently Asked Questions
- What Is An Inline Function In C++?
- How To Define The Inline Function In C++?
- How Does Inline Function In C++ Work?
- The Need For An Inline Function In C++
- Can The Compiler Ignore/ Reject Inline Function In C++ Programs?
- Normal Function Vs. Inline Function In C++
- Classes & Inline Function In C++
- Understanding Inline, __inline, And __forceinline Functions In C++
- When To Use An Inline Function In C++?
- Advantages Of Inline Function In C++
- Disadvantages Of Inline Function In C++
- Why Not Use Macros Instead Of An Inline Function In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Static Data Member In C++?
- How To Declare Static Data Members In C++?
- How To Initialize/ Define Static Data Member In C++?
- Ways To Access A Static Data Member In C++
- What Are Static Member Functions In C++?
- Example Of Member Function & Static Data Member In C++
- Practical Applications Of Static Data Member In C++
- Conclusion
- Frequently Asked Questions
- What Is A Constant In C++?
- Ways To Define Constant In C++
- What Are Literals In C++?
- Pointer To A Constant In C++
- Constant Function Arguments In C++
- Constant Member Function Of Class In C++
- Constant Data Members In C++
- Object Constant In C++
- Conclusion
- Frequently Asked Questions(FAQ)
- What Is Friend Function In C++?
- Declaration Of Friend Function In C++ With Example
- Characteristics Of Friend Function In C++
- Global Friend Function In C++ (Global Function As Friend Function )
- Member Function Of Another Class As Friend Function In C++
- Function Overloading Using Friend Function In C++
- Advantages & Disadvantages Of Friend Function in C++
- What Is A C++ Friend Class?
- A Function Friendly To Multiple Classes
- C++ Friend Class Vs. Friend Function In C++
- Some Important Points About Friend Functions And Classes In C++
- Conclusion
- Frequently Asked Questions
- What Is Function Overriding In C++?
- The Working Mechanism Of Function Overriding In C++
- Real-Life Example Of Function Overriding In C++
- Accessing Overriding Function In C++
- Accessing Overridden Function In C++
- Function Call Binding With Class Objects | Function Overriding In C++
- Function Call Binding With Base Class Pointers | Function Overriding In C++
- Advantages Of Function Overriding In C++
- Variations In Function Overriding In C++
- Function Overloading In C++
- Function Overloading Vs Function Overriding In C++
- Conclusion
- Frequently Asked Questions
- Errors In C++
- What Is Exception Handling In C++?
- Exception Handling In C++ Program Example
- C++ Exception Handling: Basic Keywords
- The Need For C++ Exception Handling
- C++ Standard Exceptions
- C++ Exception Classes
- User-Defined Exceptions In C++
- Advantages & Disadvantages Of C++ Exception Handling
- Conclusion
- Frequently Asked Questions
- What Are Templates In C++ & How Do They Work?
- Types Of Templates In C++
- What Are Function Templates In C++?
- C++ Template Functions With Multiple Parameters
- C++ Template Function Overloading
- What Are Class Templates In C++?
- Defining A Class Member Outside C++ Template Class
- C++ Template Class With Multiple Parameters
- What Is C++ Template Specialization?
- How To Specify Default Arguments For Templates In C++?
- Advantages Of C++ Templates
- Disadvantages Of C++ Templates
- Difference Between Function Overloading And Templates In C++
- Conclusion
- Frequently Asked Questions
- Structure
- Structure Declaration
- Initialization of Structure
- Copying and Comparing Structures
- Array of Structures
- Nested Structures
- Pointer to a Structure
- Structure as Function Argument
- Self Referential Structures
- Class
- Object Declaration
- Accessing Class Members
- Similarities between Structure and Class
- Which One Should You Choose?
- Key Difference Between a Structure and Class
- Summing Up
- Test Your Skills: Quiz Time
- What Is A Class And Object In C++?
- What Is An Object In C++?
- How To Create A Class & Object In C++? With Example
- Access Modifiers & Class/ Object In C++
- Member Functions Of A Class In C++
- How To Access Data Members And Member Functions?
- Significance Of Class & Object In C++
- What Are Constructors In C++ & Its Types?
- What Is A Destructor Of Class In C++?
- An Array Of Objects In C++
- Object In C++ As Function Arguments
- The this (->) Pointer & Classes In C++
- The Need For Semicolons At The End Of A Class In C++
- Difference Between Structure & Class In C++
- Conclusion
- Frequently Asked Questions
- What Are Static Members In C++?
- Static Member Functions in C++
- Ways To Call Static Member Function In C++
- Properties Of Static Member Function In C++
- Need Of Static Member Functions In C++
- Regular Member Function Vs. Static Member Function In C++
- Limitations Of Static Member Functions In C++
- Conclusion
- Frequently Asked Questions
- What Is Constructor In C++?
- Characteristics Of A Constructor In C++
- Types Of Constructors In C++
- Default Constructor In C++
- Parameterized Constructor In C++
- Copy Constructor In C++
- Dynamic Constructor In C++
- Benefits Of Using Constructor In C++
- How Does Constructor In C++ Differ From Normal Member Function?
- Constructor Overloading In C++
- Constructor For Array Of Objects In C++
- Constructor In C++ With Default Arguments
- Initializer List For Constructor In C++
- Dynamic Initialization Using Constructor In C++
- Conclusion
- Frequently Asked Questions
- What Is A Constructor In C++?
- What Is Constructor Overloading In C++?
- Dеclaration Of Constructor Ovеrloading In C++
- Condition For Constructor Overloading In C++
- How Constructor Ovеrloading In C++ Works?
- Examples Of Constructor Overloading In C++
- Lеgal & Illеgal Constructor Ovеrloading In C++
- Types Of Constructors In C++
- Characteristics Of Constructors In C++
- Advantage Of Constructor Overloading In C++
- Disadvantage Of Constructor Overloading In C++
- Conclusion
- Frеquеntly Askеd Quеstions
- What Is A Destructor In C++?
- Rules For Defining A Destructor In C++
- When Is A Destructor in C++ Called?
- Order Of Destruction In C++
- Default Destructor & User-Defined Destructor In C++
- Virtual Destructor In C++
- Pure Virtual Destructor In C++
- Key Properties Of Destructor In C++ You Must Know
- Explicit Destructor Calls In C++
- Destructor Overloading In C++
- Difference Between Normal Member Function & Destructor In C++
- Important Uses Of Destructor In C++
- Conclusion
- Frequently Asked Questions
- What Is A Constructor In C++?
- What Is A Destructor In C++?
- Difference Between Constructor And Destructor In C++
- Constructor In C++ | A Brief Explanation
- Destructor In C++ | A Brief Explanation
- Difference Between Constructor And Destructor In C++ Explained
- Order Of Calling Constructor And Destructor In C++ Classes
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Type Conversion In C++?
- What Is Type Casting In C++?
- Types Of Type Conversion In C++
- Implicit Type Conversion (Coercion) In C++
- Explicit Type Conversion (Casting) In C++
- Advantages Of Type Conversion In C++
- Disadvantages Of Type Conversion In C++
- Difference Between Type Casting & Type Conversion In C++
- Application Of Type Casting In C++
- Conclusion
- Frequently Asked Questions
- What Is A Copy Constructor In C++?
- Characteristics Of Copy Constructors In C++
- Types Of Copy Constructors In C++
- When Do We Call The Copy Constructor In C++?
- When Is A User-Defined Copy Constructor Needed In C++?
- Types Of Constructor Copies In C++
- Can We Make The Copy Constructor In C++ Private?
- Assignment Operator Vs Copy Constructor In C++
- Example Of Class Where A Copy Constructor Is Essential
- Uses Of Copy Constructors In C++
- Conclusion
- Frequently Asked Questions
- Why Do You Need Object-Oriented Programming (OOP) In C++?
- OOPs Concepts In C++ With Examples
- The Class OOPs Concept In C++
- The Object OOPs Concept In C++
- The Inheritance OOPs Concept In C++
- Polymorphism OOPs Concept In C++
- Abstraction OOPs Concept In C++
- Encapsulation OOPs Concept In C++
- Other Features Of OOPs In C++
- Benefits Of OOP In C++ Over Procedural-Oriented Programming
- Disadvantages Of OOPS Concept In C++
- Why Is C++ A Partial OOP Language?
- Conclusion
- Frequently Asked Questions
- Introduction To Abstraction In C++
- Types Of Abstraction In C++
- What Is Data Abstraction In C++?
- Understanding Data Abstraction In C++ Using Real Life Example
- Ways Of Achieving Data Abstraction In C++
- What Is An Abstract Class?
- Advantages Of Data Abstraction In C++
- Use Cases Of Data Abstraction In C++
- Encapsulation Vs. Abstraction In C++
- Conclusion
- Frequently Asked Questions
- What Is Encapsulation In C++?
- How Does Encapsulation Work In C++?
- Types Of Encapsulation In C++
- Why Do We Need Encapsulation In C++?
- Implementation Of Encapsulation In C++
- Access Specifiers & Encapsulation In C++
- Role Of Access Specifiers In Encapsulation In C++
- Member Functions & Encapsulation In C++
- Data Hiding & Encapsulation In C++
- Features Of Encapsulation In C++
- Advantages & Disadvantages Of Encapsulation In C++
- Difference Between Abstraction and Encapsulation In C++
- Conclusion
- Frequently Asked Questions
- What Is Inheritance In C++?
- What Are Child And Parent Classes?
- Syntax And Structure Of Inheritance In C++
- Implementing Inheritance In C++
- Importance Of Inheritance In C++
- Types Of Inheritance In C++
- Visibility Modes Of Inheritance In C++
- Access Modifiers & Inheritance In C++
- How To Make A Private Member Inheritable?
- Member Function Overriding In Inheritance In C++
- The Diamond Problem | Inheritance In C++ & Ambiguity
- Ways To Avoid Ambiguity Inheritance In C++
- Why & When To Use Inheritance In C++?
- Advantages Of Inheritance In C++
- The Disadvantages Of Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Hybrid Inheritance In C++?
- Importance Of Hybrid Inheritance In Object Oriented Programming
- Example Of Hybrid Inheritance In C++: Using Single and Multiple Inheritance
- Example Of Hybrid Inheritance In C++: Using Multilevel and Hierarchical Inheritance
- Real-World Applications Of Hybrid Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Multiple Inheritance In C++?
- Examples Of Multiple Inheritance In C++
- Ambiguity Problem In Multiple Inheritance In C++
- Ambiguity Resolution In Multiple Inheritance In C++
- The Diamond Problem In Multiple Inheritance In C++
- Visibility Modes In Multiple Inheritance In C++
- Advantages & Disadvantages Of Multiple Inheritance In C++
- Multiple Inheritance Vs. Multilevel Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Multilevel Inheritance In C++?
- Block Diagram For Multilevel Inheritance In C++
- Multilevel Inheritance In C++ Example
- Constructor & Multilevel Inheritance In C++
- Use Cases Of Multilevel Inheritance In C++
- Multiple Vs Multilevel Inheritance In C++
- Advantages & Disadvantages Of Multilevel Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Hierarchical Inheritance In C++?
- Example 1: Hierarchical Inheritance In C++
- Example 2: Hierarchical Inheritance In C++
- Impact of Visibility Modes In Hierarchical Inheritance In C++
- Advantages And Disadvantages Of Hierarchical Inheritance In C++
- Use Cases Of Hierarchical Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Are Access Specifiers In C++?
- Types Of Access Specifiers In C++
- Public Access Specifiers In C++
- Private Access Specifier In C++
- Protected Access Specifier In C++
- The Need For Access Specifiers In C++
- Combined Example For All Access Specifiers In C++
- Best Practices For Using Access Specifiers In C++
- Why Can't Private Members Be Accessed From Outside A Class?
- Conclusion
- Frequently Asked Questions
- What Is The Diamond Problem In C++?
- Example Of The Diamond Problem In C++
- Resolution Of The Diamond Problem In C++
- Virtual Inheritance To Resolve Diamond Problem In C++
- Scope Resolution Operator To Resolve Diamond Problem In C++
- Conclusion
- Frequently Asked Questions
C++ Templates | Types, Usage, Overloading & More (+Code Examples)

The C++ templates are a feature of the C++ programming language that makes it possible to carry out generic programming or write generic code. Generic programming is a paradigm for writing reusable, adaptable code that can operate on various data types and heavily relies on templates.
In other words, templates serve as the building blocks for generating the functions or classes rather than making separate functions or classes for each distinct data type. In this article, we will discuss everything related to C++ templates, how they work, how to use them, and more.
What Are Templates In C++ & How Do They Work?
In C++, templates are defined using the template keyword and type parameters, which are substituted with actual data types at compile time.
- C++ templates use placeholders referred to as template parameters to represent different data types, which can be user-defined and built-in types.
- When using a template, the compiler automatically creates the correct version of the function or class for the specified data type.
- A procedure known as template instantiation takes place at compile time, which leads to creating a new function with the specific datatype.
This generates specialized code for each data type, providing flexibility, code reusability, and efficient execution for various scenarios. Then, we'll discuss how templates work in C++ programming.
Working Mechanism Of C++ Templates
- Template Definition: As mentioned before, templates are defined using the template keyword, followed by the template parameter(s) enclosed in angle brackets (<>). Template parameters can represent data types (typename T) or non-type parameters (int N), like array size.
- Template Instantiation: When a function or class template is used with a specific data type (e.g., add<int>(2, 3) or MyVector<double>), the compiler generates the appropriate version of the function or class for that data type. This process is called template instantiation.
- Code Generation: During template instantiation, the compiler generates a unique version of the function or class for each data type used. This avoids the runtime overhead and ensures efficient code execution.
- Type Deduction: In many cases, C++ can deduce the template parameter types automatically based on the arguments provided when calling a function template. This is called template argument deduction.
- Code Reusability: C++ Templates promote code reusability by allowing you to write a single generic function or class that can be used with different data types, reducing the need for redundant code.
- Specialization: You can provide specialized versions of function or class templates for specific data types, enabling custom behavior for those cases.
Let us take a look at a C++ template example to better understand the topic.
Code Example:
#include<bits/stdc++.h>
using namespace std;
template <typename T>
T Max(T a, T b) {
return (a > b) ? a : b;}
int main(){
cout<<Max(10,20)<<endl;
cout<<Max(10.5,20.6)<<endl;
}
Output:
20
20.6
Explanation:
In the simple C++ program above, we first include the <bits/stdc++.h> header, which is a convenience header provided by some C++ compilers to include commonly used standard headers. It includes most of the standard C++ library headers in one go.
- We then use the namespace std statement, which helps bring all the standard library symbols (e.g., cout, endl) into the current scope. It allows you to use these symbols without explicitly specifying the std:: prefix.
- (Function Template Declaration) Then, we declare a function template using the template keyword, and the template typename is T, which serves as the placeholder for the data type of the parameters and return value.
- (Function Definition) Next, we define the function template and name the function Max() using the type name T.
- The function takes two parameters, a and b, of the data type T and returns a value of type T. It first compares the values of a and b using the greater than operator and then returns the larger value. We use the ternary operator (?:) in the expression.
- In the main() function, we call the Max() function with integers 10 and 20 as arguments inside a cout statement.
- Since the template parameter T is deduced to be integer type (int), the function returns the larger integer (20) and prints it.
- We, once again, call the Max() function passing values 10.5 and 20.6 (of type double) as arguments. Here, the template parameter T is deduced to be double, and the function returns the larger double (20.6), which is printed using cout.
Note: C++ deduces the actual data types for the function template based on the argument types provided during the function calls. This is known as template type deduction, which allows the Max function to work with both integers and floating-point numbers without the need for explicit type specification.
Types Of Templates In C++
There are two primary types of templates in C++, i.e., function templates and class templates. We will discuss both of these in detail in the sections ahead, but first, here is a brief description of both:
- Function Templates: Function templates allow you to define a single function that can work with multiple data types. They are defined using the template keyword, followed by template parameters inside angular/ angle brackets <>. These parameters represent the generic types of data on which the function can operate.
- Class Templates: Class templates allow you to create generic classes with various data types. Like function templates, they are defined using the template keyword and template parameters inside angular brackets <>.
Besides the two types mentioned above, there are some other types of C++ templates that aren't commonly used. They are as follows:
- Alias Template (since C++11): An alias template provides a new name (alias) for a template instantiation. It simplifies complex type names and enhances code readability by creating a shorter, more descriptive alias for a specific template instantiation.
- Variable Template (since C++14): A variable template is a blueprint for creating generic variables that can be parameterized with types or non-type values. It allows you to define families of variables that share common properties but can have different types or values.
- Concept (Constraints and Concepts, since C++20): A concept is a set of requirements that define a set of valid template arguments. It enables template constraints, allowing you to specify which types or expressions are valid for a template, resulting in more readable error messages and better template error handling.
What Are Function Templates In C++?
As the name suggests, C++ template functions are those templates that help us create multiple functions of the type defined in the template. This way, a single function can operate on several data kinds, eliminating the need to write separate code for each type. Generic programming extensively uses function templates to improve the reuse and maintainability of code.
Why Use Function Templates In C++ Programs?
Function templates have many uses, such as making standardized functions in generic programming and many more. Some more benefits are :
- They help avoid code duplication.
- Code becomes cleaner and more condensed with the use of the C++ template functions.
- They increase the flexibility and ease of maintenance of programs.
Syntax Of Function Template In C++
template <typename T>
return_type functionName(T parameter) {
// Function code here
// You can use the 'parameter' of type T in the code
// Return value of type 'return_type'
}
Here,
- The template keyword indicates that there is a template declaration, in this case, a function template.
- <typename T> is a template parameter declaration, where T represents the placeholder for the data type. You can use any valid identifier instead of T.
- return_type refers to the data type of the function template's return value.
- The functionName refers to the name of the function template.
- T parameter reflects the function template's parameter, which is of type T.
Defining A Function Template In C++
Function templates use template parameters as placeholders for the data types of the function's arguments and return type. These template parameters are defined using the template keyword, followed by the typename keyword (or class keyword, which is equivalent in this context) and a placeholder name, typically represented by T.
For example:
template <typename T>
T getMax(T a, T b) {
return (a > b ? a : b);}
In this example, T is the template argument which can be any of the different data types (e.g., int, float). The typename keyword is used to indicate that T is a type parameter.
How To Call A Function Template?
To call a function template in C++ (or function template instantiation), you provide specific data types or values as arguments when calling the function. This process is similar to calling a regular function. However, with function templates, the C++ compiler automatically deduces the template arguments based on the function arguments you provide.
This is how a function template serves as a blueprint to generate multiple versions of the function based on the provided types or values. Look at the snippet showing how to call a function template in the main function, given below.
int main() {
int x = 5, y = 10;
int maxInt = getMax<int>(x, y); // Call the function template with int as the data type
float a = 3.14, b = 2.71;
float maxFloat = getMax<float>(a, b); // Call the function template with float as the data type
return 0;
}
In this example-
- The expression getMax<int>(x, y) is the function call, which invokes the function template with int as the actual template parameter and variables x and y as the arguments.
- The function call getMax<float>(a, b) invokes the function template with float as the data type and variables a and b as arguments.
C++ Template Function Example
Now, let's look at a complete C++ program example showcasing how to use a C++ template function with different type parameters.
Code Example:
#include <iostream>
using namespace std;
// Function template to add two numbers of any data type
template <typename T>
T AddNumbers(T a, T b) {
return a + b;}
int main() {
// Creating an AddNumbers function for integer type
int intSum = AddNumbers(5, 7);
cout << "The sum of integers: " << intSum <<endl;
//Calling the AddNumber function for double type
double doubleSum = AddNumbers(3.14, 2.71);
cout << "The sum of doubles: " << doubleSum << endl;
return 0;
}
Output:
The sum of integers: 12
The sum of doubles: 5.85
Explanation:
In the C++ code example-
- We first define a function template AddNumbers, which takes two arguments of type T and returns their sum using addition arithmetic operator.
- Here, T is a placeholder for the data type that will be deduced by the compiler when the function is called.
- In the main() function, we use the AddNumbers function template with both int and double arguments.
- As mentioned in the code comments, we first call the AddNumbers() function with integer type and arguments 5 and 7. The outcome of this function is stored in the intSum variable.
- This creates and invokes the AddNumbers() function for integer type. It calculates the sum of the arguments and returns the same, which we print to the console using the cout statement.
- We again call the AddNumbers() function but with arguments 3.14 and 2.71, which are of type double.
- This invokes the AddNumbers() function for double type. It calculates the sum and the outcome is stored in the doubleSum variable.
- As is evident, the compiler automatically generates specialized versions of the function for int and double, allowing us to add numbers of different data types using the same function template.
- The output for both the function calls is printed to the console using the cout command.
- Lastly, the main() function terminates with a return 0 statement indicating successful execution.
The time complexity of the function template is O(1) as it performs a simple addition.
The space complexity is also O(1) as it uses a constant amount of memory to store the arguments and return value, regardless of the data type used.
Restrictions Of Generic Functions
As you know, generic functions can operate on multiple data types without being limited to a specific type. They are implemented through C++ template functions and are designed to work with a variety of data types, allowing for code reusability and flexibility. However, there are a few shortcomings when using these functions. They are:
- Type Compatibility: Generic functions must work with a broad range of data types. As a result, they are subject to type compatibility constraints. Some operations or expressions might not be valid for all data types, leading to compilation errors. You need to ensure that the operations used in the generic function are supported by all possible data types.
- Overhead: Generic functions can introduce some overhead compared to specialized functions for specific data types. The compiler generates separate versions of the function for different data types, potentially increasing the binary size. However, this overhead is usually minimal compared to the benefits of code reusability.
- Performance considerations: The performance of generic functions can be a little sluggish in comparison to specialized functions. Also, the code produced by the compiler may not be as effective as code designed exclusively for one data type since it supports a variety of data types. This means there is performance disparity, though it is frequently insignificant.
- Code Bloat: Using generic functions extensively with many different data types might lead to code bloat. Each specialization increases the size of the executable, potentially impacting load times and memory usage. It is essential to strike a balance between code reusability and excessive code generation.
- Template Specialization Complexity: As generic functions become more complex, handling template specialization and managing code variations for specific data types can become challenging. Template specialization can sometimes be verbose and harder to maintain.
Despite these restrictions, generic functions remain a powerful tool whose judicious use can help you write more flexible and maintainable code while minimizing code duplication. To make the most of generic functions, it is essential to carefully consider the potential type compatibility issues and performance trade-offs.
C++ Template Functions With Multiple Parameters
Function templates can have multiple parameters of different data types, allowing you to create more flexible and versatile normal functions. They enable you to handle complex identical operations involving multiple data types while still benefiting from code reusability.
Syntax:
template <typename T1, typename T2, ...>
return_type FunctionName(T1 parameter1, T2 parameter2, ...) {
// Function code here
// You can use the 'parameter1', 'parameter2', ... of their respective types in the code
}
Here,
- Like before, the template keyword indicates the declaration of a template.
- <typename T1,typename T2,......> is the template parameter declaration, where T1,T2,... acting as placeholders for the multiple data type. You can have as many parameters separated by a comma.
- The FunctionName and return_type refer to the name of the function and the data type of its return value, respectively.
Code Example:
#include <iostream>
using namespace std;
template <typename T1, typename T2>
auto Multiply(T1 a, T2 b) {
return a * b;}
int main() {
int intNum = 5;
double doubleNum = 3.14;
auto result1 = Multiply(intNum, intNum);
auto result2 = Multiply(intNum, doubleNum);
cout << "Result 1: " << result1 << endl;
cout << "Result 2: " << result2 << endl;
return 0;
}
Output:
Result 1: 25
Result 2: 15.7
Explanation:
- In this example, we defined the function template Multiply using the keyword template, which takes two parameters of different data types, T1 and T2.
- The function mutlitplies the parameters and returns the product value, i.e., a*b.
- In the main() function, we declare and initialize two variables intNum (integer type) and doubleNum (double type) with values 5 and 3.14, respectively.
- We then call the Multiply() function with intNum as both the arguments. The outcome is stored in the result1 variable with type auto.
- Then, we call the Mulitply() function again, but now with doubleNum as both the arguments and the outcome is stored in resutl2 (type auto).
- The function template generates a specialized version of the function, allowing us to multiply integers and mix different data types using the same template.
- In the call Multiply (intNum, intNum), both these arguments are of type int. So both T1, and T2 will be assigned integers as data types.
- Similarly, in the call Multiply (intNum, doubleNum), the template T1 will be assigned integer type, and T2 will be assigned double as data type.
- We print both the results to the console using the cout command.
The time complexity of the function template for multiplication is O(1) because multiplication is a constant-time operation for built-in numeric data types like int and double.
The space complexity of the function template is also O(1) since it uses a constant amount of memory regardless of the data types involved.
C++ Template Function Overloading
Function templates in C++ can also be overloaded, just like regular functions. Overloading a function template allows you to define multiple versions of the template, each tailored to handle specific cases or data types. This enables you to provide specialized implementations for different scenarios while still benefiting from the generic nature of the C++ template.
Syntax:
// Function template
template <typename T>
return_type FunctionName(T parameter) {
// Generic implementation
}// Overloaded function template
template <>
return_type FunctionName<>(T parameter1, T parameter2) {
// Specialized implementation for the given data types
}
Here, the first section of the syntax is the same as the function template declaration.
- For the overloading function template, the keyword template is followed by empty angular brackets <> since it uses the same template parameter as the original function.
- T parameter1 and T parameter2 refer to the specialized data types.
- The curly brackets {} contain specialized implementations for the specific data types within the function body.
Code Example:
#include <bits/stdc++.h>
using namespace std;
// Function template to find the maximum of two elements
template <typename T>
T Max(T a, T b) {
return (a > b) ? a : b;}
// Overloaded function template for handling char pointers
template <>
const char* Max<const char*>(const char* a, const char* b) {
return (strcmp(a, b) > 0) ? a : b;}
int main() {
int intMax = Max(5, 9);
cout << "Max integer: " << intMax << endl;
double doubleMax = Max(3.14, 2.71);
cout << "Max double: " << doubleMax << endl;
const char* str1 = "Apple";
const char* str2 = "Banana";
const char* strMax = Max(str1, str2);
cout << "Max string: " << strMax << endl;
return 0;
}
Output:
Max integer: 9
Max double: 3.14
Max string: Banana
Explanation:
In the example C++ code-
- We first define a function template Max using the template keyword and the template parameter typename T. This template allows the function to work with various data types.
- The function template compares two values, a and b, and returns the larger value of the two.
- Then, we overload the template function by defining the Max() function with constant character pointer or C-style string as data type (template parameter), i.e., const char*.
- This version of the function allows the code to handle C-style strings differently than other data types. It also compares the function parameters and returns the larger of two.
- Note that the C-style strings (character pointers) need special handling because the standard comparison operator (>) cannot be directly used with them.
- To handle this the specialized version uses the strcmp() function to compare the strings and returns the larger string based on lexicographical order.
- Inside the main() function, we use the MAX template function three times. The first two calls reflect the generic use of the template.
- The first call invokes MAX to find the maximum of two integers and the second the maximum of two double type values. The template parameter T is deduced as int and double, respectively.
- Next, we declare two character pointers, str1 and str2, and initialize them with string values Apple and Banana, respectively.
- In the third call to MAX, we pass str1 and str2 as arguments. This invokes the specialized version of MAX.
- The result of all the functions is printed to the console using the cout command.
The time complexity of the function template is O(1) for comparisons and O(N) for C-style string comparison (using strcmp).
The space complexity is also O(1) as it uses a constant amount of memory to store the arguments and return value, regardless of the data types used.
What Are Class Templates In C++?
Class Templates are another way of using templates to reduce the need for code duplication. They help us write generic classes and define a blueprint for a class that can work with different data types. Some of the most common reasons that we use the class templates in C++ programs are as follows:
- When we want to design a generic implementation of a class.
- Using class template specialization, we can write a single implementation of a class and then instantiate it with various data types.
- This reduces code duplication and increases the flexibility and maintainability of code.
How Do C++ Templates Class Work?
The working mechanism of C++ template classes can be summarised in the following points:
- The keyword template is used to define class templates, which are then followed by template parameters enclosed in angular brackets <>.
- For the data types or non-type values that the class will operate on, these template parameters serve as placeholders.
- When you create a new instance of the class, you specify particular data types for the template parameters, and the compiler creates a unique version of the class specifically for those types.
Syntax of Class Templates In C++
template <typename T>
class ClassName {
public:
// Member variables and functions that use types T
};
Here,
- The template keyword indicates that we are declaring a template, and <typename T> refers to the template parameters where T is the placeholder. You can have as many template parameters as needed, each separated by a comma.
- ClassName refers to the name of the class template. You can use any valid identifier as the class name.
- The curly brackets {} contain the class template block consisting of the definition of member variables, class member functions, and other class elements that use the template parameters T1, T2, and so on.
Code Example:
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Stack {
private:
vector<T> elements;
public:
void Push(const T& value) {
elements.push_back(value);}
void Pop() {
if (!IsEmpty()) {
elements.pop_back();}
}
T Top() const {
if (!IsEmpty()) {
return elements.back();}
throw runtime_error("Stack is empty.");
}
bool IsEmpty() const {
return elements.empty();}
};
int main() {
Stack<int> intStack;
intStack.Push(10);
intStack.Push(20);
intStack.Push(30);
cout << "The top element of intStack: " << intStack.Top() << endl;
Stack<double> doubleStack;
doubleStack.Push(3.14);
doubleStack.Push(2.71);
cout << "The top element of doubleStack: " << doubleStack.Top() << endl;
return 0;
}
Output:
The top element of intStack: 30
The top element of doubleStack: 2.71
Explanation:
In this example-
- We first define a class template called Stack to represent a stack data structure.
- The class template uses a vector (std::vector) from the C++ Standard Library to store its elements.
- The Stack class template consists of 4 functions that make it a fully functional stack implementation. These are:
- Push: Adds an element to the top of the stack using std::vector's push_back function.
- Pop: Removes the top element from the stack using std::vector's pop_back function (if the stack is not empty).
- Top: Returns the top element of the stack using std::vector's back function (if the stack is not empty). Otherwise, it throws a runtime_error.
- IsEmpty: Uses an if-statement to check if the stack is empty with the std::vector's empty function.
- In the main() function, we create two instances of the Stack class, namely, intStack (for the int data type) and doubleStack (for the double data type).
- Next, we push three integers (10, 20, 30) onto the intStack, and then use the Top() function with the cout command to print the top element of the stack.
- Then, we push two double values (3.14, 2.71) onto the doubleStack and again display the top element to the console using Top() and cout.
The time complexity is O(1) because most of the operations like push(), pop(), Top(), and Empty() are constant time operations.
The space complexity is considered as O(N) because the class template requires extra memory to store elements, and the vector may require additional memory for dynamic resizing.
Defining A Class Member Outside C++ Template Class
We have already discussed how to define C++ template classes and how to define members within the class. Wondering if we can define members outside the template?
Well, yes, we can. In fact, defining a class member outside the class template is similar to defining a regular member function of a non-template class.
- For this, we must declare the member function inside the class template definition and then provide its implementation outside the class template using the scope resolution operator (::).
- This technique is particularly useful for more complex member functions or when you want to hide the implementation details from the header file.
Let's extend the previous Stack class template example and see how to define a member function outside the class template.
Code Example:
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Stack {
private:
vector<T> elements;
public:
void Push(const T& value) {
elements.push_back(value);}
void Pop() {
if (!IsEmpty()) {
elements.pop_back();}
}
T Top() const {
if (!IsEmpty()) {
return elements.back();}
throw runtime_error("Stack is empty.");}
bool IsEmpty() const {
return elements.empty();}
void PrintSize();
};
// Definition of the PrintSize member function outside the class template
template <typename T>
void Stack<T>::PrintSize() {
cout << "Size of the stack: " << elements.size() << endl;
}
int main() {
Stack<int> intStack;
intStack.Push(10);
intStack.Push(20);
intStack.Push(30);
intStack.PrintSize();
return 0;
}
Output:
Size of the stack: 3
Explanation:
Just like in the previous example, we define a template class Stack with 4 functions- Push (pushes the argument into the vector), Pop (removes the last element of the stack), Top (returns the top element of the stack), IsEmpty (returns true if the stack is empty else return false).
- After the definition of four functions, we declare a new member function PrintSize() to the Stack class template.
- The implementation of the PrintSize function is defined outside the class template using the template keyword followed by <typename T> and the scope resolution operator (::).
- In the main() function, we created an instance of the Stack class template called intStack and added three elements to it using the Push() function.
- Next, call the PrintSize() member function, which prints the size of the stack to the console using the cout command.
Time and Space Complexity: The time complexity of the PrintSize() function is O(1), and space complexity is the same as explained previously, i.e., O(N).
C++ Template Class With Multiple Parameters
Just like we discussed in the case of the C++ template function, the C++ template classes can also have multiple template parameters. Class templates with multiple parameters allow us to create versatile and flexible generic classes that can work with multiple data types simultaneously.
How To Create C++ Template Class With Multiple Parameters?
When defining a class template with multiple parameters, we provide a list of template parameters within angular brackets <>. The syntax for this is mostly the same as that of the C++ template class declaration. The only difference is that the angular brackets will contain more than one parameter type.
Syntax:
template <typename T1, typename T2, ...>
class ClassName {
public:
// Member variables and functions that use types T1, T2, ...
};
Here, each template (i.e., typename T1, typename T2, etc.) parameter represents a different data type. When we create an instance of the class, we specify specific data types or values for each parameter, and the compiler generates a specialized version of the class accordingly. Now, let's take a look at an example for better understanding.
Code Example:
#include <iostream>
using namespace std;
template <typename T1, typename T2>
class Pair {
private:
T1 first;
T2 second;
public:
Pair(T1 a, T2 b){
first=a;
second=b;}
T1 GetFirst() {
return first;}
T2 GetSecond() {
return second;}
};
int main() {
Pair<int, double> pair1(10, 3.14);
cout << "First: " << pair1.GetFirst() << ", Second: " << pair1.GetSecond() << endl;
Pair<string, bool> pair2("hello", true);
cout << "First: " << pair2.GetFirst() << ", Second: " << pair2.GetSecond() << endl;
return 0;
}
Output:
First: 10, Second: 3.14
First: hello, Second: 1
Explanation:
In the sample C++ code-
- We define a class template called Pair, which represents a pair of elements with two different types.
- The class template has two private member variables, first and second, of types T1 and T2, respectively.
- Next, we have a public constructor, which takes two parameters, a of type T1 and b of type T2. The constructor, initializes member variables/data members first and second with values a and b, respectively.
- Then, we define two public member functions in the class, called GetFirst() and GetSecond(). These functions allow external access to the values of the private member variables first and second, respectively.
- In the main() function, we create two objects of the Pair class using different template arguments.
- The first object pair1 takes int and double template arguments, which are initialized with the values 10 and 3.14, respectively.
- The second object, pair2, takes string and bool template arguments which are initialized with the values hello and true, respectively.
- Then, we call the GetFirst() and GetSecond() member functions of both objects to retrieve and print the values of their member variables using cout.
- This demonstrates how to access the private member variables of the Pair class using the public member functions.
The time complexity of operations on class templates with multiple parameters depends on the operations performed inside the class. In this example, the time complexity of the constructor, GetFirst, and GetSecond member functions is O(1) as they involve simple assignments and returns.
The space complexity of the Pair class template is O(1) as it uses a constant amount of memory to store the first and second member variables, regardless of the data types used. The memory consumption is constant and does not depend on the number of elements stored.
What Is C++ Template Specialization?
Templates specialization is a powerful feature in C++ that allows you to write custom implementations for specific data types when using function templates or class templates. It enables you to override the default behavior of a template for certain types and tailor the code to handle them differently.
Template class specialization is particularly useful when the default template implementation is not suitable for certain data types, and you need a specific solution.
Template Parameter & C++ Template Specialization
The term template parameter simply refers to the type of parameter to be passed into the template for the specific implementation. It is a placeholder for the type or non-type values in a template declaration.
This eliminates the need to write multiple implementations of the same function or classes with only changes in their datatypes by allowing us to write generic as well as specific implementations. There are two types of template parameters:
1. Type Parameters:
These are template parameters that represent data types. They allow you to define classes or functions that can work with various data types. For example, template <typename T> or template <class T>. These are declared using keywords typename and class followed by an identifier which serves as the template parameter name.
Syntax:
template <typename T>
OR
template <class T>
Code Example:
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;}
int main() {
int result1 = add(5, 10); // Instantiated as int add(int, int)
double result2 = add(3.14, 2.71); // Instantiated as double add(double, double)
cout<<result1<<endl;
cout<<result2<<endl;
}
Output:
15
5.85
Explanation:
- In this example, we define a function template where the template parameter T represents a generic data type.
- The add function can be called with different data types (e.g., int, double) while using the same code for addition. The compiler generates specialized versions of the add function based on the provided data types.
- In the first call, inside the main() function, the compiler generates a version of add for int, and in the second call, it generates a version for double.
- This allows for efficient code execution with different data types, all using the same function template.
2. Non-type parameter:
A non-type template parameter in C++ allows you to pass constant values as template arguments instead of data types. This means you can create functions or classes that are parameterized by a specific constant value, allowing the compiler to generate specialized versions of the function or class for different constant values at compile time.
Syntax:
template <data type N> //here datatype is what we want e.g. int
Code Example:
#include <iostream>
using namespace std;
template <int N>
int MultiplyByConstant(int x) {
return x * N;}
int main() {
int result1 = MultiplyByConstant<5>(10); // Instantiated as int MultiplyByConstant<5>(int)
int result2 = MultiplyByConstant<3>(7); // Instantiated as int MultiplyByConstant<3>(int)
int result3 = MultiplyByConstant<-2>(15); // Instantiated as int MultiplyByConstant<-2>(int)
cout<<result1<<endl;
cout<<result2<<endl;
cout<<result3<<endl;
}
Output:
50
21
-30
Explanation:
- The template parameter <int N> denotes that the function template accepts a parameter of type integer constant, denoted by N.
- The template function called MultiplyByConstant takes the parameter x and multiplies it by constant N to return the outcome.
- In the main() function, we create three instances of the MultiplyByConstant function template with the constant values 5, 3, and -2, respectively. The outcomes of these functions are stored in variables result1, result2, and result3, respectively.
- The template parameter <5> indicates that the value of N will be changed to 5, so the result for MultiplyByConstant<5>(10) will be 50 (i.e., 10 * 5).
- Similarly, for the template parameters <3> and <-2>, the results will be 21 (i.e., 7 * 3) and -30 (i.e., 15 * -2), respectively.
- All the results are printed to the console using the cout command.
Working Mechanism Of C++ Template Specialisation
When we use generic templates in our code and call them using different types of parameters, the compiler automatically copies the called function with the parameter type that we used to call it. This copy is called a specific version of the template.
But internally, the compiler searches for the closest match, i.e., a function with the same type of parameters used in the function call, i.e., the specialized template is searched first, and if it is available, it is invoked and used.
Code Example:
#include <iostream>
using namespace std;
template <typename T>
T Square(T x) {
return x * x;}
template <>
double Square(double x) {
return x * x;}
int main() {
cout<< Square(5)<<endl;
cout<< Square(3.14)<<endl;
}
Output:
25
9.8596
Explanation:
- In this example, we first create a template function, Square, which takes an input and returns its square value.
- We also provide a template specialization for this function with the double data type to handle the square of a floating-point number differently than the generic template.
- Then, in the main() function, we call the Square(5) function. The compiler uses the generic template and generates 5 * 5 = 25.
- We again call the Square function, with double type value 3.14. In this case, the compiler detects the specialized version for double and generates 3.14 * 3.14 = 9.8596.
- The results are hence produced accordingly.
Can We Pass Non-type Parameters To C++ Templates?
Yes, you can pass non-type parameters to templates in C++ programming. Non-type template parameters are constant values that are used to customize the behavior of template classes or functions.
- These parameters can be integral, enumeration, or pointer types (starting from C++11) and are known at compile-time.
- By using non-type parameters, you can create specialized versions of templates based on specific constant values.
For example, you can define a fixed-size array using a non-type parameter to determine the size:
template <int Size>
class FixedSizeArray {
// Class implementation with a fixed size of Size
};
FixedSizeArray<10> array1; // Instantiate with size 10
FixedSizeArray<20> array2; // Instantiate with size 20
You can also use non-type parameters to create template functions that operate differently depending on the provided constant value, offering a high degree of customization while maintaining efficiency and type safety.
Note- The non-type template parameters must be constants known at compile-time and have certain limitations depending on their type.
How To Specify Default Arguments For Templates In C++?
In C++, we can easily define the default arguments for functions as well as class templates. This allows us to give default values or data types when no value or datatype is passed into the function call.
To specify default arguments for templates in C++, we can use the assignment operator (=) after the template parameter declaration, followed by the default value.
Code Example:
#include <iostream>
using namespace std;
template <typename T = int>//default datatype
T Add(T a, T b = 0) {
return a + b;}
int main() {
int result1 = Add(5); // Uses default value for 'b', result1 = 5
int result2 = Add(10, 20); // Explicitly provided value for 'b', result2 = 30
double result3 = Add<double>(3.14);
// Specify 'T' as double, uses the default value for 'b', result3 = 3.14
cout << "Result1: " << result1 << endl;
cout << "Result2: " << result2 << endl;
cout << "Result3: " << result3 << endl;
return 0;
}
Output:
Result1: 5
Result2: 30
Result3: 3.14
Explanation:
In this example, we have a function template named Add with a default template argument for the data type (typename T = int). This means if no explicit data type is provided during the function call, the function will use int as the default data type.
Let's break the code down further:
- The Add function template takes two parameters of type T, where the second parameter, b, has a default value of 0. This means that if not provided explicitly during the function call, the default value 0 will be used.
- The function calculates the sum of two values of the same data type, T, and returns the result.
- In the main() function, we first call the Add function with only one argument, i.e., Add(5).
- Since the value for b isn't provided, the default value 0 is used. Therefore, the result of Add(5) will be 5 + 0, which is stored in variable result1.
- The second call to Add is made with the value of both parameters provided explicitly. So, Add(10, 20) will calculate the sum as 10 + 20 and store the outcome in variable result2.
- Next, we call Add for a third time with a double data type parameter. Here, we explicitly specify the data type as double by using <double> when calling the function.
- So, Add<double>(3.14) will calculate the sum as 3.14 + 0, with the default value for b, and the outcome is stored in variable result3.
- Finally, the cout statements are used to print the results of result1, result2, and result.
Advantages Of C++ Templates
There are many advantages of using C++ templates in our code. Some of them are listed here:
- Code Reusability: Templates allow you to write a single piece of code that can work with multiple data types. This reduces code duplication and makes maintenance easier, as changes only need to be made in one place.
- Type Safety: The C++ templates guarantee type safety at compile time. That is, the compiler type checks the template argument to catch any potential type-related errors before the program runs.
- Performance Optimisation: The compiler can produce specialized versions of functions or classes using templates in C++ programs, which results in effective code execution. This is done at compile time, hence decreasing the runtime. Inlined functions, for instance, can lead to better performance as the compiler can tailor the generated code specifically for each type used.
- Flexibility: Templates offer a high degree of flexibility. You can use templates for functions, classes, and even template parameters, allowing you to tailor your code for various scenarios.
- Compile-time Polymorphism: Polymorphism simply implies multiple forms of the same function. We have seen that using templates, a specialized copy of the function is made at compile time; hence it is a way of compile time polymorphism.
- Generics: Templates provide a way to create generic functions and generic classes that can be used with different types. This is similar to how other programming languages use generics to achieve type flexibility.
- Standard Library: The C++ Standard Library heavily utilizes templates. Many container classes (like , , etc.) are implemented as templates. This allows these containers to work with various data types seamlessly.
- Custom Data Types: When you create your own data structures or classes, templates can help you define functions or operators that work consistently with your custom types.
- Function Overloading: Templates can provide an alternative to function overloading when you need to perform similar operations on different data types. Instead of writing multiple overloaded functions, you can use a template function to cover all cases.
- Reduced Maintenance: Without templates, if you were to change an algorithm or function, you'd need to update the code for each specific data type. Templates reduce this maintenance burden by centralizing the common logic.
- Standardization: When designing libraries or APIs, templates can help you offer flexible and customizable components that users can use with their chosen data types.
Disadvantages Of C++ Templates
While there are many advantages to using templates in C++, there are also some disadvantages as well. Some of these are as follows:
- Compilation Time: Templates can significantly increase compilation times, especially for complex templates or when many different instantiations are required. Longer compilation times can slow the development process, particularly in large projects.
- Complex Error Messages: Compiler error messages related to templates can be notoriously complex and difficult to decipher. Debugging template-related errors may be challenging, especially for beginners.
- Abstraction Overhead: Overusing C++ templates can lead to excessive levels of abstraction, making the code harder to understand and maintain. It may become challenging for other developers to grasp the full behavior of the code.
- Verbose Syntax: Template syntax can be verbose and harder to read than regular C++ code. The code readability may also suffer, especially when dealing with complex template declarations.
- Increased Debugging Difficulty: Debugging templates can be more complicated than regular code. Understanding how templates are expanded and instantiated during compilation may require specialized knowledge.
- Steep Learning Curve: For newcomers to C++ programming, understanding and effectively using templates can be challenging. Learning the intricacies of template syntax and best practices may take time.
Despite these disadvantages, templates are an essential and powerful tool in C++ programming that increases code readability, maintenance, and flexibility.
Difference Between Function Overloading And Templates In C++
Both function overloading and templates are different things, and they have their separate use cases. Some of the differences between them are listed below:
Aspect |
Function Overloading |
Templates |
Definition |
Multiple functions with the same name but different parameter lists are defined. |
A single function template is defined, which can work with multiple data types or values. |
Purpose |
It provides different implementations for the same operation based on different input parameters. |
It leads to the creation of generic code that can work with various data types or values. |
Parameter Types |
Overloaded functions must have different parameter types or a different number of parameters. |
Template parameters can be of any data type, and the template can work with multiple types or values. |
Invocation |
The appropriate function is selected at compile time based on the provided argument types. |
The template is instantiated at compile time based on the specific data types or values used with it. |
Syntax |
Function names are the same, but the parameter lists differ. |
A single template function is defined using template parameters. |
Code Duplication |
This may lead to code duplication if similar functionality is implemented in multiple functions. |
It reduces code duplication as a single template can handle multiple data types or values. |
Flexibility |
Limited flexibility as new overloads need to be explicitly defined for each unique parameter combination. |
Highly flexible as it allows code reuse and can work with various data types without additional definitions. |
Code Reusability |
Moderate reusability, as functions can be reused based on different argument types. |
High reusability, as a single template, can be used with various data types and values. |
Compile Time Error |
Errors occur at compile time if an appropriate overload cannot be found for the given arguments. |
Errors may occur during template instantiation if the template is not compatible with the provided data types or values. |
Conclusion
In conclusion, we can say that C++ templates are a powerful tool for generic programming, enabling code flexibility and reusability. Function templates and class templates allow developers to write a single implementation that can work with various data types and values. They promote efficient code execution by generating specialized versions at compile time.
Templates enrich the C++ Standard Template Library (STL) with generic containers, algorithms, and data structures. However, they may lead to code bloat (longer codes) and longer compilation times. Despite these challenges, C++ templates are an essential aspect of modern programming, empowering developers to build efficient, type-safe, and versatile code for a wide range of applications.
Also read: 51 C++ Interview Questions For Freshers & Experienced (With Answers)
Frequently Asked Questions
Q. What is template vs. class in C++?
In C++, both templates and classes are fundamental features that serve different purposes. Let's take a closer look at the differences between templates and classes:
Class: A class in C++ is a blueprint for creating objects. The keyword class defines the structure and behavior of an object, encapsulating data members (attributes) and member functions (methods). Instances of a class are created as objects. Each object of a class has its own set of data members, but the code for member functions is shared among all instances of the class.
class MyClass {
public:
int data; // Data member
void display() {
// Member function
cout << "Data: " << data << endl;}
};
Template: A template in C++ is a way to define generic code that can work with multiple types. Templates allow you to create ordinary functions or classes that can be instantiated with different data types as needed. This enables you to write a single template definition that can be reused with different types, reducing code duplication and enhancing code flexibility.
template <typename T>
class MyTemplate {
public:
T data; // Data member of type T
void display() {
cout << "Data: " << data << endl;}
};
The differences between C++ templates and classes are as follows:
-
Purpose:
- Classes are used to define the blueprint for creating objects with specific attributes and behaviors.
- Templates are used to create generic functions or classes that can work with multiple types.
-
Type Flexibility:
- Classes are designed for a specific type of object, and its members are fixed for that type.
- Templates provide flexibility by allowing the same code to work with different types.
-
Instantiation:
- Classes are instantiated to create objects of a specific type.
- Templates are instantiated with a specific data type when you use them, creating a specialized version of the template for that type.
-
Code Duplication:
- Classes can lead to code duplication if you want similar behavior for different types, requiring you to define multiple classes.
- Templates help reduce code duplication by providing a single template definition that can be used with various types.
-
Example:
- A single class might define a specific type of container, like a linked list for integers.
- A template might define a generic container, like a linked list that can hold various types of data.
In summary, classes are used to define object blueprints with fixed attributes and behaviors, while templates allow you to create flexible, generic code that can be instantiated with different types. The concept of a template is particularly useful for code reusability and type flexibility when you want to write functions or classes that work with multiple data types.
Q. What is the difference between template and inheritance in C++?
The difference between templates and inheritance in C++ lies in their fundamental purpose. That is, templates enable generic programming and code reusability across different data types, while inheritance facilitates the creation of class hierarchies and the implementation of the "is-a" relationship between classes.
Template |
Inheritance |
Used for generic programming, creating reusable code that works with multiple data types. |
Used for creating class hierarchies and implementing the "is-a" relationship between classes. |
Requires explicit instantiation with data types. |
Implicitly applies to derived classes. |
Works with functions and classes. |
This only applies to classes and their members. |
It provides a way to write generic algorithms and data structures. |
Enables code sharing and polymorphism through base class pointers. |
Template specialization allows customization for specific data types. |
Inheritance allows customization through virtual functions and overridden methods. |
Q. What is template <typename>?
The template <typename> is the syntax used to define templates in C++. The typename keyword indicates that the following identifier is a type and will be used as a placeholder for a specific data type when the template is instantiated with actual arguments.
Syntax:
template <typename T>
return_type FunctionName(T parameter) {
// Function code here
}
Here,
- T: This is the template type parameter. It represents a placeholder for an actual data type that will be determined at the time of template instantiation.
- return_type: This is the return type of the function template.
- FunctionName: This is the name of the function template.
- T parameter: This is the function parameter of type T, representing the data of the specified type passed to the function template.
Q. What is the difference between a template and a macro in C++?
The main difference between templates and macros in C++ lies in their capabilities and how the compiler processes them.
Template |
Macro |
Type-safe: Templated code is type-checked by the compiler, ensuring type safety. |
Not type-safe: Macros are simple text replacements and may lead to type-related issues. |
Generates type-specific code: Templated code is generated for each data type used. |
Textual substitution: Macros are text-based replacements before compilation. |
Supports generic programming: Templates can work with multiple data types. |
Limited to text substitution: Macros can only perform text replacements. |
Encourages code reuse: Templates allow writing generic algorithms and data structures. |
Prone to code bloat: Macros may generate duplicated code, increasing executable size. |
Better debugging and error messages: Templated code errors are reported with clear error messages. |
Difficult debugging: Macros may lead to obscure error messages and challenging debugging. |
Q. Describe template vs. abstract class in C++.
The key difference between templates and abstract classes lies in their primary purpose and how they achieve code reusability. The differences are listed in the table below:
Template |
Abstract Class |
Used for generic programming with multiple data types. |
Used for defining a common interface for a group of related classes. |
Requires explicit instantiation with data types. |
It cannot be instantiated directly as it serves as a base class for derived classes. |
Template code is generated at compiler time for each data type used. |
Abstract classes are resolved at runtime when creating objects of derived classes. |
It provides a way to write generic algorithms and data structures. |
Enables polymorphism and code sharing through virtual functions and inheritance. |
C++ template classes can have member functions or variables specific to each data type. |
Abstract classes can have pure virtual functions to enforce implementation in derived classes. |
Q. What do templates do?
Templates in C++ enable generic programming, which is the ability to write code that can work with multiple data types or values without explicitly specifying those types at the time of writing the code. They serve as blueprints that allow you to create functions, families of classes, or even variables with placeholders for types or values. When we instantiate or use C++ templates with actual types or values, the compiler generates specialized versions of the code specific to those types or values.
Quiz Time!!!
By now, you must know what C++ templates are and how to use them. Here are a few other topics you must know:
- C++ Type Conversion & Type Casting Demystified (With Examples)
- What is Function Prototype In C++ (Definition, Purpose, Examples)
- Dynamic Memory Allocation In C++ Explained In Detail (With Examples)
- Static Member Function In C++: How to Use Them, Properties, & More
- OOPs Concept In C++ | A Detailed Guide With Codes & Explanations
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.

Subscribe
to our newsletter
Kartik Deshmukh 3 weeks ago