Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling child's overridden method from parent class

I want to practice C++ by coding a simple mobile phone application with an interchangeable system. I created a System base class and also created MyOS class which extends the System class. In the Phone class, I have a variable of System class because I think like in Java, you can assign it with child class. (eg. System sys = new MyOS();). But in C++ it calls the function in the base class.

What I want to work in C++ but it’s in Java.

public class MyParent {
    public void start() {
        System.out.println("start() executed in MyParent");
    }
}

public class MyChild extends MyParent {
    @Override
    public void start() {
        System.out.println("start() excecuted in MyChild");
    }
}

public class Inherit {
    MyParent parent;
    
    public Inherit(MyParent parent) {
        this.parent = parent;
    }
    
    public void start() {
        parent.start();
    }
}

public class TestInherit {
    public static void main(String[] args) {
        Inherit i = new Inherit(new MyChild());
        i.start();
    }
}

Output: start() excecuted in MyChild

My current c++ code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

System.h

#pragma once

#include <iostream>

class System {
public:

    void start() {
        std::cout << "Booting System..." << std::endl;
    }
};

MyOS.h

#pragma once

#include <iostream>
#include "System.h"

class MyOS: public System {
public:

    // Override
    void start() {
        std::cout << "Booting MyOS..." << std::endl;
    }
};

Phone.h

#pragma once

#include "System.h"

class Phone {
public:

    Phone(System system) {
        Phone::system = system;
    }

    void start() {
        system.start();
    }

private:

    System system;
};

MyPhone.cpp

#include "MyOS.h"
#include "Phone.h"
#include "System.h"

int main() {
    MyOS os;
    Phone myPhone(os);
    myPhone.start();

    return 0;
}

Output: Booting System...

>Solution :

If you’re coming from java, you need to remember that every class reference in java is implicitly a pointer, so to make equivalent C++ code, you need to make all interclass references into pointers.

In addition, every method in java is implicitly virtual, so if you want to override them, you need an explicit virtual in C++.

So you end up with something like:

#include <iostream>

class System {
public:

    virtual void start() {
        std::cout << "Booting System..." << std::endl;
    }
};

class MyOS: public System {
public:

    // Override
    void start() override {
        std::cout << "Booting MyOS..." << std::endl;
    }
};

class Phone {
public:

    Phone(System *system) {
        this->system = system;
    }

    void start() {
        system->start();
    }

private:

    System *system;
};

int main() {
    MyOS os;
    Phone myPhone(&os);
    myPhone.start();

    return 0;
}

Of course, using raw pointers is a recipe for memory leaks and corruption, as C++ does not have built in garbage collection. So you generally want to use "smart" pointers instead — either std::unique_ptr or std::shared_ptr

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading