Q:

C++ program to demonstrate example of Templates.

belongs to collection: C++ programs on various topics

0

C++ program to demonstrate example of Templates.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ program - Demonstration of Template in C++ programming language

Let’s consider the example

#include <iostream>
using namespace std;

template <typename TYPE>

class Numbers
{
	private:
	TYPE x, y;
	public:
	//constructor
	Numbers(const TYPE a, const TYPE b) : x(a), y(b) {}
	TYPE getX() { return x; }
	TYPE getY() { return y; }
};

int main() 
{
	Numbers<float> NUM1(100, 200);
	cout<<"Values with integer numbers: ";
	cout << NUM1.getX() << ", " << NUM1.getY() << endl;
	
	Numbers<float> NUM2(100.20, 150.69);
	cout<<"Values with float numbers: ";
	cout << NUM2.getX() << ", " << NUM2.getY() << endl;
	return 0;
}
 
    Values with integer numbers: 100, 200 
    Values with float numbers: 100.2, 150.69

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ - Read Characters from One file and Write them... >>
<< C++ Class Exercise - Read and Print House details ...