题目 1、类的定义与基本操作
class Fraction {
//数据成员,访问控制属性默认是私有
int m_numerator = 0; // 分子默认为0; C++11
int m_denominator = 1; //分母默认为1;
public://公有成员函数
Fraction(int above = 0, int below = 1) :
m_numerator(above), m_denominator(below) {
cout << “Constructor called” << endl;
}
Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator),
m_denominator(rhs.m_denominator) {
cout << “Copy constructor called” << endl;
}
};
Fraction divide1(const Fraction& divident, const Fraction& divisor) {
return Fraction(divident.getnumerator() * divisor.getdenominator(),
divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
Fraction result(divident.getnumerator() * divisor.getdenominator(),
divident.getdenominator() * divisor.getnumerator());
return result;
}
说明执行下列语句后,分别执行的什么操作,会输出什么?
Fraction a;
执行默认构造函数,输出Constructor called
Fraction b(a);
执行复制构造函数,输出Copy constructor called
Fraction c = Fraction(3, 2);
执行参数构造函数,输出Constructor called
Fraction d1(2, 3), d2(4, 5);
执行两次参数构造函数,输出
Constructor called
Constructor called
Fraction e1 = divide1(d1, d2);
divide1返回了Fraction(int,int),定义e1执行参数构造函数,输出Constructor called
Fraction e2 = divide2(d1, d2);
divide2的两个形参以复制构造的方式创建,分别复制实参d1和d2,返回的临时对象也以复制构造的方式产生,再用这个临时对象复制构造对象e2。输出
Copy constructor called
Copy constructor called
Copy constructor called
Copy constructor called
在上述类的定义基础上,完善下列操作:
- 显示定义析构函数;
- 获取分数的分子;
- 获取分数的分母;
- 实现分数的约分;
- 实现对两个分数对象进行通分;
- 使用 operator/操作符重载实现两个分数的除法运算。
#include<iostream>
using namespace std;
class Fraction {
int m_numerator = 0;
int m_denominator = 1;
int gcd(int a, int b) { //获取最大公约数
return b == 0 ? a : gcd(b, a % b);
}
public:
Fraction(int above = 0, int below = 1) : //含参构造函数
m_numerator(above), m_denominator(below) {
cout << "Constructor called" << endl;
}
Fraction(const Fraction& rhs) :m_numerator(rhs.m_numerator),
m_denominator(rhs.m_denominator) { //复制构造函数
cout << "Copy constructor called" << endl;
}
~Fraction() { //显式定义析构函数
cout << "Destructor called" << endl;
}
int getnumerator() const { //获取分子
return m_numerator;
}
int getdenominator() const { //获取分母
return m_denominator;
}
void simplify() { //约分
int n = gcd(m_numerator, m_denominator);
m_numerator /= n; m_denominator /= n;
}
};
Fraction operator/(Fraction& f1, Fraction& f2) { //重载/号 两分数相除
Fraction temp(f1.getnumerator() * f2.getdenominator(), f1.getdenominator() * f2.getnumerator());
return temp;
}
Fraction divide1(const Fraction& divident, const Fraction& divisor) {
return Fraction(divident.getnumerator() * divisor.getdenominator(), \
divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
Fraction result(divident.getnumerator() * divisor.getdenominator(), \
divident.getdenominator() * divisor.getnumerator());
return result;
}
Fraction tongfen(const Fraction& a, const Fraction& b) { //通分
int denominator = a.getdenominator() * b.getdenominator();
int numerator = a.getnumerator() * b.getdenominator() + a.getdenominator() * b.getnumerator();
Fraction result(numerator, denominator);
return result;
}