Professional Documents
Culture Documents
•}
Function template overloading:
• using namespace std;
• template <class T>
•
• void display(T x, T y)
•{
• cout << "Displaying Template: "
• << "\n";
•}
• void display(int x, T y, T z)
•{
• cout << "Explicitly display: "
• << "\n";
•}
•
• int main()
•{
• // Function Call with a
• // different arguments
• display(200,564);
• display(12.40,23.45, 34.23);
• return 0;
•}
Syntax for Generic Class:
template <class type> // here type support any type of data type
class class-name
{
Public:
Type Variable;
Type function Name(type argu-list)
}
Example:
• #include <iostream>
• using namespace std;
• template<class T>
• class A
•{
• public:
• T num1 = 5;
• T num2 = 6;
• void add()
• {
• std::cout << "Addition of num1 and num2 : " <<
num1+num2<<std::endl;
• }
•
• };
• int main()
•{
• A<int> d;
• d.add();
• return 0;
•}
Inheritance in Class template:
• #include <iostream>
• Derived<int> derived;
• derived.callBase();
•}