Constructors and array of object in C++

This is a discussion on Constructors and array of object in C++ within the C++ Programming forums, part of the General Programming Boards category; Hello, im trying to create an application in C++. In the application i have the default constructor and another constructor ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    12

    Constructors and array of object in C++

    Hello, im 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 im having issues with the creation of the array of objects that they will use the non default constructor. Any suggestions or help?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    What have you done so far in code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    Code:
      
    #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;
    }
    thats what i have so far..
    i want to initialize the array objects according to the values that i'll get from cin
    thanx
    Last edited by kspen; 11-29-2011 at 07:53 AM. Reason: addiotional info

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Okay, so how does it not work?

    Indent your code properly, e.g., your main function should look like:
    Code:
    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;
    }
    Next, stop using malloc. If you need to do such manual memory management, use new and delete or new[] and delete[] instead. If allowed, use std::string instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    thank you for your answer, but i really dont get it!
    how im going to initialize the array of objects using the values from the cin and the non default constructor?
    as i create the array of objects now it is using the default constructor.
    Last edited by kspen; 11-29-2011 at 09:12 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Patience. First clean up the code to make it readable and use proper C++ constructs, then we can talk about your problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    I cant use string and i have already clean the code, can you please provide me an answer to my problem?

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by kspen View Post
    i have already clean the code
    Post it..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by kspen View Post
    thank you for your answer, but i really dont get it!
    how im going to initialize the array of objects using the values from the cin and the non default constructor?
    as i create the array of objects now it is using the default constructor.
    Although technically possible, non-default constructors typically aren't used with arrays of objects. Consider using the default constructor followed by direct assignment of each element (eg: array[index] = value), an std::vector, or what have you.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    If you see someone trying to change a tyre by using a hammer to remove the nuts from the wheel, the first step is not to tell them how to swing a hammer properly. The first step is to first show them the correct tool, and then move on to show them the correct method (e.g. losen the nuts a little before jacking the car up all the way).
    This is all we are doing here. We're helping you fix the glaringly obvious problems so that we can then focus on the minor mistakes.

    This is very wrong:
    Code:
        char *addrr;
        addrr = (char *)(malloc(sizeof(addrr+1)));
    You need to fix such things first by getting rid of the malloc and instead considering the use of new[]. Then consider throwing away the new[] and consider just using a fixed-sized array on the stack.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Objects with Constructors
    By Eman in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2010, 03:49 PM
  2. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-17-2008, 11:46 PM
  3. 2d array of object pointers
    By Potterd64 in forum C++ Programming
    Replies: 17
    Last Post: 07-20-2006, 01:27 PM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM
  5. Bitmap from array to an object
    By steve_i83 in forum Windows Programming
    Replies: 2
    Last Post: 09-28-2001, 08:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21