面向对象程序设计第四章

简介: 面向对象程序设计第四章

存款利息问题

【问题描述】

利息存款问题

存款类型和利率分别如下:

活期 0.35% 一年 3.25% 两年3.75% 三年4.25% 五年4.75%

编写程序账户类,包括编号,姓名 ,本金,存储类型,以及利率等信息,能够计算账户存款到期的利息值并显示。

#include <string>
#include<iostream>
using namespace std;
class Account
{
    public:
        Account();
           Account(string ID, string Name, int MoneyCapital, int DepositType, double Life)
           {
               this->mID = ID;
               this->mName = Name;
               this->mMoneyCapital = MoneyCapital;
               this->mDepositType = DepositType;
               this->mLife = Life;
           }
        double getInterst()
        {
          return mMoneyCapital * InterestRate[mDepositType] * mDepositType+mMoneyCapital*mLife*InterestRate[0];
        }
    private:
        string mID,mName;
        int mMoneyCapital;//  本金
        int mDepositType;  //存款类型
        float mLife;   //简化处理输入以年为单位
        static float InterestRate[5];//利率
        float mInerest;//利息
};
    float  Account::InterestRate[5]= { 0.0035,0.0325,0.0375,0.0425,0.0475 };
Account::Account()
{
};
int main()
{
    Account a1("1001","zhang3",10000,1,0.8);
    Account a2("1002","li4",2000,2,1);
    cout<<a1.getInterst();
    cout<<a2.getInterst();
}

静态数据成员应用-货物销售

#include <string>
#include<iostream>
using namespace std;
class Goods
{
    public:
        Goods(string id,string name,float inprice,float outprice,int num);
         Goods();
               void sell(int sales);
               float getInPrice()
               {
                   return this->mInPrice;
               }
               float getOutPrice()
               {
                   return this->mOutPrice;
               }
        void buy(int num)
        {
            this->mNum+=num;
        }
        string GetId() { return mId; }
        void SetId(string val) { mId = val; }
        string GetName() { return mName; }
        void SetName(string val) { mName = val; }
        static float Getsum()  { return sum; }
        static float getProfit() {return profit;}
    private:
        string mId;
        string mName;
        float mInPrice;
        float  mOutPrice;
        int mNum;
           static float profit;
           static float sum;
;
};
float Goods::profit = 0;
float Goods::sum = 0;
Goods::Goods(string  id, string  name, float  inprice, float  outprice, int  num)
{
  this->mId = id;
  this->mName = name;
  this->mInPrice = inprice;
  this->mOutPrice = outprice;
  this->mNum = num;
}
Goods::Goods()
{
}
void Goods::sell(int sales)
{
  this->sum += this->getOutPrice() * sales;
  this->profit += (this->getOutPrice() - this->getInPrice()) * sales;
}
int main()
{   Goods gd[100];
    gd[0]=Goods("1001","白菜",0.8,1.5,500);
    gd[1]=Goods("1002","萝卜",1.5,2.1,500);
    gd[0].sell(10);
    gd[1].sell(20);
    gd[0].sell(5);
    cout<<gd[0].Getsum()<<endl;
    cout<<gd[1].Getsum()<<endl;
    cout<<gd[0].getProfit()<<endl;
    return 0;
}


目录
相关文章
|
1月前
|
Java 物联网 测试技术
Java面向对象程序设计3面向对象基础
Java面向对象程序设计3面向对象基础
201 0
|
7月前
|
C++
20 C++ - 面向对象程序设计案例
20 C++ - 面向对象程序设计案例
60 0
|
7月前
|
存储 C++
C++语言面向对象程序设计实验
C++语言面向对象程序设计实验
78 0
|
11月前
面向对象程序设计(OOP)的基本概念
面向对象程序设计(OOP)的基本概念
134 0
|
存储 Java
面向对象程序设计概述
面向对象程序设计概述
168 0
|
程序员 测试技术 C语言
c++面向对象程序设计入门
c++面向对象程序设计入门
144 0
|
存储 机器学习/深度学习 编译器
面向对象程序设计 C++总结笔记(1)
理解面向对象程序设计的基本原理,掌握面向对象技术的基本概念和封装性、继承性和多态性,能够具有面向对象程序设计思想。掌握C++语言面向对象的基本特性和C++语言基础知识,能够使用C++语言进行计算机工程领域复杂工程问题的表述,能够进行C++程序阅读和分析。
157 0
面向对象程序设计-第二章
面向对象程序设计-第二章
40 0
面向对象程序设计-第二章
面向对象程序设计-第一章:面向对象程序设计基础
面向对象程序设计-第一章:面向对象程序设计基础
144 0
面向对象程序设计-第一章:面向对象程序设计基础
面向对象程序设计第三章
面向对象程序设计第三章
57 0