a Q about pure virtual method

i have a question about pure virtual method in the abstract class, like virtual void print() = 0;. if i have a subclass of this abstract class, in the class declaration, do i need to write 'virtual' specifier again? I think there is no need, since once a method is virtual, then the method with the same function signature in all the subclasses should be virtual, too.
no need.
but your class have to inherit with the abstract class

1
2
3
4
5
6
7
8
9
10
class A{
public:
    virtual void print() = 0;
};

class B : public A{
public:
    print(){ //..Implementation }
};
Last edited on
@Felicia123
You need to repeat the return type in the derived class.
void print(){ /* Implementation */ }
Last edited on
closed account (D80DSL3A)
You need to repeat the return type in the derived class.

There is some flexibility here. Functions may return a pointer or reference to a derived type instead, eg:
1
2
3
4
5
6
7
8
9
class A{
public:
    virtual A& ref_to_type() = 0;
};

class B : public A{
public:
    B& ref_to_type(){ return *this; }
};

This is sometimes useful.
Topic archived. No new replies allowed.