2688 - EE2310_Lec_12 Scoreboard

Time

2022/12/08 08:10:00 2022/12/08 10:30:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
13746 EE2310_Lec_12_1
13747 EE2310_Lec_12_2

13746 - EE2310_Lec_12_1   

Description

Fraction Class w/ Friend Functions

In Lab 11, we practiced the Fraction class. However, that version is far from perfect. In Lab 11's main function, when we performed arithmetic operations we did things like ans.add(num2); This is ugly and asymmetric. It contradicts to our basic understanding of addition. We want to use add(ans, num) instead to reflect the nature of addition as well as other arithmetic operations. The best way to achieve this goal is to use friend functions.

What you need to do

You need to re-implement the four arithmetic functions in fraction.cpp using friend functions.

  1. Declare these 4 arithmetic operations as friend functions in the class definition (from Lab 11's header file).

  2. Implement these friend functions in "fraction.cpp". Note that they are not member functions, so they don't belong to the Fraction class. There is no need to add the class name and the scope resolution operator Fraction::

  3. Main function is given. DO NOT CHANGE MAIN, otherwise your work will not be regarded as correct even if you pass all test cases.  You may get PENALTY POINTS for changing main().

  4. You can use all other member functions from Lab 11. Submit everything including main().

// DO NOT CHANGE main() !!!
int main() {
    Fraction num1, num2, ans;
    char oper;
    num1.input();
    cin >> oper;
    num2.input();

    switch (oper) {
      case '+':
        ans = add(num1, num2); break;
      case '-':
        ans = sub(num1, num2); break;
      case '*':
        ans = mul(num1, num2); break;
      case '/':
        ans = div(num1, num2); break;
    }

    // display result
    cout << '(';
    num1.display();
    cout << ") "  << oper << " (";
    num2.display();
    cout << ") = ";
    ans.display();
    cout << endl;

    return 0;
}

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss




13747 - EE2310_Lec_12_2   

Description

Operator Overloading as Friend Functions

In Lec 12-1, we practiced the Fraction class with friend functions. The function calls are already much better like add(ans, num). We want to further increase the readability of main(). We want to apply operator overloading so that expressions like num1 + num2 will work. There are two ways to do that. We can either overload the arithmetic operators either as member functions or friend functions. In this project, we are to overload them as friend functions. In Lab 12-3, you are to practice the other one.

What you need to do

  1. Re-write add(), sub(), mul(), and div() as operators using friend functions in in the class definition (in "fraction.h")

  2. Implement these operators in "fraction.cpp". Note that they are friend functions, so they ** do not belong to the Fraction class**. The class name and the scope resolution operator Fraction:: are not necessary.

  3. Main function is given. DO NOT CHANGE MAIN, otherwise your work will not be regarded as correct even if you pass all test cases.  You may get PENALTY POINTS for changing main().

  4. You can use all other member functions from Lab 11. Submit everything including main().

// DO NOT CHANGE main() !!!
int main() {
    Fraction num1, num2, ans;
    char oper;
    num1.input();
    cin >> oper;
    num2.input();

    switch (oper) {
      case '+':
        ans = num1 + num2; break;
      case '-':
        ans = num1 - num2; break;
      case '*':
        ans = num1 * num2; break;
      case '/':
        ans = num1 / num2; break;
    }

    // display result
    cout << '(';
    num1.display();
    cout << ") "  << oper << " (";
    num2.display();
    cout << ") = ";
    ans.display();
    cout << endl;

    return 0;
}

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss