要在对象数组(CD)中动态添加?

时间:2011-04-24 14:43:55

标签: c++

#include <iostream>
#include <cstring>

using namespace std;

class CD
{

public : 
    void getCDdetails(CD *);
    void printCDD(CD *) const;
    int putCount() const
    {
    return count;
    }

private :
static int count;
char Title[60];
char Artist[60];
char Type;
int Tracks;
};


int CD :: count=0;

void CD :: getCDdetails(CD * cd)
{
cd=new CD[putCount() + 1];  //THIS LINE IS NOT HELPING
    for(int j=putCount(); j < 100; j++)
    {
    cout << "\nTitle :";
    cin >> cd[j].Title;
    cout << "\nArtist :";
    cin >> cd[j].Artist;
    cout << "\nType :";
    cin >> cd[j].Type;
    cout << "\nTracks";
    cin >> cd[j].Tracks;
    count++;
    cout << "Details added, Press enter to continue";
    fflush(stdin);
    getchar();
    break;;
    }
    printCDD(cd);
}


void CD :: printCDD(CD * cd) const 
{

cout << "\nThere are " << putCount() << " number of CD details in the database at  present";
    for(int j=0; j < putCount(); j++)
    {
    cout << "\nTitle : " << cd[j].Title;
    cout << "\nArtist : " << cd[j].Artist;
    cout << "\nType : " << cd[j].Type;
    cout << "\nTracks " << cd[j].Tracks << endl;
    }
    fflush(stdin);
    cout << "\nPress enter to continue";
    getchar();
}


void display_menu();

int main()
{
display_menu();
return 0;
}

void display_menu()
{
CD * st;
int choice;
int j;
for(;;)
{
system("clear");
cout << "\n\t\t\t\tCurrent number of CDs in the CD database is  " << st[0].putCount();
cout << "\n1:Enter a CD detail"   \
     << "\n2:Print all the CD details" \
     << "\n3:Quit"  \
     << "\nEnter your choice : ";
cin >> choice;
     switch(choice)
     {
     case 1 :
        st[0].getCDdetails(st);
            break;
     case 2 :
            st[0].printCDD(st);
            break;
     case 3 :
           exit(0);
     default :
            cout << "\nPlease enter valid choice";
            fflush(stdin);
            getchar();
            break;
     }
  }         
}

这一行(cd = new CD [putCount()+ 1];)没有帮助,因为每次我去向对象数组添加数据时,可能会分配一大块新内存,之前添加的数据会丢失

示例:当我向ist对象添加其O.K

 Current number of CDs in the CD database is  0
  1:Enter a CD detail
  2:Print all the CD details
  3:Quit
  Enter your choice : 1
  Title :a
  Artist :a
  Type :a
  Tracks1
  Details added, Press enter to continue

There are 1 number of CD details in the database at present
Title : a
Artist : a
Type : a
Tracks 1

Press enter to continue

1:Enter a CD detail
2:Print all the CD details
3:Quit
Enter your choice : 1
Title :a
Artist :a
Type :a
Tracks1
Details added, Press enter to continue

There are 1 number of CD details in the database at present
Title : a
Artist : a
Type : a
Tracks 1

                   Press enter to continue

                     Current number of CDs in the CD database is  1
1:Enter a CD detail
2:Print all the CD details
3:Quit
Enter your choice : 1
Title :b
Artist :b
Type :b
Tracks2
Details added, Press enter to continue

There are 2 number of CD details in the database at present
**Title :
Artist :
Type :
Tracks 0**   //ITS NOT DISPLAYING THAT WAS ADDED JUST BEFORE

Title : b
Artist : b
Type : b
Tracks 2

按Enter继续

如果看到它的痛苦可以有人向我展示一些如何动态添加到对象数组的示例(我只是尝试先使用新运算符)并且如果我成功我还没有尝试链表提前并使用链表。

1 个答案:

答案 0 :(得分:1)

您可能更容易使用像vector这样的动态容器。这样您就不必担心为数组动态分配空间。如果这是作业,你应该这样做。