簡體   English   中英

如何使用new運算符創建對象並通過for循環使用它們

[英]How to create objects using new operator and use them using for loop

#include<iostream.h>
#include<conio.h>
class account \\simple class
{
    int accno;
    int balance;
    public:
    account()
    {
        cout<<"Enter Account No : ";
        cin>>accno;
        cout<<"Enter Balance : ";
        cin>>balance;
    }
    void display()
    {
        cout<<"\n" <<accno<<"\t"<<balance;
    }
};
void main()
{
    int n;
    clrscr();
    cout<<"Enter no of Accounts : ";
    cin>>n;
    account *ob = new account[n]; \\is this statement work..??
    cout<<"\n Account No \t Balance \n";
    for(int i=0;i<n;i++)
    {
        ob[i]->display(); \\  how to access all object's display function.
    }
    delete ob; 
    getch();
}

//在ob [i]-> display()中,我得到一個錯誤..指向左側所需結構的指針// //但是如果我編寫代碼ob-> display(),則僅第一條記錄顯示n次。 請提供任何解決方案的人... thnks

一旦獲得第i個元素,它就不再是指針。 所以

ob[i].display();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM