C++ learning-classes and objects

Requirements:
1. Create a score class. The members of the fraction category include numerator and denominator, and operations include reduction, division, addition, subtraction, multiplication, division, copy assignment, reciprocal calculation, comparison, display, and input;
2. 2. Add (+), subtract (-), multiply (*), divide (/), compare (>, >=, <, <=), copy assignment (=) operations are implemented by operator overloading;
3. Function requirements: able to handle mixed operations of int, double, float data and fractional objects;
4.The test case covers all functions (including the calculations between the scores and the calculations between the scores and int, double, and float data), and it needs to include the running result graph, the test purpose description, and the running result analysis;
5. The code is clean and standardized
6. Attached with the running result diagram, it explains and analyzes
some related issues of test cases, test purposes, and running results :
1. Briefly describe the difference between member functions and friend functions, and analyze which functions need to be implemented by member functions and which functions need UF Meta-function realization;
①Member function is a part of class definition and is called by specific object. Member functions can implicitly access the members of the calling object without using member operators. Friend functions are not part of the class, so they are called direct function calls. Friend functions cannot implicitly access class members, but must use member operators for objects passed as parameters. As a non-member function of the class, and when the data to be accessed is a private member function of the object, it must be declared as a friend function of the class.
② When an operator overloaded function is used as a member function of a class, it is required that the left side of the operand must be an object, and the parameters of the function can be objects of the same kind or ordinary variables.
③Because the default copy assignment operator is a member function, the friend function cannot replace the member function, so the copy copy operator (=) overload must also be a member function. The four arithmetic operations of fractions and comparison operations can use either member functions or friend functions. However, when operations involving fractional objects and different data types are involved, friend functions must be used.
2. Briefly describe the difference between value passing and quoting, and analyze the parameter passing methods required by different functions, and briefly describe the reasons;
① The value call passes a copy of the variable, and the latter two calls pass the variable itself, so pass In the value call, only the copy of the variable is changed, not the variable itself. On the contrary, the latter two calls can be used when the variable itself needs to be changed.
②When the passed variable is large, the result of the call by value is low efficiency, because it will go through a copy process of type parameter to the actual parameter, and the latter two calls will be more efficient because the variable itself is passed.
③The difference between call by reference and call by pointer is that the reference parameter must point to an object, and the referent cannot be changed in the function. On the contrary, there is no such problem in the call by pointer. The pointer can be a null pointer that does not point to anything at first. , And can point to different objects in the function.
④The four arithmetic operations and comparison operations of the fraction type do not need to create temporary variables, which can improve efficiency; and the copy assignment operation is in the form of value transfer, which does not affect the actual parameters;
C++ code:

#include<iostream>
#include<cmath>
using namespace std;

class Fraction
{
    
    
    double nume;   //分子
    double deno;   //分母
public:
    Fraction(double nume = 0, double deno = 1);    //默认参数
    void reduction();                           //约分
    void tongfen(Fraction& , Fraction& );     //通分
    void display();                 //显示分数
    void input();                   //输入分数

    //重载四则运算符
    friend Fraction operator +(Fraction& , Fraction&);  //加法
    friend Fraction operator -(Fraction& , Fraction&);  //减法
    friend Fraction operator *(Fraction& , Fraction&);  //乘法
    friend Fraction operator /(Fraction& , Fraction&);  //除法
    Fraction operator =(Fraction);        //复制赋值

    //重载比较操作符
    friend bool operator > (Fraction&, Fraction&);    //大于
    friend bool operator >=(Fraction&, Fraction&);   //大于等于
    friend bool operator < (Fraction&, Fraction&);    //小于
    friend bool operator <=(Fraction&, Fraction&);   //小于等于
};
Fraction::Fraction(double nu, double de)
{
    
    
    nume = nu;
    deno = de;
}
//输入
void Fraction::input()  
{
    
    
    char ch;
    cin >> nume >> ch >> deno;
    while (deno == 0)
    {
    
    
        cout << "分母不能为0,请重新输入:" << endl;
        cin >> nume >> ch >> deno;
    }
    nume *= 1000; //处理小数,只能精确到后三位
    deno *= 1000;
}
//显示
void Fraction::display()    
{
    
    
    reduction();
    if (nume == 0)
        cout << '0' << endl;
    else if (deno == 1)
        cout << nume << endl;
    else
        cout << nume << '/' << deno << endl;
}
//约分
void Fraction::reduction()   
{
    
    
    int m, n, r;
    n = fabs(deno);
    m = fabs(nume);
    r = m % n;
    while (r)  // 求m,n的最大公约数
    {
    
    
        m = n;
        n = r;
        r = m % n;
    }
    deno /= n;     // 化简
    nume /= n;
    if (deno < 0)  // 将分母转化为正数
    {
    
    
        deno = -deno;
        nume = -nume;
    }
}
//通分
void Fraction::tongfen(Fraction& x,Fraction& y)
{
    
    
    x.deno = (x.deno * y.deno);
    x.nume = (x.nume * y.deno);
    reduction();
    cout << x.nume << '/' << x.deno << endl;
}

// 分数复制赋值
Fraction Fraction:: operator=(Fraction f)
{
    
    
    Fraction temp;
    temp.nume = f.nume;
    temp.deno = f.deno;
    nume = temp.nume;
    deno = temp.deno;
    return temp;
}
//加法
Fraction operator+(Fraction& x, Fraction& y)
{
    
    
    Fraction temp;
    temp.nume = x.nume * y.deno + x.deno * y.nume;
    temp.deno = x.deno * y.deno;
    return temp;
}
//减法
Fraction operator-(Fraction& x, Fraction& y)
{
    
    
    Fraction temp;
    temp.nume = x.nume * y.deno - x.deno * y.nume;
    temp.deno = x.deno * y.deno;
    return temp;
}
//乘法
Fraction operator*(Fraction& x, Fraction& y)
{
    
    
    Fraction temp;
    temp.nume = x.nume * y.nume;
    temp.deno = x.deno * y.deno;
    return temp;
}
//除法
Fraction operator/(Fraction& x, Fraction& y)
{
    
    
    Fraction temp;
    temp.nume = x.nume * y.deno;
    temp.deno = x.deno * y.nume;
    return temp;
}
//大于
bool operator >(Fraction& x, Fraction& y)
{
    
    
    if (x.nume * y.deno - x.deno * y.nume > 0)
        return true;
    else
        return false;
}
//大于等于
bool operator >=(Fraction& x, Fraction& y)
{
    
    
    if (x.nume * y.deno - x.deno * y.nume >= 0)
        return true;
    else
        return false;
}
//小于
bool operator <(Fraction& x, Fraction& y)
{
    
    
    if (x.nume * y.deno - x.deno * y.nume < 0)
        return true;
    else
        return false;
}
//小于等于
bool operator <=(Fraction& x, Fraction& y)
{
    
    
    if (x.nume * y.deno - x.deno * y.nume <= 0)
        return true;
    else
        return false;
}
//主函数
int main() {
    
    
    Fraction x, y, s;
    cout << "请输入x:" << endl;
    x.input();
    cout << "请输入y:" << endl;
    y.input();
    s = x + y;
    cout << "x+y="; s.reduction(); s.display();
    s = x - y;
    cout << "x-y="; s.display();
    s = x * y;
    cout << "x*y="; s.display();
    s = x / y;
    cout << "x/y="; s.display();
    s = x > y;
    cout << "x>y的结果true为1,false为0:"; s.display();
    s = x >= y;
    cout << "x>=y的结果true为1,false为0:"; s.display();
    s = x < y;
    cout << "x<y的结果true为1,false为0:"; s.display();
    s = x <= y;
    cout << "x<=y的结果true为1,false为0:"; s.display();
    cout << "x对y通分结果为:"; x.tongfen(x, y);
    s = x = y;
    cout << "x=y="; s.display();
}

Operating results:
Insert picture description here
current methods are inadequate and limitations:
1) Being lazy when processing double and float data, directly multiply by 1000, which is equivalent to converting to int type, so it can only be calculated to three decimal places; if it is improved, you can Enter the form of the string, and then convert it;
when it is displayed, the result of the comparison operation is not processed, only 0 and 1 are displayed, not converted to true or false, which is convenient for viewing and understanding.

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/113030741