C++로 응용 프로그램을 만들려고합니다. 응용 프로그램에서 기본 생성자와 3 개의 인수가있는 다른 생성자가 있습니다. 사용자는 키보드에서 기본이 아닌 생성자를 사용하여 객체 배열을 만드는 데 사용할 정수를 제공합니다. 불행히도 나는 지금까지 그것을 끝낼 수 없었습니다. 왜냐하면 저는 기본이 아닌 생성자를 사용할 객체의 배열 생성에 문제가 있기 때문입니다. 제안이나 도움이 필요하십니까? 내가 cin
걸릴 것C++에서 생성자 및 객체 배열
#include<iostream>
#include<cstring>
#include<cstdlib>
#include <sstream>
using namespace std;
class Station{
public:
Station();
Station(int c, char *ad, float a[]);
~Station();
void setAddress(char * addr){
char* a;
a = (char *)(malloc(sizeof(addr+1)));
strcpy(a,addr);
this->address = a;
}
void setCode(int c){
code=c;
}
char getAddress(){
return *address;
}
int getCode(){
return code;
}
float getTotalAmount(){
float totalAmount=0;
for(int i=0;i<4;i++){
totalAmount+=amount[i];
}
return totalAmount;
}
void print(){
cout<<"Code:"<<code<<endl;
cout<<"Address:"<<address<<endl;
cout<<"Total Amount:"<<getTotalAmount()<<endl;
cout<<endl;
}
private:
int code;
char *address;
float amount[4];
};
Station::Station(){
code= 1;
setAddress("NO ADDRESS GIVEN");
amount[0]= 0.0;
amount[1]= 0.0;
amount[2]= 0.0;
amount[3]= 0.0;
}
Station::Station(int c, char *ad, float a[]){
if((c>=1&& c<=10)){
code=c;
address=ad;
for(int i=0;i<4;i++){
amount[i]=a[i];
}
}else{
code= 1;
setAddress("NO ADDRESS GIVEN");
amount[0]= 0.0;
amount[1]= 0.0;
amount[2]= 0.0;
amount[3]= 0.0;
}
}
Station::~Station(){
}
int main(){
int size,code;
char *addrr;
addrr = (char *)(malloc(sizeof(addrr+1)));
float mes[4];
do{
cout<<"size of array:";
cin>>size;
}while(size<=0 || size>=11);
// Station *stations= new Station[size];
// Station** stations = new Station*[size];
Station stations[size];
for(int i=0;i<size;i++){
cout<<"code:";
cin>>code;
cout<<"address:";
cin>>addrr;
double amo=0;
for(int k=0;k<4;k++){
cout<<"values"<<k+1<<":";
cin>>mes[k];
}
}
/*
for(int q=0;q<size;q++){
stations[q].print();
}
*/
return 0;
}
값이 나는 배열의 객체에 할당 할!
이 우리에게 코드를 제공 ... –
HTTP : //mattgemmell.com/2008/12/08/what-have-you-tried/ – FailedDev
배열 생성을 보여 주시겠습니까? new 또는 malloc을 사용합니까? – LostMohican