classPoint2d {public: Point2d(int x = , int y = ): _x(x), _y(y) {}virtualvoidprint()const{ printf("Point2d(%d, %d)\n", _x, _y); }virtualintz()const{ printf("Point2d get z: 0\n"); return; }virtualvoidz(int z){ printf("Point2d set z: %d\n", z); }protected:...
Point3d(int x = 0, int y = 0, int z = 0):Point2d(x, y), _z(z) {} void print() const { printf("Point3d(%d, %d, %d)\n", _x, _y, _z); } int z() const { printf("Point3d get z: %d\n", _z); return _z; } void z(int z) { printf("Point3d set z: %d\...
声明了 = 0 才表示 这个是一个纯虚函数 这个类是一个抽象类~就是C++对纯虚函数的一种规范一种定义~谢谢~ 有问题请追问~
virtual void foo() { cout << "A::foo() is called" << endl;} }; class B: public A { public: //备注:只要在基类中已声明为virtual,这里即使不使用virtual关键字,默认也是虚函数 //同样,如果还有从B派生的子类,对应的成员函数也是虚函数 virtual void foo() { cout << "B::foo() is called...
void Show() { printf("Show() from CPoint2D/n"); }; ---*/ }; 当我们实例化CPoint2D时,在编译时(at the compiling)也会出现如下的报错: error C2259: 'CShape' : cannot instantiate abstract class due to following members: warning C4259: 'void __thiscall CShape::Show(void)' : pure...
例如基类虚函数的原型为virtual void Func();,派生类虚函数的原型为virtual void Func(int);,那么当...
printf("this is a test\n"); printf("size of test: %d\n",sizeof(Test)); return 0; } 结果和前面讲的类差不多: blacktea@ubuntu:~/C++$ ./a.out this is a test size of test: 8 然后我们尝试继承 #include<iostream> #include<string> ...
}voidChild::Function2() { printf("This is child,function2\n"); }intmain(intargc,char*argv[]) { Parent*p;//定义一个基类指针if(_getch()=='c')//如果输入一个小写字母cp=&child;//指向继承类对象elsep=&parent;//否则指向基类对象p->Function1();//这里在编译时会直接给出Parent::Function...
void Show() { printf("Show() from CPoint2D/n"); }; ---*/ }; 当我们实例化CPoint2D时,在编译时(at the compiling)也会出现如下的报错: error C2259: 'CShape' : cannot instantiate abstract class due to following members: warning C4259: 'void __thiscall CShape::Show(void)' : pure...
#include <cstdio> class A { public: virtual void Hello() = 0; }; void A::Hello() { printf("A::Hello\n"); } class B : public A { public: void Hello() { printf("B::Hello\n"); A::Hello(); } }; int main() { /* Prints: B::Hello A::Hello */ B b; b.Hello()...