A very simple C++ class template working program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Creating and using a very simple C++ class template in C++ programming

To show: How to create and use a very simple C++ class template in C++ programming

 

// C++ class template example

#include <iostream>

using namespace std;

 

// class template declaration part

template <class any_data_type>

class Test

{

public:

// constructor

Test();

// destructor

~Test();

// method

any_data_type Data(any_data_type);

};

 

template <class any_data_type>

any_data_type Test<any_data_type>::Data(any_data_type Var0)

{return Var0;}

 

// a class template definition part

// should be in the same header file with the class template declaration

// constructor

template <class any_data_type>

Test<any_data_type>::Test()

{cout<<"Constructor, allocate..."<<endl;}

 

// destructor

template <class any_data_type>

Test<any_data_type>::~Test()

{cout<<"Destructor, deallocate..."<<endl;}

 

// the main program

int main(void)

{

Test<int> Var1;

Test<double> Var2;

Test<char> Var3;

Test<char*> Var4;

 

cout<<"\nOne template fits all data type..."<<endl;

cout<<"Var1, int = "<<Var1.Data(100)<<endl;

cout<<"Var2, double = "<<Var2.Data(1.234)<<endl;

cout<<"Var3, char = "<<Var3.Data('K')<<endl;

cout<<"Var4, char* = "<<Var4.Data("The class template")<<endl<<endl;

return 0;

}

 

Output examples:

 

Constructor, allocate...

Constructor, allocate...

Constructor, allocate...

Constructor, allocate...

One template fits all data type...

Var1, int = 100

Var2, double = 1.234

Var3, char = K

Var4, char* = The class template

Destructor, deallocate...

Destructor, deallocate...

Destructor, deallocate...

Destructor, deallocate...

Press any key to continue . . .

 

 

A very simple nested C++ class template, template in template program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Creating and using the C++ nested class template in C++ programming

To show: How to create and use the C++ nested class template in C++ programming

 

// C++ nested template class

template <class any_data_type>

class MyClass

{

// ...

// nested class template

template <class another_data_type>

class NestedClass

{ };

// ...

};

 

int main(void)

{ return 0; }

 

Output examples:

 

// No output

 

 

C and C++ Programming Resources | C & C++ Code Example Index