Προς το περιεχόμενο

C++ ερώτηση


fonsde

Προτεινόμενες αναρτήσεις

Δημοσ.

έχω 2 κλάσεις, μια ταχυδρόμου, και μια του γραφείου. Θέλω μέσα στο γραφείο να βάλω K ταχυδρόμους. Τα K,MaxUnits,MinUnits θα τα στέλνω από τη main.

Πως t κάνω με χρήση πινάκων?

 

>class Postman
{
   int MaxUnits; 
   int MinUnits;
   int RemUnits; // Remaining units
   int CurUnits; // Current units
public:	
	Postman(int MaxUnits,int MinUnits)
       {
		RemUnits=0;
		CurUnits=0;
       }
       ~Postman();
};

class Office
{
   int K; // K postmen
   int units; // units in post office
   Postman *pstman;
   public:
Office(int k,int MaxUnits,int MinUnits): units(0), K(k)
       {
    
}
       ~Office();    
};

Δημοσ.
>class object
{
public:
object(void)
{
	printf("created\n");
}
~object(void)
{
	printf("deleted\n");
}
void foo(void)
{
	printf("foo called\n");
}
};
void main(void)
{
object *objs = new object[10];
objs[2].foo();
       delete[] objs;
getchar();
}

Δημοσ.

>class Postman
{
   int MaxUnits; 
   int MinUnits;
   int RemUnits; // Remaining units
   int CurUnits; // Current units
   public:    
       Postman(int MaxUnits,int MinUnits)
       {
           RemUnits=0;
           CurUnits=0;
       }
       ~Postman();
};

class Office
{
   int K; // K postmen
   int units; // units in post office
   Postman *pstman;
   public:
   Office(int k,int MaxUnits,int MinUnits): units(0), K(k)
       {
       pstman= new postman[K];  // <-----------------------------------------
   }
       ~Office();    
};  

 

 

αυτό έκανα αλλά μου βαράει

Δημοσ.

ΔΕΝ ΕΧΩ ΑΣΧΟΛΗΘΕΙ ΜΕ C++ CLASSES

 

αλλά από την εμπειρία μου σε JAVA

θα το έκανα κάπως έτσι

 

>
#include <iostream>
using namespace std;

class Postman {
   int MaxUnits,MinUnits,RemUnits,CurUnits;
 public:
       void set_values(int ,int);
       int get_MaxUnits();
       int get_MinUnits();
       Postman(){
           RemUnits=0;
           CurUnits=0;
       }
       
   Postman(int MaxUnits,int MinUnits)
       {
           RemUnits=0;
           CurUnits=0;
       }
       ~Postman(){};


};

void Postman::set_values(int a ,int {
   MaxUnits=a;
   MinUnits=b;
}
int Postman::get_MaxUnits(){
   return MaxUnits;
}
int Postman::get_MinUnits(){
   return MinUnits;
}

class Office
{
   int K; // K postmen
   int units; // units in post office
   Postman *pstman;
   public:
   Office(int k,int MaxUnits,int MinUnits): units(0), K(k)
       {
       pstman= new Postman[K];
   }
       ~Office(){};    
};  


int main () {

 Postman * d1 = new Postman[2];
d1[0].set_values(20,10);
d1[1].set_values(40,30);
cout<<"max units="<<d1[0].get_MaxUnits()<<" min units="<<d1[0].get_MinUnits()<<endl;
cout<<"max units="<<d1[1].get_MaxUnits()<<" min units="<<d1[1].get_MinUnits()<<endl;
 delete[] d1;
 system("pause");
 return 0;

}


Δημοσ.

η φαση παει ως εξης

απο εδω κανω το παρακατω quote

Consequently, if we are allocating an array of objects, there is no way to pass arguments to objects’ constructors. Therefore it is required that the objects that are stored in such an array have a no-argument constructor. In particular, it's okay if no constructor is defined for a given class-—we are allowed to allocate an array of such objects. No constructors will be called during initialization.

 

Με αλλα λόγια, πρεπει να υπαρχει τουλαχιστον ενας constructor της Postman που να μη παιρνει ορισματα ή να βάλεις default ορισματα στον ενα constructor που εχεις κανει.

Δηλ πχ

>
class Postman
{
   int MaxUnits; 
   int MinUnits;
   int RemUnits; // Remaining units
   int CurUnits; // Current units
   public:    
       Postman() //no arguments ctor
       {
           // do stuff here....
       } 

       Postman(int MaxUnits,int MinUnits)
       {
           RemUnits=0;
           CurUnits=0;
       }
       ~Postman();
};

 

ή

 

>
class Postman
{
   int MaxUnits; 
   int MinUnits;
   int RemUnits; // Remaining units
   int CurUnits; // Current units
   public:    

       // ctor with default arguments
       Postman(int MaxUnits=100,int MinUnits=1)
       {
           RemUnits=0;
           CurUnits=0;
       }
       ~Postman();
};

Δημοσ.

θα δωσεις τις τιμες ξεχωρισα μετα απο το initialization δηλαδη

>

class Office
{
   int K; // K postmen
   int units; // units in post office
   Postman *pstman;
   public:
   Office(int k,int MaxUnits,int MinUnits): units(0), K(k)
   {
       pstman= new Postman[K];

       for(int i=0; i<K; i++)
       {
           // exeis frontisei na yparxei mia methodos
           // SetValues stin Postman pou thetei tis times
           pstman[i].SetValues( ....... );
       }
   }
       ~Office(){};    
};  


εναλλακτικά, αν θες οι τιμές να οριζοντια στον constructor της Postman, μπορεις να αλλαξεις την μεταβλητη pstman απο ποιντερ σε Postman που ειναι, σε pointer to pointer to Postman, δηλαδη να γινει πινακας δεικτων

 

Δες πως γινεται:

>
class Office
{
   int K; // K postmen
   int units; // units in post office
   Postman **pstman; <--2 αστερακια τώρα

public:
   
   Office(int k,int MaxUnits,int MinUnits): units(0), K(k)
   {
       pstman= new Postman*[K]; <<--- προσεξε το * πριν το [

       for(int i=0; i<K; i++)
       {
           // κάθε στοιχειο του pstman ειναι δεικτης σε ενα αντικειμενο Postman
           // και το δημιουργουμε κανονικα με τη new
           pstman[i] = new Posman( i, i*2 ); <-- τα ορισματα ειναι τυχαια, βαζεις οτι θες
       }
   }
       
   ~Office()
   {
       if(!pstman) return;

       // πρεπει να κανουμε delete ενα-ενα τα στοιχεια της pstman
       for(int i=0; i<K; i++)
           delete pstman[i];
       
       //... και τελος να κανουμε delete[] την ιδια την πστμαν (με [] οκ?)
       delete[] pstman;
   };    

   void ShowPostmans()
   {
       if(pstman==NULL) return;

       for(int i=0; i<K; i++)
       {
            printf("postman %d:\n",i);
            printf("MinUnits=%d, MaxUnits=%d\n",pstman[i]->MinUnits, pstman[i]->MaxUnits);
       }
    }
   
};   

Δημοσ.

>class Postman
{
   int MaxUnits; 
int MinUnits;
   int RemUnits; // Remaining units
   int CurUnits; // Current units
Parcel *dema;
   Letter *gramma;
public:	
	Postman();
	Postman(int MaxUnits,int MinUnits)
       {
		RemUnits=0;
		CurUnits=0;
       }
       ~Postman();  
	void setmin(int);
	void setmax(int);
	int assign(int, int, int);
	void finish();
};

class Office
{
int K; // K postmen
   int units; 
   Postman *pstman;
   Letter *gramma;
Parcel *dema;
   public:
	Office(int k, int mx, int mn): units(0), K(k)
       {
		int i;

		pstman=new Postman[K];
		for (i=0;i<K;i++){
			pstman[i].setmin(mn);
			pstman[i].setmax(mx);}

	}
       ~Office();    
	int arrive(int, int, int);
};

 

τα CurUnits , RemUnits πρέπει να τα βάλω και αυτά η δεν χρειάζεται ?

Δημοσ.
τα CurUnits , RemUnits πρέπει να τα βάλω και αυτά η δεν χρειάζεται ?

εσυ ξερεις, αν χρειαζεται να τους βαλεις μια αρχικη τιμη, τοτε φυσικα ναι, πρεπει να τα βαλεις

αν παλι ξερεις πως παντα ξεκινανε απο 0, τοτε μπορεις να τα αρχικοποιησεις και αυτα στον constructor του Postman:

>
Postman()
{ 
   RemUnits=0; 
   CurUnits=0; 
}

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...