Question #261333

 Rewrite the code in C++ using Factory Method. The goal is to remove the

main function’s dependency on the concrete classes, such as WalkToSchool.


#ifndef WALKTOSCHOOL_H

#define WALKTOSCHOOL_H

#include <iostream>

#include "GoToSchool.hpp"


using namespace std;


class WalkToSchool:public GoToSchool {

  void takeTransit() { cout << "We are walking to school." << endl; }

  void arrive() { cout << "After 10 minutes, we arrive." << endl; }

};


#endif


Expert's answer

#ifndef WALKTOSCHOOL_H
#define WALKTOSCHOOL_H

#include <iostream>
#include "GoToSchool.hpp"

using namespace std;

class WalkToSchool:public GoToSchool {
  void takeTransit() { cout << "We are walking to school." << endl; }
  void arrive() { cout << "After 10 minutes, we arrive." << endl; }
};

WalkToSchool *createNewWalkToSchool() {
  return new WalkToSchool();
}

#endif
LATEST TUTORIALS
APPROVED BY CLIENTS