主题总数:270876
总帖数:1119824
注册会员总数:255497
今日总帖数:307
|
|
├─◆ 狼盟首页 > 查看贴子 > 详细信息
楼主
一个不长的C++程序,我怎么也找不出错误,请帮帮我!

#include<iostream.h>class sshape{public: virtual float area()=0; virtual float show()=0;};class rectangle:public sshape{public: rectangle(float d,float t) { l=d; w=t; } float area() { return l*w; } void show() { cout<<"The area of rectangle:s="<<area()<<endl; }};class trapezoid:public sshape{public: trapezoid(float u,float v,float q) { a=u; b=v; h=q; } float area() { return(float)0.5*(a+b)*h; } void show() { cout<<"This area of trapezoid:s="<<area()<<endl; }private: float a,b,h;};void main(){ sshape * sp; rectangle re(5.0,6.0); sp= &re; sp->show(); trapezoid tr(2.0,4.0,10.0); sp=&tr; sp->show();}这是一个用纯虚函数求矩形和梯形面积的程序,但不知道哪里出现了错误,编译不能通过,提示如下:--------------------Configuration: 127 - Win32 Debug--------------------Compiling...127.cppD: est127127.cpp(21) : error C2555: 'rectangle::show' : overriding virtual function differs from 'sshape::show' only by return type or calling convention D: est127127.cpp(3) : see declaration of 'sshape'D: est127127.cpp(39) : error C2555: 'trapezoid::show' : overriding virtual function differs from 'sshape::show' only by return type or calling convention D: est127127.cpp(3) : see declaration of 'sshape'D: est127127.cpp(48) : error C2259: 'rectangle' : cannot instantiate abstract class due to following members: D: est127127.cpp(8) : see declaration of 'rectangle'D: est127127.cpp(48) : warning C4259: 'float __thiscall sshape::show(void)' : pure virtual function was not defined D: est127127.cpp(6) : see declaration of 'show'D: est127127.cpp(48) : error C2259: 'rectangle' : cannot instantiate abstract class due to following members: D: est127127.cpp(8) : see declaration of 'rectangle'D: est127127.cpp(48) : warning C4259: 'float __thiscall sshape::show(void)' : pure virtual function was not defined D: est127127.cpp(6) : see declaration of 'show'D: est127127.cpp(52) : error C2259: 'trapezoid' : cannot instantiate abstract class due to following members: D: est127127.cpp(25) : see declaration of 'trapezoid'D: est127127.cpp(52) : warning C4259: 'float __thiscall sshape::show(void)' : pure virtual function was not defined D: est127127.cpp(6) : see declaration of 'show'D: est127127.cpp(52) : error C2259: 'trapezoid' : cannot instantiate abstract class due to following members: D: est127127.cpp(25) : see declaration of 'trapezoid'D: est127127.cpp(52) : warning C4259: 'float __thiscall sshape::show(void)' : pure virtual function was not defined D: est127127.cpp(6) : see declaration of 'show'执行 cl.exe 时出错.127.obj - 1 error(s), 0 warning(s)请大家帮我看一下,我怎么也找不出错误。希望能说明原因,谢谢!
-
dynamic516 [ 1 楼 ]
2006-04-22 23:10:00
虚基类说明格式如下: virtual <继承方式><基类名> 其中,virtual是虚类的关键字。虚基类的说明是用在定义派生类时,写在派生类名的后面。例如: class A { public: void f(); protected: int a; }; class B : virtual public A { protected: int b; }; class C : virtual public A { protected: int c: }; class D : public B, public C { public: int g(); private: int d; }; 由于使用了虚基类,使得类A,类B,类C和类D之间关系用DAG图示法表示如下: A{ f(), a } / B{b} C{c} / D{g(),d} 从该图中可见不同继承路径的虚基类子对象被合并成为一个对象。这便是虚基类的作用,这样将消除了合并之前可能出现的二义性。这时,在类D的对象中只存在一个类A的对象。因此,下面的引用都是正确的: D n; n.f(); //对f()引用是正确的。 void D::g() { f(); //对f()引用是正确的。 } 下面程序段是正确的。 D n; A *pa; pa = &n; 其中,pa是指向类A对象的指针,n是类D的一个对象,&n是n对象的地址。pa=&n是让pa指针指向类D的对象,这是正确的,并且也无二义性。
-
yexin218 [ 2 楼 ]
2006-04-22 23:10:00
-
dynamic516 [ 3 楼 ]
2006-04-22 23:12:00
楼主的程序我在vc6.0下调试通过,下面是正确代码:#include<iostream.h>class sshape{public: virtual float area()=0; virtual void show()=0;//函数返回值与继承类的类型不同};class rectangle:virtual public sshape//virtual public 关键字{public: rectangle(float d,float t) { l=d; w=t; } float area() { return l*w; } void show() { cout<<"The area of rectangle:s="<<area()<<endl; }private: float l,w;};class trapezoid:virtual public sshape{public: trapezoid(float u,float v,float q) { a=u; b=v; h=q; } float area() { return(float)0.5*(a+b)*h; } void show() { cout<<"This area of trapezoid:s="<<area()<<endl; }private: float a,b,h;};void main(){ sshape * sp; rectangle re(5.0,6.0); sp= &re; sp->show(); trapezoid tr(2.0,4.0,10.0); sp=&tr; sp->show();}
-
tanglewish [ 4 楼 ]
2006-04-23 12:06:00
#include<iostream.h>class sshape{public: virtual float area()=0; virtual void show()=0;//函数返回值与继承类的类型不同};class rectangle: public sshape//virtual public 关键字{public: rectangle(float d,float t) { l=d; w=t; } float area() { return l*w; } void show() { cout<<"The area of rectangle:s="<<area()<<endl; }private: float l,w;};class trapezoid: public sshape{public: trapezoid(float u,float v,float q) { a=u; b=v; h=q; } float area() { return (float)0.5*(a+b)*h;//楼主return后少了空格 } void show() { cout<<"This area of trapezoid:s="<<area()<<endl; }private: float a,b,h;};void main(){ sshape * sp; rectangle re(5.0,6.0); sp= &re; sp->show(); trapezoid tr(2.0,4.0,10.0); sp=&tr; sp->show();}编译通过
-
tanglewish [ 5 楼 ]
2006-04-23 12:07:00
Virtual FunctionC++ Specific —>A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.END C++ SpecificExample 1class WageEmployee{public: virtual float computePay();};class SalesPerson : public WageEmployee{public: float computePay();};You can execute different versions of computePay( ) depending on the type of object you're calling it for.Example 2WageEmployee aWorker;SalesPerson aSeller;WageEmployee *wagePtr;wagePtr = &aWorker;wagePtr->computePay(); // call WageEmployee::computePaywagePtr = &aSeller;wagePtr->computePay(); // call SalesPerson::computePayThe virtual keyword is needed only in the base class's declaration of the function; any subsequent declarations in derived classes are virtual by default.A derived class's version of a virtual function must have the same parameter list and return type as those of the base class. If these are different, the function is not considered a redefinition of the virtual function. A redefined virtual function cannot differ from the original only by return type.
-
byts布殊 [ 6 楼 ]
2006-04-23 13:05:00
应该是这样的:#include<iostream.h>class sshape{public: virtual float area()=0; virtual void show()=0;//函数返回值与继承类的类型不同};class rectangle:virtual public sshape//virtual public 关键字{public: rectangle(float d,float t) { l=d; w=t; } float area() { return l*w; } void show() { cout<<"The area of rectangle:s="<<area()<<endl; }private: float l,w;};class trapezoid:virtual public sshape{public: trapezoid(float u,float v,float q) { a=u; b=v; h=q; } float area() { return(float)0.5*(a+b)*h; } void show() { cout<<"This area of trapezoid:s="<<area()<<endl; }private: float a,b,h;};void main(){ sshape * sp; rectangle re(5.0,6.0); sp= &re; sp->show(); trapezoid tr(2.0,4.0,10.0); sp=&tr; sp->show();}
-
05518993524 [ 7 楼 ]
2006-04-25 11:30:00
#include "iostream"using namespace std; // .net 环境下class sshape{public: virtual float area()=0; virtual void show()=0; //你的问题主要出在这里,重写函数一定要和虚函数类型一致,这点很重要!};class rectangle:public sshape{ float l,w; // 其次你在这里少定义了两个变量。public: rectangle(float d,float t) { l=d; w=t; } float area() { return l*w; } void show() { cout<<"The area of rectangle:s="<<area()<<endl; }};class trapezoid:public sshape{public: trapezoid(float u,float v,float q) { a=u; b=v; h=q; } float area() { return (float)0.5*(a+b)*h; } void show() { cout<<"This area of trapezoid:s="<<area()<<endl; }private: float a,b,h;};void main(){ sshape * sp; rectangle re(5.0,6.0); sp= &re; sp->show(); trapezoid tr(2.0,8.0,10.0); sp=&tr; sp->show();}
|
|
| |
|
|