具有基本类,抽象类和派生类的C ++程序,在主函数中使用所有相关数据和方法

问题是:定义一个类Record,用于存储与旋律(艺术家,标题,时长)有关的数据。 抽象类(播放列表)包含指向记录数组的指针作为私有变量。 指针通过内存分配过程在构造函数中初始化 (记录的数量由用户定义)。 该类包含每个记录字段的setter和getter方法,以及根据条件对记录数组进行排序的抽象方法(纯虚拟方法) 编码在接收到的参数中(1 =按标题排序,2 =按艺术家排序,3 =按持续时间排序)。 abstract方法在继承Playlist类的另一个类(PlaylistImplementation)中实现。 在main()函数中,实例化PlaylistImplementation类,并初始化和使用所有相关数据和方法。

我写了代码,但有49个错误,主要问题是Record类,它没有默认构造函数,并且不能使用我在PlaylistImplementation类中使用的“ aux”对象。

//AbstractClass.h

#pragma once
#include<iostream>
using namespace std;
#include"Header.h"

class Playlist
{
protected:
    Record* r;
    int n;
public:
    Playlist(int nn)
    {
        nn = n;
        r = new Record[n];
    }
    void init()
    {
        char t[20], a[30];
        int d;
        for (int i = 0; i < n; i++)
        {
            cout << "Song " << i + 1 << " :";
            cout << "Title:";
            cin >> t;
            r[i].set_Title(t);
            cout << "Artist:";
            cin >> a;
            r[i].set_Artist(a);
            cout << "Duration (in sec):";
            cin >> d;
            r[i].set_Duration(d);
        }
    }
    void setRecord(Record rr, int n) //setter
    {
        r[n] = rr;
    }
    Record getRecord(int n) //getter
    {
        return r[n];
    }
    virtual void ord(int c, int n) = 0; //pure virtual function
};

派生类:

//Playlist.h

#include"AbstractClass.h"
//#include"Header.h"   //i have errors if i include the header.h file too
using namespace std;

class PlaylistImplementation :public Playlist
{
public:
    PlaylistImplementation(int n) :Playlist(n) {}
    void display(int n)
    {
        for (int i = 0; i < n; i++)
        {
            cout << "\nTitle: " << r[i].get_Title();
            cout << "\nArtist: " << r[i].get_Artist();
            cout << "\nDuration: " << r[i].get_Duration() << " seconds";
        }
    }
    void ord(int c, int n)
    {
        switch (c)
        {
        case 1:
        {
            char s1[20], s2[30];
            cout << "\nAlphabetical order of Titles:\n ";
            for (int i = 0; i < n; i++)
                for (int j = i + 1; n - 1; j++)
                {
                    strcpy_s(s1, 20, r[i].get_Title());
                    strcpy_s(s2, 30, r[i].get_Title());
                    if (strcmp(s1, s2) > 0)
                    {
                        Record aux;
                        aux = r[i];
                        r[i] = r[j];
                        r[j] = aux;
                    }

                }
            break;
        }
        case 2:
        {

            char s1[20], s2[30];
            cout << "\nAlphabetical order of Artists:\n ";
            for (int i = 0; i < n; i++)
                for (int j = i + 1; n - 1; j++)
                {
                    strcpy_s(s1, 20, r[i].get_Artist());
                    strcpy_s(s2, 30, r[i].get_Artist());
                    if (strcmp(s1, s2) > 0)
                    {
                        Record aux;
                        aux = r[i];
                        r[i] = r[j];
                        r[j] = aux;
                    }

                }
            break;
        }
        case 3:
        {

            char d1, d2;
            cout << "\nAscending order of duration:\n ";
            for (int i = 0; i < n; i++)
                for (int j = i + 1; n - 1; j++)
                {
                    d1 = r[i].get_Duration();
                    d2 = r[j].get_Duration();
                    if (d1 > d2)
                    {
                        Record aux;
                        aux = r[i];
                        r[i] = r[j];
                        r[j] = aux;
                    }
                }
            break;
        }
        default:
            cout << "\nEnter 1,2 or 3 key\n";
            break;
        }

    }
};

记录类:

//Header.h


#include"AbstractClass.h"
#include"Playlist.h"

class Record
{
private:
    char artist[20];
    char title[30];
    int duration;
public:
    Record(char* a, char* t, int d)
    {
        *artist = *a;
        *title = *t;
        duration = d;
    }
    //setters
    void set_Artist(char* a)
    {
        strcpy_s(artist, 20, a);
    }
    void set_Title(char* t)
    {
        strcpy_s(title, 30, t);
    }
    void set_Duration(int d)
    {
        duration = d;
    }
    //getters
    char* get_Artist()
    {
        return artist;
    }
    char* get_Title()
    {
        return title;
    }
    int get_Duration()
    {
        return duration;
    }

};

主要的():

//Source.cpp

#include"AbstractClass.h" //abstract class
#include"Playlist.h" //derived class
#include"Header.h"

int main()
{
    int n;
    cout << "\nEnter nr of records:";
    cin >> n;
    PlaylistImplementation ob(n);
    ob.init();
    int c;
    cout << "\nEnter a key in order to organize the playlist:";
    cin >> c;
    ob.ord(c, n);
    cout << "\nThe ordered list:";
    ob.display(n);

}

我试图让构造函数记录类,但是什么也没解决

Errors: Errors from the code

errors

评论
  • jiafeng
    jiafeng 回复

    您永远不会实例化Record类:

    if (d1 > d2) {
      Record aux;
      aux = r[i];
      r[i] = r[j];
      r[j] = aux;
    }
    

    因为您从不实例化Record,所以C ++尝试调用默认构造函数,但是它不存在,因此c ++崩溃了。