?? 模板繼承.cpp
字號:
#include<iostream.h>
template <class TYPE>/*類模板1的定義*/
class Point
{
protected: TYPE a,b;/*受保護成員*/
public:Point(){}/*構造函數*/
Point(TYPE m,TYPE n)
{
a=m;
b=n;
}
Point(const Point &other)/*構造函數的重載*/
{
a=other.a;
b=other.b;
}
void enter()/*輸入坐標值*/
{
cout<<"please input x,y of the point"<<endl;
cin>>a>>b;
}
void out()/*輸出坐標值*/
{
cout<<"The point is"<<"("<<a<<","<<b<<")"<<endl;
}
};
template <class TYPE>/*類模板2的定義*/
class Line:public Point<TYPE>/*私有繼承模板1*/
{
private:TYPE c,d;
public:
Line(){}
Line(TYPE A,TYPE B,TYPE C,TYPE D):Point<TYPE>(A,B)/*構造函數成員的初始化*/
{
c=C;
d=D;
}
void enter()
{
cout<<"please input two x,y of the line"<<endl;
cin>>a>>b>>c>>d;
}
void out()
{
cout<<"The line is"<<"("<<a<<","<<b<<")"<<"("<<c<<","<<d<<")"<<endl;
}
};
int main()/*主函數的實現*/
{
Point<int> a(0,0);
a.enter();
a.out();
Line<int>b(0,0,0,0);
b.enter();
b.out();
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -