涉及类功能的麻烦

时间:2015-11-27 20:17:06

标签: c++ function class parameters

我无法通过main中的函数传递类参数。我的print函数语法正确。我遇到麻烦的功能是将f2传递给fracmult函数。如果任何人对代码有任何解释或想法,会导致分子和denom与f1和f2成倍增加,这将是非常好的。注意:这是我第一次使用类

class fraction

{
    private:
        int numerator;
        int denom;
        bool positive;

    public:
        void inputFrac();
        void printFrac();

        fraction fracMult(fraction b);
        fraction fracDiv(fraction b);
        fraction fracAdd(fraction b);
        fraction fracSub(fraction b);
};

 void fraction::printFrac()
{
    if (!positive)
    {
    cout << "-";
    }
    cout << numerator << " / " << denom;
}
void fraction::inputFrac()
{    
    cout<<"Please input the numerator ";
    cin>>numerator;
    cout<< "Please input the denominator ";
    cin>>denom; 
    cout<<"Is the fraction positive? (Y or N);
    cin>>positive;
}

 fraction fraction::fracMult(fraction b)
{

     numerator=b.numerator;
     denom=b.denom;
}
fraction fraction::fracAdd(fraction& b)
 {
     fraction temp; 
     temp.numerator=numerator + b.numerator;
     temp.denom=denom + b.denom;

     return temp;
 }
 fraction fraction::fracDiv(fraction& b)
 {
     fraction temp;
     temp.numerator = numerator * b.denom;
     temp.denom = denom * b.numerator;

     return temp;

 }
 fraction fraction::fracSub(fraction& b)
 {

    fraction temp;
     temp.numerator = numerator - b.numerator;
     temp.denom = denom - b.denom;

     return temp;   
 }

int main(int argc, char** argv) {

    fraction f1, f2, fresult;

    f1.inputFrac(); //input the first fraction
    f2.inputFrac(); //input the second fraction
    cout<<endl;
    f1.printFrac();
    cout<<endl;
    f2.printFrac();
    cout<<endl;

    cout << "The result of a * b is: ";

    fresult = f1.fracMult(f2); // calculate a * b
    fresult.printFrac(); // print out the result

3 个答案:

答案 0 :(得分:0)

在fraction :: fracMult中你需要这个:

fraction fraction::fracMult( const fraction& b)
{
    if(positive==b.positive) positive=false;

    numerator *=b.numerator;
    denom *=b.denom;

    return *this;
}

或者你可以:

fraction fraction::fracMult(const fraction&b) const
{
  fraction output;
  output.numerator = this->numerator *b.numerator;
  output.denom = this->denom *b.denom ;
  output.positive=false;
  if(this->positive==b.positive) output.positive=true;

  return output;

}

答案 1 :(得分:0)

编译代码时,是否有警告指出您正在退出一个预期返回值的函数,但是没有返回值?

fraction fraction::fracMult(fraction b)
{
     numerator=b.numerator;
     denom=b.denom;
}

您已定义了一个返回fraction的函数,但您未能返回值。因此,该函数产生未定义的行为

修复是返回分数对象。

fraction fraction::fracMult(fraction b)
{
     numerator=b.numerator;
     denom=b.denom;
     return *this;
}

然而,这似乎违反直觉。如果您要将对象乘以一个值并返回一个新的fraction,则必须创建this的副本,然后返回该副本。你不想改变目前的分数。

fraction fraction::fracMult(fraction b)
{
     fraction temp(*this); 
     temp.numerator=b.numerator;
     temp.denom=b.denom;
     return temp;
}

如果您确实想要更改当前分数,则返回对*this的引用,而不是新对象:

fraction& fraction::fracMult(fraction b)
{
     numerator=b.numerator;
     denom=b.denom;
     return *this;
}

但这就是operator *operator *=的原因。您需要决定您的函数应该执行哪些操作。更改当前对象,或使用当前对象的数据进行计算并返回新对象。

答案 2 :(得分:0)

class fraction
{
private:
    int numerator;
    int denom;
char choice;

public:
void inputFrac();
void printFrac();

fraction fracMult(fraction b);
fraction fracDiv(fraction b);
fraction fracAdd(fraction b);
fraction fracSub(fraction b);
}; 

void fraction::printFrac()
{
if (choice=='y'||choice=='Y')
{
cout << "-";
}
cout << numerator << " / " << denom;
}
void fraction::inputFrac()
{
 cout<<"Please input the numerator ";
cin>>numerator;
cout<< "Please input the denominator ";
cin>>denom;
cout<<"Is the fraction positive? (y or n)";
cin>>choice;
}

fraction fraction::fracMult(fraction b)
{

 fraction temp;
temp.numerator=numerator*b.numerator;
 temp.denom=denom*b.denom;
 return temp;
 }
 fraction fraction::fracAdd(fraction b)
 {
 fraction temp;
 temp.numerator=numerator + b.numerator;
 temp.denom=denom + b.denom;

 return temp;
 }
 fraction fraction::fracDiv(fraction b)
 {
 fraction temp;
 temp.numerator = numerator * b.denom;
 temp.denom = denom * b.numerator;

 return temp;

  }
 fraction fraction::fracSub(fraction b)
 {

 fraction temp;
 temp.numerator = numerator - b.numerator;
 temp.denom = denom - b.denom;

 return temp;
  }

 int main(int argc, char** argv) {

 fraction f1, f2, fresult;

f1.inputFrac(); //input the first fraction
f2.inputFrac(); //input the second fraction
cout<<endl;
f1.printFrac();
cout<<endl;
f2.printFrac();
cout<<endl;

cout << "The result of a * b is: ";

fresult = f1.fracMult(f2); // calculate a * b
fresult.printFrac(); // print out the result
}