Method (computer programming): Difference between revisions

Content deleted Content added
C++Bro123 (talk | contribs)
m Operator methods: Improve C++ example
C++Bro123 (talk | contribs)
m Virtual functions: Improve C++ example
Line 134:
#include <memory>
 
class Super {
public:
{
virtual ~Super() = default;
public:
 
virtual void iAmIAm() { std::cout << "I'm the super class!\n"; }
};
 
class Sub : public Super {
public:
{
void iAmIAm() override { std::cout << "I'm the subclass!\n"; }
public:
void iAm() { std::cout << "I'm the subclass!\n"; }
};
 
int main() {
std::unique_ptr<Super> inst2(newinst1 Sub= std::make_unique<Super>());
{
std::unique_ptr<Super> inst1(newinst2 Super= std::make_unique<Sub>());
std::unique_ptr<Super> inst2(new Sub());
 
inst1->iAmIAm(); // callsCalls |Super::iAm()IAm|.
inst2->iAmIAm(); // callsCalls |Sub::iAm()IAm|.
}
</syntaxhighlight>