Object oriented programming -- Application of classes and objects 2

1, Purpose of the experiment:

  1. Master the concept, definition and usage of classes and objects.
  2. Master the usage of static data members and const modified member functions.
  3. Master the general structure of c + + program.

2, Experiment content:

Modify and complete the following contents on personal current savings account class SavingsAccount:

  • Add a static data member total in class SavingsAccount to record the total amount of each account, and add a corresponding static member function getTotal to access it.
  • Declare the member functions in the class SavingsAccount that do not need to change the object state as constant member functions, such as getBalance.
  • Add Date class Date
class  Date
{
int  year, month, day;
int  totalDays;   //The date is the day from January 1, A.D
  public: 
    Date(int year, int month, int day);	
	 int  getYear() const { return year; }
	 int  getMonth() const { return month; }
	 int  getDay() const { return day; }
	 void  show() const;			//Output current date
bool  isLeapYear() const;	//Determine whether the current year is a leap year
	 int  distance(const Date& date) const;//Calculates the number of days between the current date and the specified date
};
  • The int date in the class SavingsAccount should be changed to the object of the Date class.
  • The whole program is divided into five files: date h account. H is the class definition header file, date cpp account. CPP is a class implementation file, 5 CPP is the main function file.

Tips:

  • Calculation method of interest: the balance of each day in a year is accumulated and divided by the total number of days in a year to obtain an average daily balance, which is multiplied by the annual interest rate.
  • Calculation method of days difference between two dates: select a base date (such as January 1, the first year of A.D.), when calculating the days difference between two dates, first calculate the relative days between the two dates and the base date, and then subtract the two relative days.
  • The calculation method of the number of days relative to the base date (such as January 1, the first year of A.D.): (1) calculate the total number of days from the first year of A.D. to year y-1 of A.D. On average, there are 365 days a year, one more day in leap years, i.e. 365 * (y-1) plus the number of leap years between the first year of A.D. and y-1. (2) Add the number of days from January 1 of the current month to January 1 of the current year. (3) Add the number of days between the day of the current month and the first day of the current month.
  • You can put the number of days from January 1 to January 1 in an array. The element values of the array are: 0, 31, 59, 90120151181212243, 73304334365
  • First write in the two header files:
#ifndef  _DATE_H_                                  
#define  _DATE_H_                 
...                                              
#endif                                       

#ifndef  _ACCOUNT_H_  
#define  _ACCOUNT_H_
 ...    
#endif

requirement:
 complete the definition of the above member functions;
 define class objects and test the correctness of the program
Two accounts s0 and s1 are defined. The annual interest rate is 1.5%. Both accounts are created on November 1, 2008. Then s0 deposits 5000 yuan and 5500 yuan respectively on November 5, 2008 and December 5, 2008, and s1 deposits 10000 yuan and withdraws 4000 yuan respectively on November 25, 2008 and December 20, 2008. January 1, 2009 is the interest date of the bank. Output the information (account number and balance) of s0 and s1 accounts and the total amount of all accounts respectively.

Source code:

Main.cpp:
#include<iostream>
#include"date.h"
#include"account.h"
using namespace std;
double SavingsAccount::total = 0;
int main()
{
    SavingsAccount s0(1001,0.015,2008,11,1);
    SavingsAccount s1(1002,0.015,2008,11,1);
    cout<<endl;
    Date d01(2008,11,5),d02(2008,12,5),d11(2008,11,25),d12(2008,12,20),d2(2009,1,1);
    s0.deposit(d01,5000);
    s1.deposit(d11,10000);
    s0.deposit(d02,5500);
    s1.withdraw(d12,4000);
    cout<<endl;
    s0.settle(d2);
    cout<<endl;
    s1.settle(d2);
    cout<<endl;
    s0.show();
    s1.show();
    cout<<"Total balance of all accounts:"<<s0.getTotal()<<endl;
}
Account.h:
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include "date.h"
class  SavingsAccount
{
    int  Id;  //accounts
    double  balance;  //balance
    double  Rate;   //Annual interest rate
    Date  lastDate; //Date of last balance change
    double  accumulation; //Sum of daily accumulated balances
    double static total;
    double  accumulate(Date date);   //Gets the daily cumulative value of the deposit amount up to the specified date
public:
    SavingsAccount(int id, double rate,int year,int month,int day);  //Constructor
    void  deposit(Date date, double amount);  //Cash deposit, date is the date and amount is the amount
    void  withdraw(Date date, double amount);  //Withdraw cash
    void  settle(Date date); //For interest settlement, this function is called once on January 1 every year
    void  show();   //Output account information
    int getId() const {return Id;}
    double  getBalance () const { return balance;}
    double  getRate() const {return Rate;}
    double static getTotal();
};
#endif // ACCOUNT_H_INCLUDED
Account.cpp:
#include <iostream>
#include "account.h"
using namespace std;
double SavingsAccount::accumulate(Date date)
{
    return balance*lastDate.distance(date);
}
SavingsAccount::SavingsAccount(int id,double rate,int year,int month,int day) :lastDate(year,month,day)
{
    Id=id;
    Rate=rate;
    balance=0;
    cout<<year<<"year"<<month<<"month"<<day<<"Date:  \t";
    cout<<"ID by"<<Id<<"Account creation completed"<<endl;
}
void  SavingsAccount::deposit(Date date, double amount)  Cash deposit, date Is the date, amount Is amount
{
    accumulation += accumulate(date);
    balance+=amount;
    lastDate = date;
    total += amount;
    cout<<"ID by"<<Id<<"Deposit into your account:"<<amount<<" \t"<<Id<<" The account balance is:"<<balance<<endl;
}
void  SavingsAccount::withdraw(Date date, double amount) Withdraw cash
{
    accumulation += accumulate(date);
    if(balance>=amount)
        balance-=amount;
    else
        cout<<"Insufficient account balance"<<endl;
    lastDate=date;
    total -= amount;
    cout<<"ID by"<<Id<<"Account withdrawal:"<<amount<<" \t"<<Id<<" Balance of this account:"<<balance<<endl;
}
void  SavingsAccount::settle(Date date)   //For interest settlement, this function is called once on January 1 every year
{
    accumulation += accumulate(date);
    double settle = (accumulation/356)*Rate;
    cout<<Id<<"Settlement interest:"<<settle<<endl;
    deposit(date,settle);
}
void  SavingsAccount::show()    //Output account information
{
    cout<<"accounts:"<<Id<<endl<<"balance:"<<balance<<endl;
}
double SavingsAccount::getTotal()
{
    return total;
}
Date.h:
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
class  Date
{
    int  year, month, day;
    int  totalDays;   //The date is the day from January 1, A.D
public:
    Date(int year, int month, int day);
    int  getYear() const { return year; }
    int  getMonth() const { return month; }
    int  getDay() const { return day; }
    void  show() const;   //Output current date
    bool  isLeapYear(int year) const; //Determine whether the current year is a leap year
    int  distance(const Date& date) const;//Calculates the number of days between the current date and the specified date
};
#endif // DATE_H_INCLUDED
Date.cpp:
#include <iostream>
#include "date.h"
using namespace std;
Date::Date(int Year, int Month, int Day)
{
    year=Year;
    month=Month;
    day=Day;
    int Days=0;
    for(int i=0;i<year;i++)
    {
        if(isLeapYear(i)==true)
            Days=Days+366;
        else
            Days=Days+365;
    }
    switch(month){
    case 1:Days+=0;break;
    case 2:Days+=31;break;
    case 3:Days+=59;break;
    case 4:Days+=90;break;
    case 5:Days+=120;break;
    case 6:Days+=151;break;
    case 7:Days+=181;break;
    case 8:Days+=212;break;
    case 9:Days+=243;break;
    case 10:Days+=273;break;
    case 11:Days+=304;break;
    case 12:Days+=334;break;
    default:cout<<"month error!"<<endl;
    }
    totalDays=Days+day;
}
void  Date::show() const
{
    cout<<getYear()<<"year"<<getMonth()<<"month"<<getDay()<<"Date:  "<<endl;
} //Output current date
bool  Date::isLeapYear(int year) const
{
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        return true;
    else
        return false;
} //Determine whether the current year is a leap year
int  Date::distance(const Date& date) const
{
    cout<<date.year<<"year"<<date.month<<"month"<<date.day<<"Date:  "<<"\t";
    return date.totalDays-totalDays;
}    //Calculates the number of days between the current date and the specified date

Operation screenshot:

Learning and mastering
The concept, definition and usage of classes and objects, static data members and the usage of const modified member functions
The general structure of c + + program. A large project is produced by the gradual refinement of small programs.
Organize the coordinated calls between various functions to achieve the purpose of use, and transform some problems in life into corresponding implementation methods in C. for example, the problem of days in this experiment can be solved by using arrays, switch statements and other methods.
The interrelationship between multiple source files and header files also requires multiple debugging before they can work together.

Keywords: C++ Javascript

Added by utdfederation on Sat, 15 Jan 2022 13:49:15 +0200