Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Method overriding
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C++=== [[C++]] does not have the keyword {{Cpp|super}} that a subclass can use in Java to invoke the superclass version of a method that it wants to override. Instead, the name of the parent or base class is used followed by the [[scope resolution operator]]. For example, the following code presents two [[Class (computer science)|classes]], the base class {{Cpp|Rectangle}}, and the derived class {{Cpp|Box}}. {{Cpp|Box}} overrides the {{Cpp|Rectangle}} class's {{Cpp|Print}} method, so as also to print its height.<ref name="malik">Malik 2006, p. 676</ref> <syntaxhighlight lang="cpp"> #include <iostream> //--------------------------------------------------------------------------- class Rectangle { public: Rectangle(double l, double w) : length_(l), width_(w) {} virtual void Print() const; private: double length_; double width_; }; //--------------------------------------------------------------------------- void Rectangle::Print() const { // Print method of base class. std::cout << "Length = " << length_ << "; Width = " << width_; } //--------------------------------------------------------------------------- class Box : public Rectangle { public: Box(double l, double w, double h) : Rectangle(l, w), height_(h) {} void Print() const override; private: double height_; }; //--------------------------------------------------------------------------- // Print method of derived class. void Box::Print() const { // Invoke parent Print method. Rectangle::Print(); std::cout << "; Height = " << height_; } </syntaxhighlight> The method {{Cpp|Print}} in class {{Cpp|Box}}, by invoking the parent version of method {{Cpp|Print}}, is also able to output the private [[Variable (programming)|variables]] {{Cpp|length}} and {{Cpp|width}} of the base class. Otherwise, these variables are inaccessible to {{Cpp|Box}}. The following [[Statement (programming)|statements]] will [[Object (computer science)|instantiate]] objects of type {{Cpp|Rectangle}} and {{Cpp|Box}}, and call their respective {{Cpp|Print}} methods: <syntaxhighlight lang="cpp"> int main(int argc, char** argv) { Rectangle rectangle(5.0, 3.0); // Outputs: Length = 5.0; Width = 3.0 rectangle.Print(); Box box(6.0, 5.0, 4.0); // The pointer to the most overridden method in the vtable in on Box::print, // but this call does not illustrate overriding. box.Print(); // This call illustrates overriding. // outputs: Length = 6.0; Width = 5.0; Height= 4.0 static_cast<Rectangle&>(box).Print(); } </syntaxhighlight> In [[C++11]], similar to Java, a method that is declared <code>final</code> in the super class cannot be overridden; also, a method can be declared <code>override</code> to make the compiler check that it overrides a method in the base class.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)