Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++如何直接通过程序自动创建对象_C++_Oop_Object_Constructor - Fatal编程技术网

C++如何直接通过程序自动创建对象

C++如何直接通过程序自动创建对象,c++,oop,object,constructor,C++,Oop,Object,Constructor,好的,我搜索了一些问题,但没有得到答案,或者没有使用合适的术语 if(choice == 2){ string tempName, tempAddress; int tempNic,tempContact; cout << "\n\t\t*\tWelcome to Our Sponsor Registeration Section\t*\n\n"; cout << "Please enter your name : "; cin>>te

好的,我搜索了一些问题,但没有得到答案,或者没有使用合适的术语

if(choice == 2){
    string tempName, tempAddress; int tempNic,tempContact;
    cout << "\n\t\t*\tWelcome to Our Sponsor Registeration Section\t*\n\n";
    cout << "Please enter your name : "; cin>>tempName;
    cout << "Please enter your National Identity Card Number : "; cin>>tempNic;
    cout << "Please enter your Contact Number : "; cin>>tempContact;
    cout << "Please enter your Address : "; cin>>tempAddress;
    //  prototype Sponsor(string n, string add, int nic_n, int phone) constructor
    Sponsor (Constructor goes here) // how to make many objects now?
}
代码粘贴在这里

检查第69行,我将使用构造函数添加值,这样我可以添加1个对象,但是如果使用程序的人想要添加更多对象,我应该怎么做

我知道我需要封装61到70之间的内容。
请帮我解决这个问题。

我猜你想让它循环?我建议做一个循环。 我已经很久没有使用向量了,教授们禁止使用它,所以我可能会犯一些错误,但你会得到总的观点

bool stop = false; //This is to check after each loop if it should continue or not
char contChoice;
vector<Sponsor> sponsors;

while(!stop){
    if(choice == 2){
        string tempName, tempAddress; int tempNic,tempContact;
        cout << "\n\t\t*\tWelcome to Our Sponsor Registeration Section\t*\n\n";
        cout << "Please enter your name : "; cin>>tempName;
        cout << "Please enter your National Identity Card Number : "; cin>>tempNic;
        cout << "Please enter your Contact Number : "; cin>>tempContact;
        cout << "Please enter your Address : "; cin>>tempAddress;
        //  prototype Sponsor(string n, string add, int nic_n, int phone) constructor
        sponsors.push_back(Sponsor(tempName, tempAddress, tempContact, tempNic)); 
        //Add whatever other arguments you want to pass in, in whatever order
        cout << "Do you want to continue? [Y/N]: "; cin>>contChoice;
        if(contChoice == 'N' || contChoice == 'n') 
              stop = true;
        else stop = false; //This isn't really necessary since it is false by default
    }
}

但我也建议您至少在赞助商中设置成员功能。您还可以使用动态数组并使其展开,这比向量更复杂,实际上更复杂。

请在问题正文中直接包含相关代码。如果有我们应该阅读的特殊行,则用注释等标记出来。想想看,如果链接消失了会发生什么,这将使这个问题毫无价值。请,然后学习如何创建。我不太确定您想要做什么,但该特定对象类型的列表或向量可能会有所帮助。将用户输入请求放入循环中,并将创建的发起人添加到向量中。您可以创建一个对象数组,然后在循环中填充。我知道将循环放入何处。但将对象添加到向量中对我来说是一件新鲜事,这就是当最终用户从菜单中选择“添加用户”选项时,我想添加新对象的地方,实际上不是预定义的编码对象。@Baqarhussan它回答了你的问题吗?还是你还在想什么?