林中誠 程式設計 20121225 作業一 林中誠 程式設計 20121225 作業一

#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;
}

 

林中誠 程式設計 20130110 虛擬與多型二 林中誠 程式設計 20130110 虛擬與多型二

#include <stdio.h>
#include <stdlib.h>
  class calculator
  {
    public:
       int a;
       int b; 
       int e; 
    public:
    virtual  void menu(void)
        {
           printf("1. 加\n");
           printf("2. 減\n");
           printf("3. 乘\n");
           printf("4. 除\n");
           printf("---------------\n");
           printf("請輸入二個數:");
           scanf("%d %d",&a,&b);
           printf("選擇功能 :");
           scanf("%d",&e);
         }
     int add()
         {
            return(a+b);
         }  
     virtual int sub()
         {
            return(a-b);
         }   
   };

 class calculator_en:public calculator
 {
  public:
       void menu(void)
        {
           printf("1. add\n");
           printf("2. sub\n");
           printf("3. mul\n");
           printf("4. div\n");
           printf("---------------\n");
           printf("please input two number:");
           scanf("%d %d",&a,&b);
           printf("choose Function:");
           scanf("%d",&e);
        }
        
      int sub()
         {
            return(b-a);
         }   
        
        
 };

int main() 
{
  
    void show_menu(calculator &m);
    
    int i=0;
    calculator a;
    calculator_en b;

    while(i<=2)
     {
     printf("1.中文選單  2.英文選單  3. 離開  --> ");
     scanf("%d",&i);
     if (i==1)
         show_menu(a);
     if (i==2)
         show_menu(b); 
     }

    system("pause"); 
    return 0;
}
 
 void show_menu(calculator &m)
     {     
      m.menu();
      if (m.e==1)
     {
        printf("The result is = %d\n", m.add());
     }
    else if (m.e==2)
     {
        printf("The result is = %d\n", m.sub());
     }
   else
    printf("輸入錯誤 %d\n",m.e);
  } 

林中誠 程式設計 20121225 虛擬與多型一 林中誠 程式設計 20121225 虛擬與多型一

參考資料:www.cmlab.csie.ntu.edu.tw/~kant/p.doc

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


system("pause");
return 0;
}