Wie rufen Konstruktoren in main()?

Ich arbeite an einem Programm, das dauert ein string und ein int als Variablen für meine friend Konstruktoren. Ich habe die meisten von meinem Programm gemacht, aber da bin ich neu in diesem Thema friends und Konstruktoren in C++, ich weiß nicht, wie die Umsetzung der Anruf auf mein main(), diese Parameter zu verwenden und anfangen zu arbeiten auf meinem istream und ostream Fragen der Nutzer zur Eingabe
die Werte und den Druck starten.

Könnte jemand bitte führe mich den richtigen Weg?

Diese sind meine aktuellen Konstruktoren:

jellyBeanFlavors::jellyBeanFlavors()
{
    flavor = "null";
    jellyBeans = 0;
}

jellyBeanFlavors::jellyBeanFlavors(int newJellyBeans)
{
    flavor = "null";
    jellyBeans = newJellyBeans;
}

jellyBeanFlavors::jellyBeanFlavors(string newFlavor, int newjellyBeans)
{
    flavor = newFlavor;
    jellyBeans = newjellyBeans;
}

void jellyBeanFlavors::output()
{
    cout << flavor << " " << jellyBeans << endl;
}

Nun bin ich versucht zu implementieren, hier meine Objekte und beginnen, meine Fragen für die Eingabe und drucken Sie dann mit meinem istream Funktion:

int main ()

    {
        jellyBeanFlavors::jellyBeanFlavors(string newFlavor, int newjellyBeans);

        jellyBeanFlavors();
        jellyBeanFlavors myFirstFlavor = jellyBeanFlavors.the;
        jellyBeanFlavors mySecondFlavor;
        jellyBeanFlavors total = 0;

        cout << "Your first flavor  is: "<< myFirstFlavor. << endl;
        cin >> myFirstFlavor;
        cout << "Your second flavor is: "<< mySecondFlavor << endl;
        cin >> mySecondFlavor;
        cout << "The expected result of adding" << " the first flavor loaded with" << mySecondFlavor <<" in the quantity with jellybean2 loaded with 6 in the quantity is: ";
        cout << "a jellybean with a quantity of 11.
        cout << "the result of adding jellybean1 and jellybean two is: " << jellybean1 + jellybean2 << endl;


        //this isnt implemented right but i need advice please on how to call my objects from my main class.


        system("pause");

        return 0;
    }

so dass Sie nicht bekommen, verwirrt, hier ist meine main-Klasse:

    #include <iostream>
    #include <string>
    using namespace std;

    class jellyBeanFlavors
    {
        friend jellyBeanFlavors operator + (jellyBeanFlavors theFirstJellyBean, jellyBeanFlavors theSecondJellyBean);//sums
        friend jellyBeanFlavors operator - (jellyBeanFlavors theFirstJellyBean, jellyBeanFlavors theSecondJellyBean);//(binary) substracts
        friend jellyBeanFlavors operator - (jellyBeanFlavors theFirstJellyBean);//(unary) checks negatives
        friend jellyBeanFlavors operator * (jellyBeanFlavors theFirstJellyBean,jellyBeanFlavors theSecondJellyBean);//multiplies
        friend jellyBeanFlavors operator / (jellyBeanFlavors theFirstJellyBean,jellyBeanFlavors theSecondJellyBean);//divides
        friend jellyBeanFlavors operator == (jellyBeanFlavors theFirstJellyBean,jellyBeanFlavors theSecondJellyBean);//compares
        friend jellyBeanFlavors operator < (jellyBeanFlavors theFirstJellyBean,jellyBeanFlavors theSecondJellyBean);//less than
        friend jellyBeanFlavors operator > (jellyBeanFlavors theFirstJellyBean,jellyBeanFlavors theSecondJellyBean);//greater than
        friend ostream& operator << (ostream& outputStream, const jellyBeanFlavors& jellyBeanOutput);//output
        friend istream& operator >> (istream& inputStream, jellyBeanFlavors& jellyBeanInput);//input 

    public:
        jellyBeanFlavors();
        jellyBeanFlavors(int);
        jellyBeanFlavors(string, int);
        void output();

    private:
        string flavor;
        int jellyBeans;

    };

> meine Lösung zu meinem Programm hinzufügen von Objekten und überlastung
Sie:

meine Lösung zu meinem Programm hinzufügen von Objekten und überladen Sie:

 int main ()
    {   
        system("cls");
        char op;
        jellyBeanFlavors obj1,obj2,obj3;
        do{
            cin>>obj1;  //flavor
            cin>>obj2;  //amount 

            system("cls");
            cout<<"JELLYBEANS:"<<endl;

            obj3=obj1+obj2;
            cout<<"\n The Sum is : ";
            obj3.output();

            obj3=obj1-obj2;
            cout<<"\n The Substraction is : ";
            obj3.output();

            obj3=obj1*obj2;
            cout<<"\n The Multiplication is: ";
            obj3.output();

            obj3=obj1/obj2;
            cout<<"\n The Divide operation is : ";
            obj3.output();

            cout<<"\n"; 
            obj3 = obj1==obj2;
            obj3.output();
            cout<<"\n";

            obj3 = obj1>obj2;
            obj3.output();
            cout<<"\n";

            obj3 = obj1<obj2;
            obj3.output();
            cout<<"\n";

            obj3 = -obj1;
            obj3.output();
            cout<<"\n";

            obj3 = -obj2;
            obj3.output();
            cout<<"\n";

            cout<<"\n\nPress A/a and Enter to continue or 0 to exit"<<endl;
            cin>>op;
            if(op = 0)
            {
                exit(0);
            }
        }while(op =='a'|| op=='A');


        system("pause");
    }
Bitte Lesen Sie das erste Kapitel von c++ - Buch. Ihre Nutzung der Objekte ist völlig falsch.
Es gibt auch verschiedene andere Fehler.
das ist nicht mein vollständiger code, ich bin nur mit meinen wichtigsten Konstruktoren mit den Variablen im initialisieren,und ich sprang auf meine main(), nur schneiden Sie den code.
Ich merke, dass jedoch aus dem code scheint es Sie nicht sogar anfangen zu verstehen, c++ überhaupt. Da gibt es zu viele Fehler.
LihOs Antwort zeigt verschiedene Möglichkeiten der Konstruktion von Objekten im Stapel. Ähnlich wie der code funktionieren würde mit Ihrer Klasse.

InformationsquelleAutor yamicaitsith | 2013-09-22

Schreibe einen Kommentar