浅谈C++与Java中 “ :: ” 的区别

7 篇文章 0 订阅

(1)在实现类中方法时,用于指明方法隶属于哪个类,即方法的路径

C++中继承关系

在这里插入图片描述
抽象类与基类:

//acctabc.h -- bank account classes
#ifndef ACCTABC_H_
#define ACCTABC_H_ 
#include <iostream>
#include <string>

//Abstract Base Class
class AcctABC
{
private:
 std::string fullName;
 long acctNum;
 double balance;

protected:
 struct Formatting
 {
  std::ios_base::fmtflags flag;
  std::streamsize pr;
 };
 const std::string & FullName() const { return fullName; }
 long AcctNum() const { return acctNum; }
 Formatting SetFormat() const;
 void Restore(Formatting & f) const;

public:
 AcctABC(const std::string & s = "Nullbody", long an = -1,
  double bal = 0.0);
 void Deposit(double amt);
 virtual void Withdraw(double amt) = 0; //pure virtual function
 double Balance() const { return balance; }
 virtual void ViewAcct() const = 0; //pure virtual function
 virtual ~AcctABC() {}
}; 

//Brass Account Class
class Brass : public AcctABC
{
public:
 Brass(const std::string & s = "Nullbody", long an = -1,
  double bal = 0.0) : AcctABC(s, an, bal) {}
 virtual void Withdraw(double amt);
 virtual void ViewAcct() const;
 virtual ~Brass() {}
};

//Brass Plus Account Class
class BrassPlus : public AcctABC
{
private:
 double maxLoan;
 double rate;
 double owesBank;
public:
 BrassPlus(const std::string & s = "Nullbody", long an = 1,
  double bal = 0.0, double ml = 500, double r = 0.0);
 BrassPlus(const Brass & ba, double ml = 500, double r = 0.1);
 virtual void ViewAcct() const;
 virtual void Withdraw(double  amt);
 void ResetMax(double m) { maxLoan = m; }
 void ResetRate(double r) { rate = r; }
 void ResetOwes() { owesBank = 0; }
};

#endif

方法实现:

//acctabc.cpp -- bank account class methods
#include <iostream>
#include "acctabc.h"
using std::cout;
using std::ios_base;
using std::endl;
using std::string;

//Abstract Base Class
AcctABC::AcctABC(const string & s, long an, double bal)
{
 fullName = s;
 acctNum = an;
 balance = bal;
}

void AcctABC::Deposit(double amt)
{
 if (amt < 0)
  cout << "Negative deposit not allowed; "
  << "deposit is cancelled.\n";
 else
  balance += amt;
}

void AcctABC::Withdraw(double amt)
{
 balance -= amt;
}

//protected methods for formatting
AcctABC::Formatting AcctABC::SetFormat() const
//AcctABC::Formatting 整体作为返回类型
{
 //set up ###.## format
 Formatting f;
 f.flag = cout.setf(ios_base::fixed, ios_base::floatfield);
 f.pr = cout.precision(2);
 return f;
}

void AcctABC::Restore(Formatting & f) const
{
 cout.setf(f.flag, ios_base::floatfield);
 cout.precision(f.pr);
}

//Brass methods
void Brass::Withdraw(double amt)
{
 if (amt < 0)
  cout << "Withdrawal amount must be positive; "
  << "withdrawal canceled.\n";
 else if (amt <= Balance())
  AcctABC::Withdraw(amt);
 else
  cout << "Withdrawal amount of $" << amt
  << " exceeds your balance.\n"
  << "Withdrawal canceled.\n";
}

void Brass::ViewAcct() const
{
 Formatting f = SetFormat();
 cout << "Brass Client: " << FullName() << endl;
 cout << "Account Number: " << AcctNum() << endl;
 cout << "Balance: $" << Balance() << endl;
 Restore(f);
}

//BrassPlus Methods
BrassPlus::BrassPlus(const string & s, long an, double bal,
 double ml, double r) : AcctABC(s, an, bal)
{
 maxLoan = ml;
 owesBank = 0.0;
 rate = r;
}

void BrassPlus::ViewAcct() const
{
 Formatting f = SetFormat();

 cout << "BrassPlus Client: " << FullName() << endl;
 cout << "Acctount Number: " << AcctNum() << endl;
 cout << "Balance: $" << Balance() << endl;
 cout << "Maximum loan: $" << maxLoan << endl;
 cout << "Owed to bank: $" << owesBank << endl;
 cout.precision(3);
 cout << "Loan Rate: " << 100 * rate << "%\n";
 Restore(f);
}

void BrassPlus::Withdraw(double amt)
{
 Formatting f = SetFormat();

 double bal = Balance();
 if (amt <= bal)
  AcctABC::Withdraw(amt);
 else if (amt <= bal + maxLoan - owesBank)
 {
  double advance = amt - bal;
  owesBank += advance * (1.0 + rate);
  cout << "Bank advance: $" << advance << endl;
  cout << "Finance charge: $" << advance * rate << endl;
  AcctABC::Withdraw(amt);
 }
 else
  cout << "Credit limit exceeded. Transaction cancelled.\n";
 Restore(f);
}

主函数:

//usebrass3.cpp -- polymorphic example using an abstract base class
//compile  with acctabc.cpp
#include <iostream>
#include <string>
#include "acctabc.h"
const int CLIENTS = 4;

int main()
{
 using std::cin;
 using std::cout;
 using std::endl;

 AcctABC * p_clients[CLIENTS];
 std::string temp;
 long tempnum;
 double tempbal;
 char kind;

 for (int i = 0; i < CLIENTS; i++)
 {
  cout << "Enter client's name: ";
  getline(cin, temp);
  cout << "Enter client's account number: ";
  cin >> tempnum;
  cout << "Enter opening balance: $";
  cin >> tempbal;
  cout << "Enter 1 for Brass Account or "
   << "2 for BrassPlus Account: ";
  while (cin >> kind && (kind != '1' && kind != '2'))
   cout << "Enter either 1 or 2: ";
  if (kind == '1')
   p_clients[i] = new Brass(temp, tempnum, tempbal);

  else
  {
   double tmax, trate;
   cout << "Enter the overdraft limit: $";
   cin >> tmax;
   cout << "Enter the interest rate "
    << "as a decimal fraction: ";
   cin >> trate;
   p_clients[i] = new BrassPlus(temp, tempnum, tempbal,
    tmax, trate);
  }
  while (cin.get() != '\n');
  continue;
 }
 cout << endl;
 for (int i = 0; i < CLIENTS; i++)
 {
  p_clients[i]->ViewAcct();
  cout << endl;
 }

  for (int i = 0; i < CLIENTS; i++)
 {
  delete p_clients[i]; //free memory
 }
 cout << "Done.\n";
 system("pause");
 return 0;
}

Java中继承关系用extends和implements

例子请详见 2020.8.1 所发布博文

(2) 静态方法调用

C++中:

在这里插入图片描述

Java中:

在这里插入图片描述

(3)实例方法调用不同(方法覆盖)

(4) 子类构造器实现

在这里插入图片描述

(5)方法引用

在这里插入图片描述

(6)子类构造器引用

在这里插入图片描述

(7)用于生成或合并函数

在这里插入图片描述

(8)嵌套类

在这里插入图片描述
参考:《C++ Primer Plus 6nd》、《Java核心技术卷Ⅰ》

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

如果大家喜欢这篇文章的话,希望大家收藏、转发、关注、评论、点赞,转载请注明出自这里。 PS:本随笔属个人学习小结,文中内容有参考互联网上的相关文章。如果您博文的链接被我引用,我承诺不会参杂经济利益;如果有版权纠纷,请私信留言。其中如果发现文中有不正确的认知或遗漏的地方请评论告知,谢谢! 还是那句话:不是我喜欢copy,是站在巨人的肩膀上~~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值