抽象基类没有正确返回

时间:2013-10-05 10:58:12

标签: c++ abstract abc

我还没有完全理解抽象基类。这是我为我的作业创建的,它似乎主要工作,除了它不会返回通过派生类添加的任何内容。每当我选择hasDMA或缺少DMA时,我都不会从颜色或样式中返回结果。

Main.cpp的

#include <iostream>
#include <conio.h>
#include "DMA.h"

using namespace std;

const int RECORDS = 1;
const int LEN = 40;

int main()
{
    ABC * p_records[RECORDS];

    int i;
    for (i = 0; i < RECORDS; i++)
    {
        char temp[LEN];
        int temprate;
        char choice;

        cout << "\nEnter label name: ";
        cin.getline(temp, LEN);
        cout << "Enter Rating: ";
        cin >> temprate;

        cout << "Enter 1 for lacksDMA or 2 for hasDMA: ";
        while (cin >> choice && (choice != '1' && choice != '2'))
            cout << "Enter 1, 2: ";
        if (choice == '1')
        {
            char tempcolor[LEN];
            cout << "Enter the color: ";
            cin.getline(tempcolor, LEN);
            p_records[i] = new lacksDMA(temp, temprate, tempcolor);         
        }
        else
        {
            char tempstyle[LEN];
            cout << "Enter the style: ";
            cin.getline(tempstyle, LEN);
            p_records[i] = new hasDMA(tempstyle, temp, temprate);           
        }
        while (cin.get() != '\n')
            continue;
    }
    cout << endl;
    for (i = 0; i < RECORDS; i++)
    {
        p_records[i]->View();
        cout << endl;
    }

    for (i = 0; i < RECORDS; i++)
    {
        delete p_records[i];
    }

    cout << "\nPress any key to continue...";
    cin.sync();
    _getch();

    return 0;
}

DMA.cpp

#include "DMA.h"

using namespace std;


ABC::ABC(const char * l, int r)
{
    label = new char [strlen(l) + 1];
    strcpy(label, l);
    rating = r;
}

ABC::ABC(const ABC & rs)
{
    label = new char[strlen(rs.label) + 1];
    strcpy(label, rs.label);
    rating = rs.rating;
}

ABC::~ABC()
{   
}

ABC & ABC::operator=(const ABC & rs)
{
    if (this == &rs)
        return *this;
    delete [] label;
    label = new char[strlen(rs.label) + 1];
    strcpy(label, rs.label);
    rating = rs.rating;
    return *this;
}

ostream & operator<<(ostream & os, const ABC & rs)
{
    rs.View();
    return os;
}

void ABC::View() const
{
    cout << "\nLabel: " << label << endl;
    cout << "Rating: " << rating << endl;
}

baseDMA::baseDMA(const char * l, int r) : ABC(l,r)
{
}

lacksDMA::lacksDMA(const char * l, int r, const char * c) : ABC(l,r)
{
    strncpy(color, c, 39);
    color[39] = '\0';
}

lacksDMA::lacksDMA(const ABC &rs, const char * c) : ABC(rs)
{
    strncpy(color, c, 39);
    color[39] = '\0';
}

void lacksDMA::View() const
{
    ABC::View();
    cout << "Color: " << color << endl; 
}

hasDMA::hasDMA(const char * s, const char * l, int r) : ABC(l,r)
{
    style = new char [strlen(s) + 1];
    strcpy(style, s);   
}

hasDMA::hasDMA(const char * s, const ABC & rs) : ABC(rs)
{
    style = new char [strlen(s) + 1];
    strcpy(style, s);
}

hasDMA::hasDMA(const hasDMA & hs) : ABC(hs)
{
    style = new char [strlen(hs.style) + 1];
    strcpy(style, hs.style);
}

hasDMA::~hasDMA()
{
    delete [] style;
}

void hasDMA::View() const
{
    ABC::View();
    cout << "Style: " << style << endl;
}

DMA.h

#ifndef DMA_H_
#define DMA_H_
#include <iostream>

using namespace std;

// Abstract Base Class
class ABC
{
private:
    char * label;
    int rating;
public:
    ABC(const char * l = "null", int r = 0);
    ABC(const ABC & rs);
    virtual ~ABC() = 0;
    virtual ABC & operator=(const ABC & rs);
    virtual void View() const;
    friend ostream & operator<<(ostream & os, const ABC & rs);
};

// Former Base Class Using DMA
class baseDMA: public ABC
{
private:

public:
    baseDMA(const char * l = "null", int r = 0);
};

// derived class without DMA
// no destructor needed
// uses implicit copy constructor
// uses implicit assignment operator
class lacksDMA : public ABC
{
private:
    char color[40];
public:
    lacksDMA(const char * l = "null", int r = 0, const char * c = "blank");     
    lacksDMA(const ABC & rs, const char * c);
    virtual void View() const;
};

// derived class with DMA
class hasDMA : public ABC
{
private:
    char * style;
public:
    hasDMA(const char * s = "none", const char * l = "null", int r = 0);
    hasDMA(const char * s, const ABC & rs);
    hasDMA(const hasDMA & hs);
    ~hasDMA();
    hasDMA & operator = (const hasDMA & rs);
    void View() const;
};

#endif

1 个答案:

答案 0 :(得分:0)

在评论中,您说显示字符串“Color:”,但不显示examaple输入“red”。因为显示“颜色:”,这不是代码的抽象/虚拟部分失败的问题(否则甚至不会显示“颜色:”)。

失败在输入中。当您读取“1”或“2”的选择时,您只读取一个字符,但用户实际上也输入了额外的新行,因此下一个cin.getline将消耗此额外的新行并返回一个空字符串。对于所有输入,您应该坚持使用cin.getline,即使输入只是一个字符,即使输入是数字。更容易永远不要使用cin >>,并且自己处理输入中的错误,而不是将operator>>getline混合,然后在它们不能很好地协同工作时处理所有边界情况。 / p>

尽管这不是答案的一部分,但这里有一些一般建议:

1)在C ++中尽可能避免使用new char[x]。如果std::string不够好,请使用std::vector<char>表示字符串,std::string表示非常罕见的情况。

1b)你在delete [] label的析构函数中忘记了ABC。如果它是std::string你就不必担心这些事情。

2)源文件(.cpp)中的use namespace std是可以的,并且仅在所有#includes之后,但从不在头文件(.h)中使用它。决不!在标头文件中使用全名(例如std::string而不是string

编辑:所以,需要一些代码示例。就像我说的那样,坚持cin.getline,并避免operator>>,即使输入只是一个字符,即使输入是一个整数:

将行cin >> temprate;替换为:

    cin.getline(temp, LEN);
    temprate = strtol(temp, NULL, 10);

替换两行while循环

    while (cin >> choice && (choice != '1' && choice != '2'))
        cout << "Enter 1, 2: ";

这些for行在中间退出(我首选的无限循环方式):

    for (;;)
    {
        cin.getline(temp, LEN);
        choice = temp[0];

        if (choice == '1' || choice == '2')
            break;

        cout << "Enter 1, 2: ";
    }