SlideShare a Scribd company logo
A class template is a common class that
represent various similar classes operating
on data of different types.
Once a class template is defined, we can
create an object of that class using a
specific basic or user defined data types to
replace the generic data types used during
class definition
Syntax of Class Template
Template<class T1,class T2,…>
Class classname
{
attributes;
methods;
};
T is the variable of template type.
<>(angle bracket) is used to declare
variables of template type, one or more
variables are declared separated by comma
To create an object of the class, type
ClassName< type > myObject;
Example: Stack< double >
doubleStack;
Template class functions
Declared normally, but preceded by
template<class T>
Generic data in class listed as type T
Binary scope resolution operator used
Template class function definition:
template<class T>
MyClass< T >::MyClass(int size)
{
myArray = new T[size];
}
Constructor definition - creates an array of
type T
Class Templates and Non-type Parameters
Can use non-type parameters in templates
Default argument
Treated as const
Example:
Template <class T, int elements>
Stack<double, 100>
mostRecentSalesFigures;
Declares object of type
Stack<double,100>
This may appear in the class definition:
T stackHolder[ elements ];
Creates array at compile time, rather than
dynamic allocation at execution time
 Classes can be overridden
For template class Array, define a class
named
Array<myCreatedType>
This new class overrides then class template
for myCreatedType
The template remains for unoverriden types
Class templates and static variables :
The rule for class templates is same as
function templates.
Each instantiation of class template has its
own copy of member static variables.
A class generated from a class template is
called a generated class.
Derive a class template from a base class,
which is a template class.
Derive a class template from a base class,
which is a template class, add more
template members in the derived class.
Derive a class from a base class which is
not a template, and template member to
that class.
Derive a class from a base class which is a
template class and restrict the template
feature, so that the derived class and its
derivatives do not have the template feature.
The syntax for declaring derived classes from
templatebased
base classes is as :
template <class T1, …..>
class baseclass
{
// template type data and functions
};
template <class T1, …..>
class derivedclass : public baseclass <T1,
….>
{
// template type data and functions
};
Class Template Specialization :
In some cases it is possible to override the
template-generated code by providing special
definitions for specific types. This called
template specialization.
 Thank You 

More Related Content

What's hot

Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
Array in c#
Array in c#Array in c#
Array in c#
Prem Kumar Badri
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
Dr.Neeraj Kumar Pandey
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
Bhavik Vashi
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
tanu_jaswal
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
Prasanna Kumar SM
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
Abhishek Wadhwa
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 

What's hot (20)

Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Array in c#
Array in c#Array in c#
Array in c#
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Inner class
Inner classInner class
Inner class
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
class and objects
class and objectsclass and objects
class and objects
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 

Similar to Class template

Templates
TemplatesTemplates
templates.ppt
templates.ppttemplates.ppt
templates.ppt
Saiganesh124618
 
TEMPLATES in C++
TEMPLATES in C++TEMPLATES in C++
TEMPLATES in C++
Prof Ansari
 
My Object Oriented.pptx
My Object Oriented.pptxMy Object Oriented.pptx
My Object Oriented.pptx
GopalNarayan7
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
Shweta Shah
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
saranuru
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
Learnbay Datascience
 
Templates1
Templates1Templates1
Templates1
zindadili
 
C# program structure
C# program structureC# program structure
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Lovely Professional University
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
Mayank Bhatt
 
type of class in c#
type of class in c#type of class in c#
type of class in c#
tahria123
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
Adeel Rasheed
 
python.pptx
python.pptxpython.pptx
python.pptx
GayathriP95
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
teach4uin
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
Khaled Anaqwa
 

Similar to Class template (20)

Templates
TemplatesTemplates
Templates
 
templates.ppt
templates.ppttemplates.ppt
templates.ppt
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
 
TEMPLATES in C++
TEMPLATES in C++TEMPLATES in C++
TEMPLATES in C++
 
My Object Oriented.pptx
My Object Oriented.pptxMy Object Oriented.pptx
My Object Oriented.pptx
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Templates1
Templates1Templates1
Templates1
 
C# program structure
C# program structureC# program structure
C# program structure
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
type of class in c#
type of class in c#type of class in c#
type of class in c#
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
python.pptx
python.pptxpython.pptx
python.pptx
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 

More from Kousalya M

Function template
Function templateFunction template
Function template
Kousalya M
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Kousalya M
 
Binary search tree deletion
Binary search tree deletionBinary search tree deletion
Binary search tree deletion
Kousalya M
 
Red black trees and their properties
Red black trees and their propertiesRed black trees and their properties
Red black trees and their properties
Kousalya M
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
Kousalya M
 
Structures in c programming
Structures in c programmingStructures in c programming
Structures in c programming
Kousalya M
 

More from Kousalya M (6)

Function template
Function templateFunction template
Function template
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Binary search tree deletion
Binary search tree deletionBinary search tree deletion
Binary search tree deletion
 
Red black trees and their properties
Red black trees and their propertiesRed black trees and their properties
Red black trees and their properties
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
 
Structures in c programming
Structures in c programmingStructures in c programming
Structures in c programming
 

Recently uploaded

2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

Class template

  • 1.
  • 2. A class template is a common class that represent various similar classes operating on data of different types. Once a class template is defined, we can create an object of that class using a specific basic or user defined data types to replace the generic data types used during class definition
  • 3. Syntax of Class Template Template<class T1,class T2,…> Class classname { attributes; methods; }; T is the variable of template type. <>(angle bracket) is used to declare variables of template type, one or more variables are declared separated by comma
  • 4. To create an object of the class, type ClassName< type > myObject; Example: Stack< double > doubleStack; Template class functions Declared normally, but preceded by template<class T> Generic data in class listed as type T Binary scope resolution operator used
  • 5. Template class function definition: template<class T> MyClass< T >::MyClass(int size) { myArray = new T[size]; } Constructor definition - creates an array of type T
  • 6. Class Templates and Non-type Parameters Can use non-type parameters in templates Default argument Treated as const Example: Template <class T, int elements> Stack<double, 100> mostRecentSalesFigures; Declares object of type Stack<double,100>
  • 7. This may appear in the class definition: T stackHolder[ elements ]; Creates array at compile time, rather than dynamic allocation at execution time  Classes can be overridden For template class Array, define a class named Array<myCreatedType> This new class overrides then class template for myCreatedType The template remains for unoverriden types
  • 8. Class templates and static variables : The rule for class templates is same as function templates. Each instantiation of class template has its own copy of member static variables. A class generated from a class template is called a generated class.
  • 9. Derive a class template from a base class, which is a template class. Derive a class template from a base class, which is a template class, add more template members in the derived class. Derive a class from a base class which is not a template, and template member to that class.
  • 10. Derive a class from a base class which is a template class and restrict the template feature, so that the derived class and its derivatives do not have the template feature. The syntax for declaring derived classes from templatebased base classes is as : template <class T1, …..> class baseclass { // template type data and functions };
  • 11. template <class T1, …..> class derivedclass : public baseclass <T1, ….> { // template type data and functions }; Class Template Specialization : In some cases it is possible to override the template-generated code by providing special definitions for specific types. This called template specialization.