C++程序 要求使用指向类成员函数的指针实现多态性 来计算来计算,正方行,矩形,直角三角形和圆的面积
展开全部
楼上的回答基本正确,但是貌似不能直接编译运行,我修正了下。
#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;
}
#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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询