开发者

How to correct the c++ code below?

开发者 https://www.devze.com 2023-01-29 10:10 出处:网络
class A1{ public void op(){cout << \"A1\";} }; class B1 : public A1{ public void op(){cout << \"B1\";}
class A1{
    public void op(){cout << "A1";}
};

class B1 : public A1{
    public void op(){cout << "B1";}
};

Seems valid to me开发者_如何学编程,but the compiler is reporting :

Error,expected a ':' at void


class A1
{
    public:
       virtual void op(){cout << "A1";}
};

class B1 : public A1
{
    public:
       void op(){cout << "B1";}
};

Beware to add the keyword virtual, otherwise, you are not overriding void op(); in B


Colons after the public labels.


class A1 { public: void op () {cout << "A1";}};

class B1 : public A1 { public: void op () {cout << "B1"; }};

PS. Also may be you want to use virtual methods?


class A1{
        public: void op(){cout << "A1";}
};

class B1 : public A1{
        public: void op(){cout << "B1";}
};

:) C++ has a different syntax than C#. In C++ it's less verbose, so you can do:

class A1{
public:
    void a(){cout << "x";}
    void b(){cout << "x";}
    void c(){cout << "x";}
};

instead of having to put public in front of all public methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消