C++ 在基类中创建派生类的对象

C++ 在基类中创建派生类的对象,c++,inheritance,C++,Inheritance,这是我的密码。我试图从基类中的派生类创建对象,但它有一些错误 #include <iostream> #include<Windows.h> #include <string> #include <ctime> using namespace std; class PrgDevice { private: tm startTime; tm stopTime; int choice; int choice1;

这是我的密码。我试图从基类中的派生类创建对象,但它有一些错误

#include <iostream>
#include<Windows.h>
#include <string>
#include <ctime>

using namespace std;

class PrgDevice {
private:
    tm startTime;
    tm stopTime;
    int choice;
    int choice1;
    char c;
public:
    int dateTime() {
        cout << "Enter start date and start time: ";
        cin >> startTime.tm_mday >> startTime.tm_mon >> startTime.tm_year >> startTime.tm_hour >> startTime.tm_min >> startTime.tm_sec;
        cout << "Enter stop date and stop time: ";
        cin >> stopTime.tm_mday >> stopTime.tm_mon >> stopTime.tm_year >> stopTime.tm_hour >> stopTime.tm_min >> stopTime.tm_sec;
    }

    void mainMenu() {
        while (choice != 3) {
            cout << "Main menu options:";
            cout << "      1. Select a device to program (contains a submenu)" << endl;
            cout << "      2. Display current status of all devices" << endl;
            cout << "      3. Exit" << endl;
            cout << "Enter your option => ";
            cin >> choice;

            if (choice == 1) {
                subMenu();
            }
            else if (choice == 2) {
                cout << choice;
            }
            else {

            }
        }
        system("pause");
    }
    void subMenu() {
        cout << "Select a device:" << endl;
        cout << "         1. PVR" << endl;
        cout << "         2. Camera DVR" << endl;
        cout << "         3. Oven" << endl;
        cout << "Enter your option => ";
        cin >> choice1;

        if (choice1 == 1) {
            PVR n1;
        }
        else if (choice1 == 2) {
            DVR n2;
        }
        else {
            Oven n3;
        }
    }
    void newDevice() {
        if (c == 'Y' || c == 'y') {
            subMenu();
        }
        else {
            mainMenu();
        }
    }
};

class PVR : public PrgDevice {
private:
    int channel;
public:
    PVR() {
        cout << "Select the channel ==> ";
        cin >> channel;
        cout << endl;
        dateTime();
        cout << endl;
        cout << "Another device to program Y/N ? => ";
        newDevice();
    }
};

class DVR : public PrgDevice {
private:
    string position;
public:
    DVR() {
        cout << "Select the position ==> ";
        getline(cin, position);
        cout << endl;
        dateTime();
        cout << endl;
        cout << "Another device to program Y/N ? => ";
        newDevice();
    }
};

class Oven : public PrgDevice {
private:
    string food;
public:
    Oven() {
        cout << "What do you want to bake? ==> ";
        getline(cin, food);
        cout << endl;
        dateTime();
        cout << endl;
        cout << "Another device to program Y/N ? => ";
        newDevice();
    }
};

int main() {
    PrgDevice obj1;
    obj1.mainMenu();



    system("pause");
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类PRG设备{
私人:
tm开始时间;
tm停止时间;
智力选择;
综合选择1;
字符c;
公众:
int dateTime(){
cout>startTime.tm_mday>>startTime.tm_mon>>startTime.tm_year>>startTime.tm_hour>>startTime.tm_min>>startTime.tm_sec;
cout>stopTime.tm_mday>>stopTime.tm_mon>>stopTime.tm_year>>stopTime.tm_hour>>stopTime.tm_min>>stopTime.tm_sec;
}
void主菜单(){
while(选项!=3){

cout在使用对象类型之前,您需要定义它们。与其在类定义中内联代码,不如在代码顶部(或在includes中)将它们分开,然后当您实际开始使用它们时,编译器会知道您在做什么

class A {
    // Define stuff, but don't inline your methods
}

class B: public A {
    // Define stuff, but don't inline your methods
};

void A::subMenu() {
    // All your code
}

不清楚您在这里使用继承的原因。基本上,三个对象
PVR
DVR
roven
不需要从
PrgDevice
派生。一旦它们不派生,您可以将它们移动到
PrgDevice
之前,以便您可以在那里使用它们

class PVR {...
};
class DVR  {...
};
class Oven {...
};
class PrgDevice {...
};
因为
PVR
DVR
烤箱
构造器都是这样做的

dateTime();
cout << endl;
cout << "Another device to program Y/N ? => ";
newDevice();

但是,这并不能解决所有问题。您会不断重复:

  • main菜单
    调用
    子菜单
  • 子菜单
    调用
    新设备
  • newDevice
    调用
    子菜单
    newDevice
下一步需要修复。新设备应该停留在
子菜单中,或者返回主菜单。我们通过在
循环时添加
do
来实现这一点

void submenu()
{
  do {
      cout << "Select a device:" << endl;
      ...
      cout << "Another device to program Y/N ? => ";

      cin >> c;
  }
  while(c == 'Y' || c == 'y');
}
它输出

Main menu options: 
      1. Select a device to program (contains a submenu)
      2. Display current status of all devices
      3. Exit
Enter your option => Select a device:
         1. PVR
         2. Camera DVR
         3. Oven
Enter your option => Select the channel ==> 

Another device to program Y/N ? => Select a device:
         1. PVR
         2. Camera DVR
         3. Oven
Enter your option => Select the position ==> 

Another device to program Y/N ? => Main menu options: 
      1. Select a device to program (contains a submenu)
      2. Display current status of all devices
      3. Exit
Enter your option => 

您不能使用这些类,因为在使用它们时它们还没有定义。您应该将这些函数的定义从声明中分离出来,并在您要使用的类下面定义它们。一个类应该负责一件事。您所有的类都做得太多了。您正在混合用户输入和逻辑。基本类通常应该不知道它的派生类。修复你的代码需要很多重构,希望你很幸运,有人路过,可以看到这里更大的图景
echo -e "1 1 1 y 2 2\n n 3"
Main menu options: 
      1. Select a device to program (contains a submenu)
      2. Display current status of all devices
      3. Exit
Enter your option => Select a device:
         1. PVR
         2. Camera DVR
         3. Oven
Enter your option => Select the channel ==> 

Another device to program Y/N ? => Select a device:
         1. PVR
         2. Camera DVR
         3. Oven
Enter your option => Select the position ==> 

Another device to program Y/N ? => Main menu options: 
      1. Select a device to program (contains a submenu)
      2. Display current status of all devices
      3. Exit
Enter your option =>