Klassenattribute miteinander vergleichen



  • Hallo,

    Ich habe eine Klasse namens Article erstellt und werden da ein Name, Preis, EAN und eine Nummer zufällig erzeugt bei jedem Aufruf des Standardkonstruktors. Da habe ich in der main 10 mal den Standardkonstruktor aufgerufen und alle Member in ein Array gesteckt. Das Array wird dann an eine Unterfunktion weiter gegeben, in der geprüft werden soll, welcher Member den höchsten Preis hat, bzw am meisten kostet. Das wollte ich so machen, wie ich es gewohnt bin, aber kennt c++ dann z.b. dat[i].price nicht mehr. Hat wer eine Idee? Den Code, wie ich mir den vorstelle, habe ich mal angefügt.

    Article maxPreis(int n, Article dat[])
    {
    	// TODO: find most expensive article 
    	Article maxPreis = dat[0];
    	for (int i = 0; i < n; i++) {
    		if (maxPreis.price < dat[i].price) {
    			maxPreis = dat[i];
    		}
    	}
    
    	return maxPreis;
    }
    


  • Zeig mal die Klasse.

    Edit:
    Besser: Zeig ein auf das Notwendigste gekürztes, aber vollständiges, kompilierbares Programm, an dem das Problem nachvollziehbar ist.



  • Ich kann dir auch gerne den ganzen Code geben. Ist jetzt noch recht übersichtlich

    // ClassWarehousing.cpp
    #include <iostream>
    #include <sstream>
    #include <ctime>
    #include <string>
    #define LINE "___________________________________________\n"
    using namespace std;
    
    //----------------------- class declaration -------------------------
    class Article
    {
    private:
    	// TODO: Attributes
    
    	string name;
    	unsigned int productCode;
    	unsigned int number;
    	double price;
    
    public:
    	// TODO: signatures for Constructor, getPrice, getNumber, toString;
    
    	Article();
    	~Article() {};
    	void printAll();
    	void getCapital();
    	string toString();
    
    };
    
    //---------------------- Definition/Implementation of Methods -------
    // default/parameterless constructor
    Article::Article() {
    	// TODO: complete constructor, input name, use random values for the rest
    	name = "Welle";
    	productCode = rand() % 10000;
    	number = rand() % 100;
    	price = (rand() % 10000) / 100;
    }
    // returns a "nice" string consisting of attributes
    string Article::toString()
    {
    	stringstream s;     // stream from  sstream.h
    	s << endl << "____________ Article ___________" << endl
    		<< "Name: " << name << endl
    		<< "EAN: " << productCode << endl
    		<< "Number: " << number << endl
    		<< "Price: " << price << " Euro" << endl
    		<< LINE;
    	return s.str();    // convert stream s into a string
    }
    
    //------------------- definition of functions -----------------------
    
    Article maxPreis(int n, Article dat[])
    {
    	// TODO: find most expensive article 
    	Article maxPreis = dat[0];
    	for (int i = 0; i < n; i++) {
    		if (maxPreis.price < dat[i].price) {
    			maxPreis = dat[i];
    		}
    	}
    
    	return maxPreis;
    }
    
    void printAll(int n, Article dat[])
    {
    	// TODO: display array dat with toString
    	for (int i = 0; i < n; i++) {
    		cout << dat[i].toString();
    	}
    }
    
    double getCapital(int n, Article dat[])
    {
    	// TODO: calculate circulating capital
    	return 0;
    }
    
    //---------------------- main()-function ----------------------------
    int main() {
    	// TODO: input number of articles, allocate a dynamic array for all articles 
    		// call print, maxPrice and getCapital
    	setlocale(LC_ALL, "de");
    	srand(time(0));
    	int n;
    	cout << "Number of Articles: ";
    	cin >> n;
    	Article* commune = new Article[n];
    	printAll(n, commune);
    
    	return 0;
    }
    


  • Die Fehlermeldung sagt doch alles:
    price ist privat, da kannst Du nicht einfach von außen drauf zugreifen!?

    Edit:
    Eine Lösungsmöglichkeit steht doch schon in Deiner Klasse:

    public:
    	// TODO: signatures for Constructor, getPrice, getNumber, toString;
    

Anmelden zum Antworten