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

Posted

技术标签:

【中文标题】在基类中创建派生类的对象【英文标题】:Create objects of derived classes in base class 【发布时间】:2020-03-17 19:17:54 【问题描述】:

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

#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;

以下是错误:

错误 C2065:“PVR”:未声明的标识符 错误 C2146:语法错误:缺少“;”在标识符“n1”之前 错误 C2065:“n1”:未声明的标识符 错误 C2065:“DVR”:未声明的标识符 错误 C2146:语法错误:缺少“;”在标识符“n2”之前 错误 C2065:“n2”:未声明的标识符 错误 C2065:“烤箱”:未声明的标识符 错误 C2146:语法错误:缺少“;”在标识符“n3”之前 错误 C2065:“n3”:未声明的标识符 1>完成建设项目“Project1.vcxproj”——失败。 ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

请帮助我。谢谢。

【问题讨论】:

你不能使用那里的类,因为在你使用它们时它们还没有被定义。您应该将这些函数的定义从声明中分离出来,并将它们定义在您要使用的类下面。 一个类应该负责一件事。你所有的课程都做得太多了。您正在混合用户输入和逻辑。基类通常不应该知道它的派生类。修复你的代码需要大量的重构,希望你很幸运,并且有人可以在这里看到更大的画面 【参考方案1】:

尚不清楚您为什么在这里使用继承。本质上,PVRDVROven 这三个对象不需要派生自PrgDevice。一旦它们没有派生,您可以将它们移动到 PrgDevice 之前,以便您可以在那里使用它们。

class PVR ...
;
class DVR  ...
;
class Oven ...
;
class PrgDevice ...
;

因为 PVR DVROven 构造函数都这样做

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

我们可以将它移到PrgDevice::submenu 函数中。

void subMenu()  ...
  if (choice1 == 1) 
     PVR n1;
  
  else if (choice1 == 2) 
     DVR n2;
  
  else 
    Oven n3;
  
  dateTime();
  cout << endl;
  cout << "Another device to program Y/N ? => ";
  newDevice();

但这并不能解决您的所有问题。你一直在递归:

mainMenu 致电subMenu submenu 致电newDevice newDevice 致电submenunewDevice

接下来需要修复。新设备真的应该留在submenu返回 到主菜单。我们通过添加 do while 循环来做到这一点

void submenu()

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

      cin >> c;
  
  while(c == 'Y' || c == 'y');

我留下了一个看似有效的版本,其中注释掉了 datetime 函数 here

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 => 

【讨论】:

【参考方案2】:

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

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

【讨论】:

以上是关于在基类中创建派生类的对象的主要内容,如果未能解决你的问题,请参考以下文章

在基类和派生类中使用基类的装饰器

将派生类对象存储在基类变量中

将派生类对象存储在基类变量中

在基类类型的向量中保存的基对象和派生对象被切片

基类只有一个派生类可以吗?

调用派生类的构造函数在基类的构造函数之前执行