yam*_*ith 2 c++ constructor call friend istream
我正在为我的构造函数使用a string和intas作为变量的程序friend。我有我的大多数项目的完成,但由于我是新来这个话题的friendS和构造与C ++,我不知道如何实现我的电话main()使用这些参数,并开始对我的工作istream,并ostream询问用户输入值并开始打印。
有人可以指导我正确的方法吗?
这些是我当前的构造函数:
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;
}
Run Code Online (Sandbox Code Playgroud)
现在,我尝试在此处实现我的对象,并开始提出要输入的问题,然后使用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;
}
Run Code Online (Sandbox Code Playgroud)
这样您就不会感到困惑,这是我的主要课程:
#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;
};
Run Code Online (Sandbox Code Playgroud)
>通过添加对象并重载它们来解决程序:
我的程序解决方案,方法是添加对象并重载它们:
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");
}
Run Code Online (Sandbox Code Playgroud)
除了您提供的代码中的许多其他语法错误之外,构造具有自动存储持续时间的对象应如下所示:
class A {
public:
A() { } // <-- default constructor
A(int i) { } // <-- constructor taking int
void foo() { } // <-- member function ("method")
static s() { } // <-- static function
};
Run Code Online (Sandbox Code Playgroud)
在某处:
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
Run Code Online (Sandbox Code Playgroud)
在继续编写更多代码之前,请考虑花一些时间阅读一些体面的书。
如果您不确定要读什么书,可以在这里找到一本:《权威C ++书籍指南和清单》
| 归档时间: |
|
| 查看次数: |
19164 次 |
| 最近记录: |