2015-10-19 220 views
-4

下面的代碼中,三角形和矩形區域的面積爲0。需要一些提示,我該如何解決這個問題。 也,如果我能得到一些固定在這,這將是偉大的!C++形狀區域:獲取區域0

#include <iostream.h> 
#include <conio.h> 

class shape 
{ 
protected: 
    double x,y; 

public: 
    void get_data(void); 
    virtual void display_area(void){ } 
}; 

class triangle:public shape 
{ 
protected: 
    double AoT; 

public: 
    void display_area(void); 

}; 

class rectangle:public shape 
{ 
protected: 
    double AoR; 

public: 
    void display_area(void); 

}; 

void shape::get_data(void) 
{ 
    cout<<"Enter the vlaue of Base(x) and Height(y):"<<endl; 
    cin>>x>>y; 
} 

void triangle::display_area(void) 
{ 
    AoT=0.5*x*y; 
    cout<<"The area of Triangle in unit sq. is:"<<AoT<<endl; 
} 

void rectangle::display_area(void) 
{ 
    AoR=x*y; 
    cout<<"The area of Rectangle in Unit sq. is:"<<AoR<<endl; 
} 

main() 
{ 
    clrscr(); 
    shape s, *p; 
    triangle t; 
    rectangle r; 

    s.get_data(); 
    p=&t; 
    p->display_area(); 
    p=&r; 
    p->display_area(); 

    getch(); 

} 

在先進的感謝。需要對此快速注視,因爲我',米kinda despo大聲笑

+0

你是什麼輸入? – Downvoter

+0

x和y是我的輸入@cad – Anuj

+0

't'和'p'不受's'輸入的影響。 – MikeCAT

回答

1

s,t,和r是完全無關的對象。調用s.get_data()只修改s.xs.y,既不t.xt.y也不r.xr.y。你需要調用shape::get_datatr seperately:

p=&t; 
p->get_data(); 
p->display_area(); 

p->get_data(); 
p=&r; 
p->display_area();