template-class extends the other template-class
2613 ワード
#include <iostream>
#include <iterator>
using namespace std;
template<typename T>
class Graph{
private:
T name;
public:
Graph(const T& n);
~Graph();
void getName();
virtual T toString();
};
template<typename T>
Graph<T>::Graph(const T& n)
:name(n)
{
cout<<"create a graph"<<endl;
}
template<typename T>
Graph<T>::~Graph()
{
cout<<"destruct Graph"<<endl;
}
template<typename T>
void Graph<T>::getName()
{
cout<<this->name<<endl;
}
template<typename T>
T Graph<T>::toString()
{
return name;
}
/* .
template<>
class Graph<string>{
private:
string name;
public:
Graph(const string& n);
~Graph();
virtual string toString();
void getName();
};
Graph<string>::Graph(const string& n)
:name(n)
{
cout<<"create a graph"<<endl;
}
Graph<string>::~Graph()
{
cout<<"destruct Graph"<<endl;
}
void Graph<string>::getName()
{
cout<<this->name<<endl;
}
string Graph<string>::toString()
{
return name;
}
*/
template<typename T>
class Rectangle:public Graph<T>
{
private:
int size;
T str;
public:
Rectangle(const T& s, const int& number, const T& st);
~Rectangle();
virtual T toString()override;
};
template<typename T>
Rectangle<T>::Rectangle(const T& s, const int& number, const T& st)
:Graph<T>(s),
size(number),
str(st)
{
cout<<"construct Rectangle"<<endl;
Graph<T>::getName();// .
// this->getName();
}
template<typename T>
Rectangle<T>::~Rectangle()
{
cout<<"destruct Rectangle"<<endl;
}
template<typename T>
T Rectangle<T>::toString()
{
return this->str;
}
int main()
{
//Graph<string> g("nothing");
Rectangle<string> rect(string("rectangle"), int(20), string("shihua"));
rect.getName();
Graph<string>* g=▭
string theS=g->toString();
// g .
//Graph<string>& g=rect;
//string theS=g.toString();
return 0;
}