① 缂栧啓涓涓绋嫔簭璁$畻涓夎掑舰銆佹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爜浼桦寲绛夐桩娈靛皢镓澶勭悊镄勬簮绋嫔簭杞鎹涓虹敤浜岃繘鍒朵唬镰佽〃绀虹殑鐩镙囩▼搴忋
铹跺悗阃氲繃杩炴帴绋嫔簭澶勭悊灏嗙▼搴忎腑镓鐢ㄧ殑鍑芥暟璋幂敤銆佺郴缁熷姛鑳借皟鐢ㄧ瓑宓屽叆鍒扮洰镙囩▼搴忎腑锛屾瀯鎴愪竴涓鍙浠ヨ繛缁镓ц岀殑浜岃繘鍒舵墽琛屾枃浠躲傝皟鐢ㄨ繖涓镓ц屾枃浠跺氨鍙浠ュ疄鐜扮▼搴忓憳鍦ㄥ瑰簲婧愮▼搴忔枃浠朵腑镓鎸囧畾镄勭浉搴斿姛鑳姐
鍙傝冭祫鏂欐潵婧愶细锏惧害锏剧-缂栫▼