ㄚ輝 發問時間: 電腦與網際網路程式設計 · 1 0 年前

C++! Operator Overloading

You are to create a program to process a savings account class.

This class needs the following data members:

firstName (string)

lastName (string)

savingsBalance

annualInterestRate

accountNumber

The class should contain the following member functions:

default constructor – set names to empty string and balance to zero

Constructor with names and a beginning balance

Note: all constructors should initialize the annual rate to 5.0%

setName

getName

setBalance

getBalance

setInterestRate

getInterestRate

setAccountNumber

getAccountNumber

calculateNewBalance – (monthly rate = annualInterestRate / 12)

destructor – display a message saying object no longer exists

In addition your class should contain the following operator overload functions (either as member functions or friend functions, which ever is appropriate).

1.Overload the stream extraction operator (>>) to allow for reading a new name, balance, account number and rate from the keyboard

2.Overload the stream insertions operator (<<) to display the name, balance, account number and rate on the screen with appropriate labels

3.Overload the += operator to allow an increase to a saver’s balance (saver1 += 1000.00)

4.Overload the -= (minus equal) operator to decrease a saver’s balance (saver1 -= 1000.00)

-------------------------------------------------------------------------------------------

請問一下operator overload functions 那一邊應該要怎麼寫呢?

我知道怎麼setup class 跟其他functions, 可是對operator overloading 還是半知半解

1跟2好像要用friend function, 3跟4用member function.

順便問一下destructor是這樣寫嗎?

Account ::~Account()

{ cout << "Object no longer exists \n";

}

我剛開始學C++, 高手幫我解答一下ㄋ~ 感恩

已更新項目:

如果我設定set function 給Name, Balance, Interest, Account Number負責檢查user是不是輸入正確的數據, 請問要怎麼做?

void Account::setFirst_Name(string FN)

{

if(isalpha(FN))

First_Name = FN;

else

{

cout << "Incorrect First Name"<<endl;

First_Name = "";

}

}

請問要怎麼把array加進去然後用loop檢查全部字元?

2 個已更新項目:

Write a driver program to test the class. Perform the following tasks in this order:

Declare at least three objects:

Saver1: Bob Jones with a balance of $2500

Saver2: no names or balance

Saver3: no names or balance

3 個已更新項目:

Set Bob Jones’ account number to 111

Enter (read from keyboard) saver2’s data: name:Cramer, balance: $5,000, and the account number: 222

Enter saver3’s data: name: Art Lopez, balance: $8,00, and the account #: 333

4 個已更新項目:

Calculate the new monthly balance for each customer(new balance = old balance + monthly interest earned)

Display all the data for all of the objects

5 個已更新項目:

Display all the data for all of the objects

Set the annual interest rate to 10.0%

Increase saver1’s balance by 1000.00

Decrease saver2’s balance by 500.00

Calculate the new monthly balance for each customer again

Display all the data for all of the objects

6 個已更新項目:

I am confuse because the New Balance function in the class is monthly rate = annualInterestRate / 12,

How comes here is different? (new balance = old balance + monthly interest earned)

Do I have to use the first one to calculate the monthly interest first?

7 個已更新項目:

我現在只差這裡

Set the annual interest rate to 10.0%

Increase saver1’s balance by 1000.00

Decrease saver2’s balance by 500.00

Calculate the new monthly balance for each customer again

要怎ㄇ抓之前的new balance, 然後從new balance再開始計算

annual interest rate跟加1000 減500?

1 個解答

評分
  • 最佳解答

    不一定要用 member function

    #include <iostream>

    #include <string>

    using namespace std;

    class Account

    {

    private:

    string firstName;

    string lastName;

    long savingsBalance;

    double annualInterestRate;

    long accountNumber;

    public:

    Account() {

    firstName = "";

    lastName = "";

    savingsBalance = 0;

    accountNumber = 1;

    annualInterestRate = 0.05;

    }

    ~Account() {

    cout << "Object no longer exists \n";

    }

    friend istream& operator >> (istream& stream, Account& in);

    friend ostream& operator << (ostream& stream, Account& out);

    friend void operator += (Account& s, long l);

    void operator -= (long l);

    };

    istream& operator >> (istream& stream, Account& in)

    {

    cout << "input firstName, lastName, savingsBalance, accountNumber, annualInterestRate" << endl;

    stream >> in.firstName >> in.lastName >> in.savingsBalance >> in.accountNumber >> in.annualInterestRate;

    cout << endl;

    return stream;

    }

    ostream& operator << (ostream& stream, Account& out)

    {

    stream << "Name: " << out.firstName << " " << out.lastName << endl;

    stream << "Balance: " << out.savingsBalance << endl;

    stream << "accountNumber: " << out.accountNumber << endl;

    stream << "annualInterestRate: " << out.annualInterestRate << endl;

    return stream;

    }

    void operator += (Account& s, long l)

    {

    s.savingsBalance += l;

    }

    void Account::operator -= (long l)

    {

    savingsBalance -= l;

    }

    int main()

    {

    {

    Account f;

    cout << f;

    cin >> f;

    cout << f << endl;

    f += 3000;

    cout << f << endl;

    f -= 999;

    cout << f << endl;

    }

    system("PAUSE");

    return EXIT_SUCCESS;

    }

    Name:

    Balance: 0

    accountNumber: 1

    annualInterestRate: 0.05

    input firstName, lastName, savingsBalance, accountNumber, annualInterestRate

    Jack Li 100000 45 2.35

    Name: Jack Li

    Balance: 100000

    accountNumber: 45

    annualInterestRate: 2.35

    Name: Jack Li

    Balance: 103000

    accountNumber: 45

    annualInterestRate: 2.35

    Name: Jack Li

    Balance: 102001

    accountNumber: 45

    annualInterestRate: 2.35

    Object no longer exists

    請按任意鍵繼續 . . .

    2007-10-06 13:41:20 補充:

    字串本就是字元陣列

    void Account::setFirst_Name(string FN)

    {

    bool noerror = true;

    for (int i = 0; i < FN.length(); i++) {

    if(!isalpha(FN[i]))

    {

    noerror = false;

    break;

    }

    }

    if (noerror)

    First_Name = FN;

    else

    {

    cout << "Incorrect First Name"<<endl;

    First_Name = "";

    }

    }

    2007-10-12 12:44:50 補充:

    說實在的,你這樣突然冒出其他的問題,又敘述不清楚,我很難明白你到底要問什麼具體的問題?

還有問題?馬上發問,尋求解答。