hi all,
i just learned the topic about templates...
i successfully made a template program in which the instance of the templated class is a normal instance:
Code:
#include<iostream>
#include<cstdlib>
using namespace std;
template<class aType>
class calc{
public:
void add();
void sub();
void mul();
void div();
private:
aType a;
aType b;
};
template<class aType> void calc<aType> :: add(){
cout << "Enter a number: ";
cin >> a;
cin.ignore();
cout << "Enter another number: ";
cin >> b;
cin.ignore();
cout << "\n===\n\nThe sum is " << a + b << "...\n\n===";
cout << "PRESS ENTER TO CONTINUE===";
cin.get();
system("cls");
}
template<class aType> void calc<aType> :: sub(){
cout << "Enter a number: ";
cin >> a;
cin.ignore();
cout << "Enter another number: ";
cin >> b;
cin.ignore();
cout << "\n===\n\nThe difference is " << a - b << "...\n\n===";
cout << "PRESS ENTER TO CONTINUE===";
cin.get();
system("cls");
}
template<class aType> void calc<aType> :: mul(){
cout << "Enter a number: ";
cin >> a;
cin.ignore();
cout << "Enter another number: ";
cin >> b;
cin.ignore();
cout << "\n===\n\nThe product is " << a * b << "...\n\n===";
cout << "PRESS ENTER TO CONTINUE===";
cin.get();
system("cls");
}
template<class aType> void calc<aType> :: div(){
cout << "Enter a number: ";
cin >> a;
cin.ignore();
cout << "Enter another number: ";
cin >> b;
cin.ignore();
cout << "\n===\n\nThe quotient is " << a / b << "...\n\n===";
cout << "PRESS ENTER TO CONTINUE===";
cin.get();
system("cls");
}
int main(){
calc <double> co;
co.add();
co.sub();
co.mul();
co.div();
}
but what should i do if i want to create a pointer instance of the templated class ?
i've tried this but doesn't work:
Code:
calc *co = new calc;
co->add();
co->sub();
co->mul();
co->div();