作業一 作業一

#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)
{

}

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()
{

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());

system("pause");
return 0;
}
 

作業二 虛擬與泛型 作業二 虛擬與泛型

#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 virtual area(void)
{

}
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++) /利用父指標指到子物件之計算面積函數 此為泛型設計 ( generic programming )
{
printf("------%d\n", pshape[i]->area());
}


system("pause");
return 0;
}