I'm really intrigued by OOP, I feel that I can help most people with OOP questions reasonably well, but I still have a lot to learn and there's something that bothers me. What is the best way to write the program's main() if I want to create a truly OOP C++ program?

My OOP programs tend to be something I would call "Procedural using Objects". They're not really procedural, but they're not really OOP either. For example, here's the main() from a program that I'm currently working on:

int main() {
  cout << "Lakewood ITA Administration Console Version 2.0\nCopyright, 2010 Lakewood Construction" << endl;
  while(true) {                 //begin application loop
    string userInput;
    cout << "What would you like to do?\n";
    cout << "\t1. Manage supervisors\n";
    cout << "\t2. Manage projects\n";
    cout << "\t0. Quit" << endl;
    cout << "Choice: ";
    getline(cin, userInput, '\n');
    char menuChoice = userInput.at(0);
    if (menuChoice == '1') {
      if (!manageSupervisors())
        cout << "Error in supervisor management system.\nPlease inform an administrator." << endl;
    } else if (menuChoice == '2') {
      if (!manageProjects())
        cout << "Error in Project management system.\nPlease inform an administrator." << endl;
    } else if (menuChoice == '0')
      break;
    else {
      cout << "Invalid menu choice, please select again." << endl;
    }

  } //end application loop

  cout << "Thank you for using ITA. Good bye!" << endl;

  cin.get();
  return 0;
}

As you can see, this is all procedural. You can't see it here, but I don't start getting into any sort of OOP until manageSupervisors() or manageProjects() is called. Once they start, I create vectors of either Supervisor or Project objects and begin manipulating both the overall vectors and the individual objects.

The main thrust of my question is this:
In C++, is there a better (more appropriate) way to write an OOP program's main(). How can I best create a "true" OOP program in C++? Assuming I convert the sub-systems (project management and supervisor management) to polymorphic classes, would something like this be better?:

int main() {
  cout << "Lakewood ITA Administration Console Version 2.0\nCopyright, 2010 Lakewood Construction" << endl;
  ITA::System *subSystem = 0;   //create a pointer to an ITA System
  while(true) {                 //begin application loop
    string userInput;
    cout << "What would you like to do?\n";
    cout << "\t1. Manage supervisors\n";
    cout << "\t2. Manage projects\n";
    cout << "\t0. Quit" << endl;
    cout << "Choice: ";
    getline(cin, userInput, '\n');
    char menuChoice = userInput.at(0);
    if (menuChoice == '1')
      subSystem = new supervisorManager;  //initialize the supervisor subsystem
    else if (menuChoice == '2')
      subSystem = new projectManager;     //initialize the project subsystem
    else if (menuChoice == '0')
      break;
    else
      cout << "Invalid menu choice, please select again." << endl;
    subSystem.execute();
    delete subSystem;          //de-allocate the pointer
  } //end application loop
  cout << "Thank you for using ITA. Good bye!" << endl;

  cin.get();
  return 0;
}

Any advice at all would be appreciated.

There is no such thing as a pure OOP c++ program. There are only two kinds of programs: event driven and procedural. MS-Windows GUI is event driven where someone clicks a button and the program responds to it. Console programs are procedural where they just run from start to finish, with a few loops and threads tossed in between the two ends. Both types can contain a few OOP concepts, such as classes, but that is about the extent to which c++ supports OOP.

So then what you're saying is that my "Procedural using Objects" is pretty much the norm but the degree of OOP usage varies from project to project.

Well, that makes me feel a little better. Thanks.

would something like this be better?

A step in the right direction, for sure. In the updated version you can easily identify the Factory, and factor it out, for example:

int main()
{
    while(true) {
        subsystem = Subsystem::open();
        subsystem.execute();
        delete subsystem;
    }
}

The static open() method deals with the user input and instantiates the proper object.

would something like this be better?

A step in the right direction, for sure. In the updated version you can easily identify the Factory, and factor it out, for example:

int main()
{
    while(true) {
        subsystem = Subsystem::open();
        subsystem.execute();
        delete subsystem;
    }
}

The static open() method deals with the user input and instantiates the proper object.

A very interesting approach. I've heard of something like this before, but I never really understood it. I'll have to take a closer look at it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.