c++ - Can I define the same function with the same number and type of params in the derived class? -
this question has answer here:
is legal (this header files, , .cpp files contain function definitions):
class human { protected: std::string m_name; int m_age; public: human(); void printname() const; void printage() const; };
and re-define same void printname() const;
(which not virtual
) in derived class.
class student : public human { public: student(); void printname() const; void study() const; };
what this? not overriding. not overloading too, right? far overloading should have different type or number of arguments (method constant in both places). have done here?
simple answer: yes.
more complex answer: yes, codicil:
student s{}; s.printname(); // call student::printname() const human &h = s; h.printname(); // call human::printname() const on same object
since methods aren't virtual, compiler call method based on type compiler sees @ point of call.
the printname()
method in student
override. not virtual override.