类的基础代码 回顾

银行储蓄账户

#include <iostream>
#include <cmath>
using namespace std;
class SavingsAccount { //储蓄账户类
private:
	int id;				//账号
	double balance;		//余额
	double rate;		//存款的年利率
	int lastDate;		//上次变更余额的时期
	double accumulation;	//余额按日累加之和
 
	//记录一笔帐,date为日期,amount为金额,desc为说明
	void record(int date, double amount);
	//获得到指定日期为止的存款金额按日累积值
	double accumulate(int date) const {
		return accumulation + balance * (date - lastDate);
	}
public:
	//构造函数
	SavingsAccount(int date, int id, double rate);
	int getId() { return id; }
	double getBalance() { return balance; }
	double getRate() { return rate; }
 
	//存入现金
	void deposit(int date, double amount);
	//取出现金
	void withdraw(int date, double amount);
	//结算利息,每年1月1日调用一次该函数
	void settle(int date);
	//显示账户信息
	void show();
};
//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(int date, int id, double rate)
	: id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
	cout << date << "\t#" << id << " is created" << endl;
}
void SavingsAccount::record(int date, double amount) {
	accumulation = accumulate(date);
	lastDate = date;
	amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
	balance += amount;
	cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}
void SavingsAccount::deposit(int date, double amount) {
	record(date, amount);
}
void SavingsAccount::withdraw(int date, double amount) {
	if (amount > getBalance())
		cout << "Error: not enough money" << endl;
	else
		record(date, -amount);
}
 
void SavingsAccount::settle(int date) {
	double interest = accumulate(date) * rate / 365;	//计算年息
	if (interest != 0)
		record(date, interest);
	accumulation = 0;
}
 
void SavingsAccount::show() {
	cout << "#" << id << "\tBalance: " << balance;
}
int main() {
	//建立几个账户
	SavingsAccount sa0(1, 21325302, 0.015);
	SavingsAccount sa1(1, 58320212, 0.015);
	//几笔账目
	sa0.deposit(5, 5000);
	sa1.deposit(25, 10000);
	sa0.deposit(45, 5500);
	sa1.withdraw(60, 4000);
	//开户后第90天到了银行的计息日,结算所有账户的年息
	sa0.settle(90);
	sa1.settle(90);
	//输出各个账户信息
	sa0.show();	cout << endl;
	sa1.show();	cout << endl;
	return 0;
}



时钟

include<iostream>
using namespace std;
 
class Clock {
public:
    // 修正:移除默认参数,只在声明或定义一个地方提供默认参数
    Clock(int newH, int newM, int newS);
    Clock(); // 修正:添加默认构造函数声明
    void settime(int newH, int newM, int newS);
    void showtime(); // 修正:修正函数声明,移除多余的 void
private:
    int hour, minute, second;
};
 
// 添加默认构造函数定义
 
Clock::Clock() {
    hour = 0;
    minute = 0;
    second = 0;
}/*与之前把默认值添加在函数声明结构的代码比较 显然把所有函数拿出声明部分 单独定义会使代码可读性更高 且更有逻辑 易于维护*/
 
 
 
// 构造函数的定义
Clock::Clock(int newH, int newM, int newS) {
    hour = newH;
    minute = newM;
    second = newS;
}
 
// 修正:修正函数定义,移除多余的 void
void Clock::showtime() {
    cout << hour << ":" << minute << ":" << second << endl;
}
 
int main() {
    // 修正:移除多余的括号
    Clock c1(8, 30, 30);
    Clock c2; // 修正:使用默认构造函数创建 c2
    c1.showtime(); // 修正:调用函数需要加括号
    c2.showtime();
 
    return 0;
}


物品和容器

#include <iostream>
using namespace std;
 
class Object {
private:
    int val;
 
public:
    Object();
    Object(int i);
    ~Object();
};
 
Object::Object() {
    val = 0;
    cout << "Default constructor for Object" << endl;
}
 
Object::Object(int i) {
    val = i;
    cout << "Constructor for Object " << val << endl;
}
 
Object::~Object() {
    cout << "Destructor for Object " << val << endl;
}
 
class Container {
private:
    Object one;
    Object two;
    int data;
 
public:
    Container();
    Container(int i, int j, int k);
    ~Container();
};
 
Container::Container() {
    data = 0;
    cout << "Default constructor for Container" << endl;
}
 
Container::Container(int i, int j, int k) : two(i), one(j) {
    data = k;
    cout << "Constructor for Container" << endl;
}
 
Container::~Container() {
    cout << "Destructor for Container" << endl;
}
 
int main() {
    Container anObj(5, 6, 10);
    return 0;
}

点和线

#include <iostream>
#include <cmath>
using namespace std;
class Point {	//Point类定义
public:
	Point(int xx = 0, int yy = 0) {
		x = xx;
		y = yy;
	}
	Point(Point& p);
	int getX() { return x; }
	int getY() { return y; }
private:
	int x, y;
};
 
/*point类有一个构造函数 用于初始化x和y 并有一个默认参数为0的构造函数*/
/*复制构造函数用于复制另一个point对象的值*/
Point::Point(Point& p) {	//复制构造函数的实现
	x = p.x;
	y = p.y;
	cout << "Calling the copy constructor of Point" << endl;
}
 
 
//类的组合
class Line {	//Line类的定义
public:	//外部接口
	Line(Point xp1, Point xp2);
	Line(Line& l);
	double getLen() { return len; }
private:	//私有数据成员
	Point p1, p2;	//Point类的对象p1,p2
	double len;
};
/*line类包含两个point对象p1和p2 以及一个表示线段长度的len*/
//组合类的构造函数
Line::Line(Point xp1, Point xp2) : p1(xp1), p2(xp2) {
	cout << "Calling constructor of Line" << endl;
	double x = static_cast<double>(p1.getX() - p2.getX());
	double y = static_cast<double>(p1.getY() - p2.getY());
	len = sqrt(x * x + y * y);
}
/*使用初始化列表初始化p1和p2 计算线段的长度并存储在‘len’中*/
Line::Line(Line& l) : p1(l.p1), p2(l.p2) {//组合类的复制构造函数
	cout << "Calling the copy constructor of Line" << endl;
	len = l.len;
}
//主函数
int main() {
	Point myp1(1, 1), myp2(4, 5);	//建立Point类的对象
	Line line(myp1, myp2);	//建立Line类的对象
	Line line2(line);	//利用复制构造函数建立一个新对象
	cout << "The length of the line is: ";
	cout << line.getLen() << endl;
	cout << "The length of the line2 is: ";
	cout << line2.getLen() << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值