C ++中的对象的构造函数和数组 [英] Constructors and array of object in C++

查看:120
本文介绍了C ++中的对象的构造函数和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中创建一个应用程序。在应用程序中,我有默认构造函数和另一个带有3个参数的构造函数。
用户从键盘提供一个整数,它将用于使用非默认构造函数创建一个对象数组。
不幸的是,我还没有能够完成它,直到现在,因为我有问题与对象数组的创建,他们将使用非默认构造函数。
任何建议或帮助?

I'm trying to create an application in C++. In the application I have the default constructor and another constructor with 3 arguments. The user is providing from the keyboard an integer that it will be used to create an array of objects using the non default constructor. Unfortunately I haven't been able to finish it till now, since I'm having issues with the creation of the array of objects that they will use the non default constructor. Any suggestions or help?

#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;
}

我将从 cin 我想将它们分配给数组的对象!

the values that I'll take from cin I want to assign them to the objects of the array!

推荐答案

- 初始化,然后用期望的对象填充数组:

You can either create the array default-initialized and then fill the array with the wanted object:

foo arr[10];
std::fill(arr, arr+10, foo(some, params));

也可以使用 std :: vector 并执行:

std::vector<foo> arr(10, foo(some, params));

这篇关于C ++中的对象的构造函数和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆