基类虚函数为shape,成员虚函数为void Drew(),成员变量为其实绘制点:int x ,int y

声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求利用多态性的概念,分别以虚函数的形式完成对圆和正方形的周长及面积的计算.要求:Shape类的数据成员包括中心点的坐标,Circle类和Square类初始值分别给出:圆的圆心和半径;正方形的中心和一个顶点.
#include #include #define PI 3.14class Point {public:Point(){x=0;y=0;}Point(float x,float y){this->x=x;this->y=y;}Point(){}float getX(){}float getY(){}void setX(float x){this->x=x;}void setY(float y){this->y=y;}private:};class Shape{public:Shape(){this->center.setX(0);this->center.setY(0);}Shape(Point center){this->center.setX(center.getX());this->center.setY(center.getY());}Shape(){}virtual float getArea(){return 0;}virtual float getCirc(){return 0;}protected:P};class Circle:public Shape{public:Circle(){}Circle(Point center,float radius){ this->center.setX(center.getX());this->center.setY(center.getY());this->radius=}Circle(){}float getArea(){return radius*radius*PI;}float getCirc(){return 2*PI*}private:};class Square:public Shape{public:Square(){}Square(Point center,Point top){this->center.setX(center.getX());this->center.setY(center.getY());this->top.setX(top.getX());this->top.setY(top.getY());}Square(){}float getArea(){float a=top.getX()-center.getX();float b=top.getY()-center.getY();return 2*(a*a+b*b);}float getCirc(){float a=top.getX()-center.getX();float b=top.getY()-center.getY();return 4*sqrt(2)*sqrt(a*a+b*b);}private:P};void main(){Point p1(0,0),p2(4,4);Circle c1=Circle(p1,4);Square s1=Square(p1,p2);Shape s=c1;cout
为您推荐:
其他类似问题
扫描下载二维码/localvar@126/blog/static//
#include&iostream.h&
class&fairyTale
&&&&virtual&void&act1(){cout&&"Princess&meets&Frog"&&act2();}
&&&&void&act2(){cout&&"Princess&kisses&Frog"&&act3();}
&&&&virtual&void&act3(){cout&&"Frog&turns&into&Prince"&&act4();}
&&&&virtual&void&act4(){cout&&"They&live&happily&ever&after"&&act5();}
&&&&void&act5(){cout&&"The&end"&&}
class&unhappyTale&:&public&fairyTale
&&&&void&act3(){cout&&"Frog&stays&a&Frog"&&act4();}//overriden
&&&&void&act4(){cout&&"Princess&runs&away&in&disgust"&&act5();}//overiden
&&&&void&ac5(){cout&&"The&not&-so&-happy&end"&&}//hide
void&main()
&&&&char&c;
&&&&fairyTale&&*
&&&&cout&&"which&tale&would&you&like&to&hear(f/u)?";
&&&&cin&&c;
&&&&if&(c&==&'f')
&&&&&&&&tale&=&new&fairyT//动态分配,将类fairyTale地址赋给
&&&&&&&&tale&=&new&unhappyT
&&&&tale-&act1();
&&&&delete&
Princess meets Frog
Princess kisses Frog
Frog turns into Prince
They live happily ever after
输入u时结果为
Princess meets Frog
Princess kisses Frog//以上两句由继承关系决定,虽然unhappyTale没有void act1()和void act2(),但是这两个都是继承自fairyTale,且act1()为虚函数
Frog stay a Frog//动态联编tale=new unhappyT指向unhappyTale中的虚函数act3()(虽然前面没有关键字virtual,由继承关系决定)
Princess runs away in disgust
The end//调用act5()时,因为是静态联编,所以由fairyTale *决定
在一个基类或派生类的成员函数中可以直接调用该类等级中的虚函数,例如:
&&&&virtual&void&act1(){act2();}
&&&&void&act2(){act3();}
&&&&virtual&void&act3(){act4();}
&&&&virtual&void&act4(){act5();}
&&&&void&act5(){cout&&"The&end"&&}
class&B:public&A
&&&&void&act3(){act4();}
&&&&void&act4(){act5();}
&&&&void&act5(){cout&&"All&done"&&
由于对非虚函数调用是采用静态联编,所以程序不必运行我们就能确定
基类A中的函数act1()中的调用语句:
act2();将调用它所在类中的函数act2(),即A::act2()。同样,基类的成员函数act4()调用A::act5()
派生类B中的成员函数act4()调用 B::act5()
对于虚函数必须具体问题具体分析。例如:
这时,各成员函数的调用顺序为:
A::act1()-&A::act2()-&B::act3()-&B::act4()-&B::act5()
这时可以通过this指针来分析,例如,A::act2()可以使用this指针改写成:
void A::act2()
this-&act3();
在上例情况下,this指向b,所以A::act2()调用B::act3().
可以使用DAG来分析上例中对虚函数的调用过程,画出最派生类B以及它的基类组成的DAG,在DAG中只列出
虚函数名,我们以类B为例
&&&&&&&&&&&&&&&&&&&&&&&&&
A{ act1(),act3(),act4()}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& A{act1()}
继承于A的B&&&&&&&&&&&&&&& 删除基类中被支配的名字如右图
B{ act3(),act4()}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& B{ act3(),act4()}
右图的DAG决定了实际对象为B的对象情况下虚函数的执行过程。即如果
调用act3()或act4()的话,总调用B中定义的虚函数,而若调用act1(),则总是调用A中的虚函数
可以使用成员名限制来阻止基类的成员函数调用派生类中的虚函数,例如:
void A::act3(){A::act4();}
void B::act3(){A::act4();}
这时,对act4()的调用使用静态联编,即调用A中的act4()
阅读(...) 评论()
& Suprasoft Inc., All right reserved.设计模式_百度知道
int Width,y2)考虑用Abstract Factory,Factory Method或Prototype中的一个来,成员变量为其实绘制点:Circle(成员变量为半径:int x ,要求画出UML图谢谢,y1和int x2,int y其子类有:int r)
Rectangle(成员变量为宽和高,Height)
Line(成员变量为起点和终点:int x1,成员虚函数为void Drew(),模拟一副图(有诸多的Circle,Rentangle和Line)的创建过程不限语言基类为shape,Builder
我有更好的答案
创建型模式用来处理对象的创建过程,主要包含以下5种设计模式:  工厂方法模式(Factory Method Pattern)  抽象工厂模式(Abstract Factory Pattern)  建造者模式(Builder Pattern)  原型模式(Prototype Pattern)  单例模式(Singleton Pattern)
结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:  适配器模式(Adapter Pattern)  桥接模式(Bridge Pattern)  组合模式(Composite Pattern)  装饰者模式(Decorator Pattern)  外观模式(Facade Pattern)  享元模式(Flyweight Pattern)  代理模式(Proxy Pattern)
行为型模式用来对类或对象怎样交互和怎样分配职责进行描述,...
其他类似问题
为您推荐:
设计模式的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁今天去参加百度的面试,遇到一个关于虚函数的机制的问题,我一直认为就是为了让基类指针或引用(指向继承类)能够看到基类的虚函数(当基类的虚函数没有被继承类重写),但是继承类又怎么看到基类的普通成员函数呢?我开始想,好想不可以调用基类的成员函数,因为基类的成员函数不是虚函数,在虚表找不到它,好吧,明天被鄙视了。回到家好好的研究了一下虚函数和成员函数,看看基类的成员函数和虚函数在派生类指针或引用是否可见,还有基类的成员函数和虚函数在基类指针或引用(指向继承类)是否可见,以及派生类的成员函数和虚函数在基类指针或引用(指向继承类)是否可见?
下面是例子:
#include &iostream&class A{public:&& &A(int i) { n=i; }&& &A(){ n=1;}&& &virtual void f(){ std::cout&&"A.f() is used"&&}&& &void g(){ std::cout&&"A.g() is used"&&}&& &void k(){ std::cout&&"A.k() is used"&&}private:&& &};class B:public A{public:&& &B(int i) { n=i; }&& &B(){ n=1;}&& &void f(){ std::cout&&"B.f() is used"&&}&& &void g(){ std::cout&&"B.g() is used"&&}&& &void h(){ std::cout&&"B.h() is used"&&}&& &virtual void j(){ std::cout&&"B.h() is used"&&}private:&& &};class C{public:&& &C(int i) { n=i; }&& &C(){ n=1;}&& &void w(){ std::cout&&"C.w() is used"&&}private:&& &};int main(){&& &B&& &b.k();&& //b显然能看到A中的k()&& &A *a;&& &a=&b;&& &a-&f(); //a看到的是B的f()&& &a-&A::f();& //a看到的是A的f()&& &a-&g();& //a看到的是A的g()&& &a-&h();& //a看不到B的h()&& &a-&j();& //a看不到B的虚拟函数j()&& &cout&&a-&C::n&&& //看不到&& &a-&C::w();& //看不到&& &while(1)&& &{&& &}}
一个类的对象中是没有关于普通成员函数的指针的slot,只有成员变量还有虚表指针,类的成员函数的代码定义在PE文件的代码区,所以从程序加载时,就已经分配好了内存用于存放这些代码;代码运行时所需要的内存,比如栈、堆等等,则是代码运行时才分配的;对于某个类的所有对象来说,类成员函数只在内存中有一份拷贝,所有的对象都共享同一份成员函数的代码。同一个类的不同的对象之间的差异仅仅是通过成员变量来体现的。c++实现成员函数的时候实际上用到了一个技巧&&this指针。this指针是当前调用成员函数的对象首地址,我们知道通过这个地址可以访问到对应对象的成员变量。那么成员函数如何区分this究竟指向哪个对象呢?技巧就在于,c++背后把成员函数当做普通函数一样调用,除了传入实际的参数外,还把所属对象的指针作为参数this传入,函数内部通过显示的或者隐含的方式访问对象成员。当然,这只是背后的事情,外面用起来就和你见到的一样,简单直观。
http://blog.csdn.net/haoel/article/details/1948051
阅读(...) 评论()定义抽象基类Shape, 其中纯虚函数printName()输出几何图形的名称和相应的成员数据、纯虚函数printArea(_百度知道
定义抽象基类Shape, 其中纯虚函数printName()输出几何图形的名称和相应的成员数据、纯虚函数printArea(
面积,面积:半径=10.5三角形:输入(依次输入圆半径:314,下底=2。并由Shape类派生出5个派生类、长方形长宽, 其中纯虚函数printName()输出几何图形的名称和相应的成员数据:底边=4:边长=5、纯虚函数printArea()计算几何图形的面积,面积:1052 41 2 34 3输出、正方形边长,面积。程序运行结果如下,面积,高=3。测试过程、Trapezoid(梯形) 、Triangle(三角形) 、梯形上底下底和高:Circle(圆形),数据成员为半径,高=3:8梯形、下底和高、Rectangle(长方形) 、Square(正方形) ,分别调用相应的成员函数显示各个几何图形的属性及面积,宽=4,数据成员为边长定义抽象基类Shape,数据成员为长和宽,使其每个元素指向一个动态产生的派生类对象:程序运行结果如下:357.159正方形:圆:4,数据成员为上底,数据成员为底和高、三角形底边和高):上底=1,定义一个指向基类的指针数组,最终输出总面积值:25长方形:长=2:6总面积
提问者采纳
void printName() {
cout&lt.0f ) ;& } void printArea() {
cout&& } void printArea() {
cout&&&lt: float fchang:
3: Shape() {
fbianchang =&lt: public Shape{public.0 ); } void printArea() {
cout&& class Rectangle .1415926&lt: fchang*fkuan&lt: public Shape{public: public Shape{&lt.0 );endl.0; virtual void printArea() = 0; }fbanjin*fbanjin*3: Shape() {
fbanjin =};Rectangle type&&class Shape{ }&&printName(); for ( int i =0;class S&&&quot, float f2 = 0.0 ); ss[0] = new Circle( 2; }
void printName() {
cout&Square type&quot: virtual void printName() = 0.0f;
fkuan = f2: Square( float f = 0;&lt: Circle( float f = 0; }private: Shape() {
fchang = f.0f ) ;}.0f ) ;fbianchang*fbianchang&lt, 5;&& ss[2] = new Rectangle( 2;};class C i &
ss[i]-&&lt#include &Circle type&void main(){ Shape *ss[3];}; ss[1] = new Square( 2;iostream&printArea();&endl: Rectangle( float f = 0; } void printName() {
其他类似问题
为您推荐:
纯虚函数的相关知识
其他1条回答
& pShape-&&&: virtual void printName() = 0: public Shape {public: Circle(int r) ; & pShape-&&lt:base class shape * @这是圆形&printArea();&lt楼上已经写的很好 了#include &class C &int main() { Shape *pShape = NULL: int m_R:&quot: m_R(r) { } void printName() {
cout &/半径.面积:Circle * @ &class Shape {&lt: */** * @ m_R & &半径}; 3.14 * m_R * m_R &/** * @brief: *// pShape = new Circle(5);printName(); &iostream& } void printArea() {
cout & & }& return 0; virtual void printArea() = 0;& virtual ~Shape() { }}; &#47
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 基类虚函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信