Comment appeler les constructeurs dans main()?

Je suis en train de travailler sur un programme qui prend un string et un int en tant que variables pour mon friend constructeurs. Je n'ai plus de mon programme fait, mais depuis que je suis nouveau à ce sujet de friends et les constructeurs avec le C++, je ne sais pas comment la mettre l'appel sur mon main() à l'utilisation de ces paramètres et de commencer à travailler sur mon istream et ostream de demander à l'utilisateur à l'entrée
les valeurs et commencer l'impression.

Quelqu'un pourrait-il svp me guider dans le bon sens?

Ce sont mes constructeurs:

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

Maintenant, je suis en train de mettre en œuvre ici mes objets et de commencer à faire de mes questions pour l'entrée et ensuite l'imprimer avec mon istream fonction:

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

de sorte que vous ne soyez pas confondu ici est ma classe principale:

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

    };

> ma solution à mon programme en ajoutant des objets et de surcharge
eux:

ma solution à mon programme en ajoutant des objets et de les surcharger:

 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");
    }
  • Merci de lire le premier chapitre de c++ livre. Votre utilisation des objets est complètement faux.
  • Il y a aussi plusieurs autres erreurs.
  • ce n'est pas mon code complet, im juste montrer mes principaux constructeurs avec les variables im lors de l'initialisation,et j'ai sauté de ma main(), il suffit de découper le code.
  • Je me rends compte que cependant à partir de votre code, il semble que vous n'avez même pas commencer à comprendre c++ à tous. Il y a beaucoup trop d'erreurs.
  • LihOs réponse montre plusieurs façons de construire des objets dans la pile. Un code similaire serait de travailler avec votre classe.
  • Astuce#2: Supprimer chaque ligne que vous avez: flavor = "null"; Ce n'est certainement pas ce que vous voulez.

InformationsquelleAutor yamicaitsith | 2013-09-22