#include <stdio.h>
#include <stdlib.h>
class shape
{
public:
int bottom;
int height;
public:
shape(int x=0, int y=0)
{
bottom=x;
height=y;
}
int area(void) //沒有虛擬函數
{
return(0);
}
void setbottom(int l)
{
bottom=l;
}
void setheight(int w)
{
height=w;
}
};
class tshape:public shape
{
private:
int top;
public:
tshape(int a, int b)
{
bottom=a;
height=b;
}
void settop(int x)
{
top=x;
}
int area()
{
return(((top+bottom)*height)/2); //override
}
};
class trishape:public shape
{
public:
trishape(int a, int b)
{
bottom=a;
height=b;
}
int area()
{
return((bottom*height)/2); //override
}
};
int main()
{
int i;
tshape t1(10,10);
t1.settop(50);
printf("=====--->%d\n",t1.area());
trishape tria(20,20);
printf("=====--->%d\n",tria.area());
trishape trib(20,20);
printf("=====--->%d\n",trib.area());
trishape tric(20,20);
printf("=====--->%d\n",tric.area());
shape *pshape[4]={&t1,&tria,&trib,&tric}; //父指標指到子物件
for(i=0;i<=3;i++) //因area沒有使用virtual函數,固泛型設計無法執行
{
printf("------%d\n", pshape[i]->area());
}
system("pause");
return 0;
}