2013-09-22 6 views
2

friend 생성자의 변수로 stringint을 사용하는 프로그램을 만들고 있습니다. 대부분의 프로그램을 끝내지 만, friend s 및 C++ 생성자에 익숙하지 않으므로이 매개 변수를 사용하고 및 ostream에 대한 작업을 시작하려면 main()에 대한 호출을 구현하는 방법을 모르겠습니다. 사용자에게 값 을 입력하고 인쇄를 시작하도록 요청하십시오.main()에서 생성자를 호출하는 방법?

누군가 나를 올바른 방향으로 유도 할 수 있습니까?

이 내 현재의 생성자는 다음과 같습니다

지금은 내 istream 기능을 인쇄 여기에 내 개체를 구현하고 입력에 대한 내 질문을 시작하려고 해요
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; 
} 

:

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

그래서 여기 혼란스러워하지 마라. 나의 주된 학급은 다음과 같다 :

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

    }; 

개체를 추가하고 과부하로 내 프로그램> 내 솔루션들을 :

개체를 추가하고 오버로드하여 내 프로그램 내 솔루션 :

외에도
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"); 
    } 
+1

모든 C++ 서적의 첫 번째 장을 읽으십시오. 귀하의 개체 사용은 완전히 잘못되었습니다. – drescherjm

+0

다른 여러 오류가 있습니다. – drescherjm

+0

이것은 내 완전한 코드가 아니며, 단지 변수를 초기화하는 주요 생성자를 보여 주며, 코드를 자르기 위해 main()으로 뛰어 들었다. – yamicaitsith

답변

4

많은에서 사용자가 제공 한 코드의 다른 구문 오류는 자동 저장 기간으로 다음과 같아야합니다.

,
class A { 
public: 
    A() { }   // <-- default constructor 
    A(int i) { }  // <-- constructor taking int 

    void foo() { } // <-- member function ("method") 
    static s() { } // <-- static function 
}; 

어딘가에는 :

더 많은 코드를 서면으로 계속하기 전에
A a;   // <-- constructs object using the default constructor 
A a2 = A(); // <-- equivalent to the previous one (copy initialization) 
A a3(a2);  // <-- copy constructor ~ constructs a3 using the a2 object 
A a4(7);  // <-- constructs object using the constructor taking int 

// now when you have some objects, you can call member functions 
// (i.e. invoke the behavior they provide) 
a.foo(); 

// static functions are not dependent on objects: 
A::s();  // <-- calls static function defined within namespace of class A 

, 몇 가지 괜찮은 책을 읽고 약간의 시간을 보내는 것이 좋습니다.
읽을 책에 대해 잘 모를 경우 여기에서 하나를 찾을 수 있습니다. The Definitive C++ Book Guide and List

+0

실제로 im 읽기 : Absolute C++ (5th Edition) 코스 북. – yamicaitsith

+0

수업에 대해 가르치는 모든 장에 집중하십시오. – drescherjm

+0

메신저 나 거기 그냥 내 main() 구현하는 방법을 해결 havent하지만, 각 힌트를주는 모두를 주셔서 감사합니다. – yamicaitsith

관련 문제