【问题标题】:How to call constructors in main()? [closed]如何在 main() 中调用构造函数? [关闭]
【发布时间】:2013-09-27 04:39:00
【问题描述】:

我正在开发一个程序,该程序将stringint 作为我的friend 构造函数的变量。我已经完成了大部分程序,但是由于我对friends 和 C++ 构造函数这个主题不熟悉,我不知道如何在我的main() 上实现调用以使用这些参数并开始工作我的istreamostream 要求用户输入 值并开始打印。

有人可以指导我正确的方式吗?

这些是我当前的构造函数:

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

现在我正在尝试在这里实现我的对象并开始提出我的问题以供输入,然后使用我的istream 函数进行打印:

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

【问题讨论】:

  • 请阅读任何 c++ 书籍的第一章。您对对象的使用完全错误。
  • 还有其他几个错误。
  • 这不是我的完整代码,我只是展示了我的主要构造函数以及我正在初始化的变量,然后我跳到了我的 main(),只是剪切了代码。
  • 我意识到,但是从您的代码看来,您甚至根本没有开始理解 c++。错误太多了。
  • LihOs 的答案显示了在堆栈中构造对象的几种方法。类似的代码适用于您的课程。

标签: c++ constructor call friend istream


【解决方案1】:

除了您提供的代码中的许多其他语法错误之外,构造具有自动存储持续时间的对象应如下所示:

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

【讨论】:

  • 其实我正在阅读:Absolute C++ (5th Edition) 课程书。
  • 专注于任何关于课程的章节。
  • 我还没有解决如何实现我的 main() 方法,但感谢大家提供的每一个提示。
  • @yamicaitsith:“我有点在那里”听起来你跳过了实际上不应该跳过的章节。在开始重载运算符等之前,还要从更简单的东西开始。还要注意using namespace std; 不是启动头文件的好方法。
  • 事实上,在我和我们的导师一起的课程中,我们没有应用头文件,我们已经经历了类和结构,我不太了解在课堂上使用朋友的实现以及如何在我的 main() 中通过引用来调用它。
猜你喜欢
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2013-08-26
  • 2021-12-06
  • 1970-01-01
  • 2010-10-18
相关资源
最近更新 更多