C++程序 要求使用指向类成员函数的指针实现多态性 来计算来计算,正方行,矩形,直角三角形和圆的面积

要求使用指向类成员函数的指针实现多态性来计算来计算,正方行,矩形,直角三角形和圆的面积... 要求使用指向类成员函数的指针实现多态性
来计算来计算,正方行,矩形,直角三角形和圆的面积
展开
 我来答
matrix_nero
2010-05-20
知道答主
回答量:13
采纳率:0%
帮助的人:6.7万
展开全部
楼上的回答基本正确,但是貌似不能直接编译运行,我修正了下。
#include <cstdio>
#include <algorithm>
#include <functional>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;

class shap;
class triangle;
class round;
class rectangle;
class square;

//定义类成员函数指针
typedef double (shap::*pShap)(void);
typedef double (triangle::*pTri)(void);
typedef double (round::*pRound)(void);
typedef double (rectangle::*pRec)(void);
typedef double (square::*pSqu)(void);

class shap
{
public:
shap()
{
_realarea = &shap::area;
_area = (pShap *)&_realarea;

}
double area()
{
cout << "shap::area()" << endl;
return 1;
}

public:
pShap *_area;
pShap _realarea;

};

class triangle : public shap
{
public:
triangle()
{
_realarea = &triangle::area;
_area = (pShap *)&_realarea;

}
double area()
{
cout << "triangle::area()" << endl;
return 1;
}
pTri _realarea;
};

class round : public shap
{
public:
round ()
{
_realarea = &round::area;
_area = (pShap *)&_realarea;
}

double area()
{
cout << "round::area()" << endl;
return 1;
}
pRound _realarea;
};

class rectangle : public shap
{
public:
rectangle ()
{
_realarea = &rectangle::area;
_area = (pShap *)&_realarea;
}

double area()
{
cout << "rectangle::area()" << endl;
return 1;
}
pRec _realarea;

};

class square : public shap
{
public:
square ()
{
_realarea = &square::area;
_area = (pShap *)&_realarea;
}

double area()
{
cout << "square::area()" << endl;
return 1;
}
pSqu _realarea;

};

void printarea(shap *s)
{
//*(s->_area) 首先对函数指针进行解引用,然后再通过->*p来进行真正的调用
(s->*(*(s->_area)))();
}
int main()
{
int nRetCode = 0;
triangle t1;
round t2;
rectangle t3;
square t4;

printarea(&t1);
printarea(&t2);
printarea(&t3);
printarea(&t4);

getchar();

return nRetCode;
}
匿名用户
2010-05-16
展开全部
class shap;
class triangle;
class round;
class rectangle;
class square;

typedef double (shap::*pShap)(void);
typedef double (triangle::*pTri)(void);
typedef double (round::*pRound)(void);
typedef double (rectangle::*pRec)(void);
typedef double (square::*pSqu)(void);

class shap
{
public:
shap()
{
_realarea = area;
_area = (pShap *)&_realarea;

}
double area()
{
cout << "shap::area()" << endl;
return 1;
}

public:
pShap *_area;
pShap _realarea;

};

class triangle : public shap
{
public:
triangle()
{
_realarea = area;
_area = (pShap *)&_realarea;

}
double area()
{
cout << "triangle::area()" << endl;
return 1;
}
pTri _realarea;
};

class round : public shap
{
public:
round ()
{
_realarea = area;
_area = (pShap *)&_realarea;
}

double area()
{
cout << "round::area()" << endl;
return 1;
}
pRound _realarea;
};

class rectangle : public shap
{
public:
rectangle ()
{
_realarea = area;
_area = (pShap *)&_realarea;
}

double area()
{
cout << "rectangle::area()" << endl;
return 1;
}
pRec _realarea;

};

class square : public shap
{
public:
square ()
{
_realarea = area;
_area = (pShap *)&_realarea;
}

double area()
{
cout << "square::area()" << endl;
return 1;
}
pSqu _realarea;

};

void printarea(shap *s)
{
(s->*(*(s->_area)))();
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
triangle t1;
round t2;
rectangle t3;
square t4;

printarea(&t1);
printarea(&t2);
printarea(&t3);
printarea(&t4);

getchar();

return nRetCode;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式