#include class Rational
{
private:
int chisl, znam;
public:
Rational() { chisl = 0; znam = 0; }
Rational(int a, int b) { chisl = a; znam = b; }
void Mult(Rational x);
Rational operator*(const Rational& x);
void Write();
};
void Rational::Mult(Rational x)
{
chisl = chisl * x.chisl;
znam = znam * x.znam;
}
Rational Rational:: operator*(const Rational& x)
{
Rational result;
result.chisl = chisl * x.chisl;
result.znam = znam * x.znam;
return result;
}
void Rational::Write()
{
std::cout << chisl << "|" << znam << std::endl;
}
int main()
{
Rational n1(2, 3), n2(3, 2);
Rational n3 = n1 * n2;
n3.Write();
return 0;
}
В этом коде нужно передать результат операции умножения по значению.
Відповідь:
С++ code:
#include <iostream>
class Rational{
private:
int chisl, znam;
public:
Rational();
Rational(int, int);
Rational operator*(const Rational&);
friend std::ostream& operator << (std::ostream& out , const Rational& obj){
out << obj.chisl << "|" << obj.znam << std::endl;
}
};
Rational::Rational(){
chisl = 0;
znam = 0;
}
Rational::Rational(int chisl,int znam){
this->chisl = chisl;
this->znam = znam;
}
Rational Rational:: operator*(const Rational& obj){
Rational result;
result.chisl = chisl * obj.chisl;
result.znam = znam * obj.znam;
return result;
}
int main(){
Rational n1(2, 3), n2(3, 2);
Rational n3 = n1 * n2;
std::cout << n3 << std::endl;
return 0;
}