A normal C++ class with function template code 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 the C++ class with function template in C++ programming

To show: How to create the C++ class with function template in C++ programming

 

// C++ normal class template

class MyClass

{

//...

// but, have template member function

template <class any_data_type>

void MemberFunct(any_data_type)

{ };

};

 

int main(void)

{

return 0;

}

 

Output examples:

 

// No output

 

 

Another simple C++ class template code sample

 

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 a very simple C++ class template in C++ programming

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

 

// a very simple C++ class template program example

template <class any_data_type>

class MyStack

{

private:

// number of the stack's element

int size;

// top position

int top;

// data type pointer

any_data_type* StackPtr;

public:

// constructor

MyStack(int =10);

// destructor

~MyStack(){delete [] StacKPtr;}

// put in data

int push(const any_data_type&);

// take out data

int pop(any_data_type&);

// test the emptiness

int IsEmpty() const {return top == -1;}

// test the fullness

int IsFull() const {return top == size - 1;}

};

 

// the main() program

int main(void)

{

return 0;

}

 

Output examples:

 

// No output

 

Output examples:

 

 

Another example of the C++ class template code sample

 

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 a simple C++ class template in C++ programming

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

 

// another example of the C++ template declaration

template <class any_data_type>

class Vector

{

private:

any_data_type *buffer;

// copy constructor

Vector<any_data_type> (const Vector <any_data_type> &Var1);

// overloaded assignment operator

Vector<any_data_type>& operator=(const Vector<any_data_type>& Var2);

// destructor

~Vector<any_data_type>();

// other member functions

any_data_type& operator [ ] (unsigned int index);

const any_data_type& operator [ ] (unsigned int index) const;

};

 

int main(void)

{

return 0;

}

 

Output examples:

 

// No output

 

 

The C++ class template program skeleton

 

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 the C++ class template program skeleton in C++ programming

To show: How to create the C++ class template program skeleton in C++ programming

 

// C++ class template skeleton

template <class any_data_type>

class MyStack

{

private:

// number of the stack’s element

int size;

// top position

int top;

// data type pointer

any_data_type* StackPtr;

public:

// constructor

MyStack(int =10);

// destructor

~MyStack(){delete [ ] StacKPtr;}

// put in data

int push(const any_data_type&);

// take out data

int pop(any_data_type&);

// test the emptiness

int IsEmpty() const {return top == -1;}

// test the fullness

int IsFull() const {return top == size – 1;}

};

 

int main(void)

{

return 0;

}

 

Output examples:

 

// No output lor!

 

 

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