① 緙栧啓涓涓紼嬪簭璁$畻涓夎掑艦銆佹f柟褰㈠拰鍦嗗艦涓夌嶅浘褰㈢殑闈㈢Н銆
#include<iostream>
using namespace std;
class base
{
public:
virtual void disp();
};
class triangle:public base
{
private:
double bottom;
double height;
public:
triangle(double b,double h)
{
bottom = b;
height = h;
}
void disp()
{
cout<<"涓夎掑艦闈㈢Н錛"<<bottom * height / 2<<endl;
}
};
class square:public base
{
private :
double a;
public:
square(double a1)
{
a = a1;
}
void disp()
{
cout<<"姝f柟褰㈤潰縐錛"<<a * a<<endl;
}
};
#define pi 3.1415
class circle:public base
{
private:
double r;
public:
circle(double r1)
{
r = r1;
}
void disp()
{
cout<<"鍦嗗艦闈㈢Н錛"<<pi * r * r<<endl;
}
};
int main() {
base *p;
triangle t(20, 20); //絎涓涓鍙傛暟涓轟笁瑙掑艦搴曡竟錛岀浜屼釜鍙傛暟涓哄簳杈逛笂鐨勯珮
square s(20);
circle c(20);
p = &t;
p->disp();
p = &s;
p->disp();
p = &c;
p->disp();
return 0;
}
鎵╁睍璧勬枡錛
緙栬瘧鏂瑰紡涓嬶紝棣栧厛閫氳繃涓涓瀵瑰簲浜庢墍鐢ㄧ▼搴忚捐¤璦鐨勭紪璇戠▼搴忓規簮紼嬪簭榪涜屽勭悊錛岀粡榪囧規簮紼嬪簭鐨勮瘝娉曞垎鏋愩佽娉曞垎鏋愩佽鎰忓垎鏋愩佷唬鐮佺敓鎴愬拰浠g爜浼樺寲絳夐樁孌靛皢鎵澶勭悊鐨勬簮紼嬪簭杞鎹涓虹敤浜岃繘鍒朵唬鐮佽〃紺虹殑鐩鏍囩▼搴忋
鐒跺悗閫氳繃榪炴帴紼嬪簭澶勭悊灝嗙▼搴忎腑鎵鐢ㄧ殑鍑芥暟璋冪敤銆佺郴緇熷姛鑳借皟鐢ㄧ瓑宓屽叆鍒扮洰鏍囩▼搴忎腑錛屾瀯鎴愪竴涓鍙浠ヨ繛緇鎵ц岀殑浜岃繘鍒舵墽琛屾枃浠躲傝皟鐢ㄨ繖涓鎵ц屾枃浠跺氨鍙浠ュ疄鐜扮▼搴忓憳鍦ㄥ瑰簲婧愮▼搴忔枃浠朵腑鎵鎸囧畾鐨勭浉搴斿姛鑳姐
鍙傝冭祫鏂欐潵婧愶細鐧懼害鐧劇-緙栫▼