Object oriented programming -- Inheritance

1, Purpose of the experiment:

  1. Master the concept of inheritance.
  2. Understand different inheritance types.

2, Experiment content:

On the basis of previous experiments,
Design a base class Account to express the commonness of all accounts, and then change the SavingsAccount class into its derived class. In addition, it also derives the class CreditAccount used to represent the credit Account.

(1) The base class retains the account number, balance, and static data members representing the total amount of the account, as well as the member functions for reading them and the show function for outputting account information. The functions of deposit, withdrawal and settlement interest are placed in each derived class.

(2) Credit account

class  CreditAccount: public Account
{
double  credit;   //Credit limit
double  rate;    //Daily interest rate of arrears
double  fee;    //Annual Fee 
double  getDebt() const;   //Obtain the amount owed
  public: 
	double  getAvailableCredit() const;  //Obtain available credit lines
};

(3) Divide the whole program into 5 or 6 files: date h account. H is the class definition header file, date cpp account. CPP is a class implementation file, 6 CPP is the main function file. (the accumulation function can be written separately as a class, and a header file of this class can be added.)

Tips:
First write in the header file:

#ifndef  _DATE_H_  
#define  _DATE_H_  

#endif (write at the end of header file)

requirement:
(1) Define class objects to test the correctness of the program

Two savings 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.
Define a credit account c0, with a credit limit of 10000 yuan, an annual fee of 50 yuan and a daily interest rate of 0.5%. Create an account on November 1, 2008, and then withdraw 2000 yuan and pay off the arrears on November 15, 2008 and December 1, 2008 respectively.
On January 1, 2009, the information (account number and balance) of s0, s1 and c0 and the total amount of all accounts were output respectively.

Source code:
main.cpp:

#include <iostream>
#include <string.h>
#include "date.h"
#include "account.h"
using namespace std;

double Account::total = 0;
int main()
{
    //Procedures related to account 1001
    Date d01(2008,11,5),d02(2008,12,5),d11(2008,11,25),d12(2008,12,20),d2(2009,1,1),dc1(2008,11,15),dc2(2008,12,1);
    cout<<"Information about account 1001:"<<endl;
    SavingsAccount s0(2008,11,1,1001,0,0.015);
    s0.deposit(d01,5000);
    s0.deposit(d02,5500);
    s0.settle(d2);
    d2.show();
    s0.show();
    cout<<endl;

    //Procedures related to account 1002
    cout<<"Information about account 1002:"<<endl;
    SavingsAccount s1(2008,11,1,1002,0,0.015);
    s1.deposit(d11,10000);
    s1.withdraw(d12,4000);
    s1.settle(d2);
    d2.show();
    s1.show();
    cout<<endl;

    //Procedures related to account 1003
    cout<<"Information about account 1003:"<<endl;
    CreditAccount c0(2008,11,1,1003,0,10000,0.0005,50);
    c0.deposit(dc1,2000);
    c0.withdraw(dc2,2000);
    c0.withdraw(dc2,-c0.getDebt(dc2));
    c0.getDebt(d2);
    d2.show();
	c0.show();
    cout<<endl;

    //Procedures for all accounts
	cout<<"Total amount of all accounts:"<<endl;
    d2.show();
    c0.getTotal();

    return 0;
}

account.h:

#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include "date.h"

class Account
{
protected:
    int id;
    double balance;
    static double total;
public:
    Account(int id,double balance);
    const int getId() {return id;}
    const double getBalance() {return balance;}
    static void getTotal();
    void show();
};
class SavingsAccount: public Account
{
    double rate;
    Date lastDate;
    double accumulation=0;
    const double accumulate(Date date);
public:
    SavingsAccount(int year,int month,int day,int id,double balance,double Rate);
    void deposit(Date date,double amount);
    void withdraw(Date date,double amount);
    void settle(Date date);
    const double getRate() {return rate;}
    const void show();
};
class CreditAccount:public Account
{
protected:
    double credit;
    double rate;
    double fee;
    Date lastDate;
public:
    CreditAccount(int year,int month,int day,int id,double balance,double credit,double rate,double fee);
    double getAvailableCredit() {return credit;}
    void deposit(Date date,double amount);
    void withdraw(Date date,double amount);
    double getDebt(Date date);
    void settle(Date date);
    const void show();
};
#endif // ACCOUNT_H_INCLUDED

account.cpp:

#include <iostream>
#include "account.h"
using namespace std;

Account::Account(int Id,double Balance)
{
    id=Id;
    balance=Balance;
}
void Account::show()
{
    cout<<"account number:"<<getId()<<"\t balance: "<<getBalance()<<"element"<<endl;
}
void Account::getTotal()
{
    cout<<"Total amount of all accounts: "<<total<<"element"<<endl;
}
SavingsAccount::SavingsAccount(int year,int month,int day,int id,double balance,double Rate):Account(id,balance),lastDate(year,month,day)
{
    rate=Rate;
    cout<<year<<"year"<<month<<"month"<<day<<"day\t";
    cout<<"ID Is:"<<id<<"Your account has been created successfully!"<<endl;
}
const double SavingsAccount::accumulate(Date date)
{
    return lastDate.distance(date)*balance;
}
void SavingsAccount::deposit(Date date,double amount)
{
    accumulation+=accumulate(date);
    balance+=amount;
    lastDate = date;
    total+=amount;
    cout<<date.getYear()<<"year"<<date.getMonth()<<"month"<<date.getDay()<<"day\t";
    cout<<"account"<<id<<"Deposit: "<<amount<<"element"<<"\t\t"<<"account"<<id<<"balance: "<<balance<<"element"<<endl;
}
void SavingsAccount::withdraw(Date date,double amount)
{
    accumulation+=accumulate(date);
    balance-=amount;
    lastDate = date;
    total-=amount;
    cout<<date.getYear()<<"year"<<date.getMonth()<<"month"<<date.getDay()<<"day\t";
    cout<<"account"<<id<<"take out: "<<amount<<"element"<<"\t\t"<<"account"<<id<<"balance: "<<balance<<"element"<<endl;
}
void SavingsAccount::settle(Date date)
{
    double settle = 0;
    accumulation+=accumulate(date);
    settle = accumulation/356*rate;
    cout<<date.getYear()<<"year"<<date.getMonth()<<"month"<<date.getDay()<<"day\t";
    cout<<"account"<<id<<"Settlement interest: "<<settle<<"element"<<endl;
    deposit(date,settle);
}
const void SavingsAccount::show()
{
    Account::show();
}
CreditAccount::CreditAccount(int year,int month,int day,int id,double balance,double Credit,double Rate,double Fee): Account(id,balance),lastDate(year,month,day)
{
    credit=Credit;
    fee=Fee;
    rate=Rate;
    cout<<year<<"year"<<month<<"month"<<day<<"day\t";
    cout<<"ID Is:"<<id<<"Your account has been created successfully!"<<endl;
}
double CreditAccount::getDebt(Date date)
{
    settle(date);
    return balance;
}
void CreditAccount::deposit(Date date,double amount)
{
    settle(date);
    if(credit<=0 || amount>credit)
    {
        date.show();
        cout<<"account"<<id<<"\t The credit card available limit is insufficient, please repay in time!"<<endl;
    }
    else
    {
        balance-=amount;
        credit-=amount;
        date.show();
        cout<<"account"<<id<<"\t Credit card used limit: "<<amount<<"element"<<"\t\t Remaining credit card limit: "<<credit<<"element"<<endl;
        total-=amount;
    }
}
void CreditAccount::withdraw(Date date,double amount)
{
    settle(date);
    balance+=amount;
    if((credit+amount)<=10000)
    {
        credit=credit+amount;
    }
    else
    {
        credit=10000;
    }
    total+=amount;
    date.show();
    cout<<"account"<<id<<"\t Credit card payment : "<<amount<<"element"<<"\t\t Remaining credit card limit: "<<credit<<"element"<<endl;
}
void CreditAccount::settle(Date date)
{
    double settle=0;
    if((date.getMonth()<lastDate.getMonth())||(date.getYear()>lastDate.getYear()))
    {
        for(int i=1;i<=date.getYear()-lastDate.getYear();i++)
        {
            settle+=fee;
        }
    }
    if(date.getDay()==1||date.getMonth()>lastDate.getMonth()||date.getYear()>lastDate.getYear())
    {
        if(date.getYear()==lastDate.getYear())
        {
            for(int j=1;j<=(date.getMonth()-lastDate.getMonth());j++)
            {
                for(int m=1;m<=lastDate.distance(date);m++)
                {
                    settle-=balance*rate;
                }
            }
        }
        else
        {
            for(int j=1;j<=(date.getMonth()-lastDate.getMonth())+12;j++)
            {
                for(int m=1;m<=lastDate.distance(date);m++)
                {
                    settle-=balance*rate;
                }
            }
        }
    }
    else
    {
        settle-=0;
    }
    balance-=settle;
    total-=settle;
    lastDate=date;
}
const void CreditAccount::show()
{
    Account::show();
}

date.h:

#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED

class  Date
{
    int  year, month, day;
    int  totalDays;
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;
    bool  isLeapYear(int year) const;
    int  distance(const Date& date) {return date.totalDays-totalDays;}
};
#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()<<"day\t";
}
bool  Date::isLeapYear(int year) const
{
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        return true;
    else
        return false;
}

Operation screenshot:

Learning and mastering
Master the concept of inheritance and understand different inheritance types.
Inheritance is a hierarchical model that connects classes. Inheritance enables the data structure and function of an existing class (base class or parent class) to be reused by another class (derived class or subclass). In a derived class, you only need to describe the data and operations not in its base class.
The further development and improvement of the previous experiments has added the credit card part and learned to use inheritance. There is no need to modify the existing class. On the basis of the existing class, we only need to obtain a new class by adding a small amount of code or modifying a small amount of code, which improves the efficiency of the improvement of the project program.

Keywords: C++

Added by smsharif on Sun, 16 Jan 2022 07:19:20 +0200