【c++思维导图与代码示例】03 类与对象

【c++思维导图与代码示例】03 类与对象

思维导图:

在这里插入图片描述

代码示例:

  • 示例1:

/*************************************************
**
** 代码描述: 通过抽象时钟类实例,熟悉 类、对象的抽象 成员函数的声明与实现等
**
** 创建人:慕灵阁-wupke
** 创建时间:2021-11-14
** 版本:3_1.cpp
** 
*
***************************************************/
#include <iostream>	
using namespace std;

class Clock	{ //时钟类的定义
public:		//外部接口,公有成员函数
	void setTime(int newH = 0, int newM = 0, int newS = 0);
	void showTime();
private:	//私有数据成员
	int hour, minute, second;
};

//时钟类成员函数的具体实现
void Clock::setTime(int newH, int newM, int newS) {
	hour = newH;
	minute = newM;
	second = newS;
}

inline void Clock::showTime() {
	cout << hour << ":" << minute << ":" << second << endl;
}

//主函数
int main() {
	Clock myClock;	//定义对象myClock
	cout << "First time set and output:" << endl;
	myClock.setTime();	//设置时间为默认值
	myClock.showTime();	//显示时间
	cout << "Second time set and output:" << endl;
	myClock.setTime(8, 30, 30);	//设置时间为8:30:30
	myClock.showTime();	//显示时间
	system("pause");
	return 0;
}


  • 示例2:
/*************************************************
**
** 代码描述: 通过枚举类实例,熟悉 枚举类的声明、构造函数与析构函数的调用
**
** 创建人:慕灵阁-wupke
** 创建时间:2021-11-14
** 版本:3_2.cpp
** 
*
***************************************************/
#include <iostream>
using namespace std;

enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
private:
	CPU_Rank rank;
	int frequency;
	float voltage;
public:
    CPU (CPU_Rank r, int f, float v)
	{
		rank = r;
		frequency = f;
		voltage = v;
		cout << "构造了一个CPU!" << endl;
	}
	~CPU () { cout << "析构了一个CPU!" << endl; }

    CPU_Rank GetRank() const { return rank; }
    int GetFrequency() const { return frequency; }
	float GetVoltage() const { return voltage; }

    void SetRank(CPU_Rank r) { rank = r; }
    void SetFrequency(int f) { frequency = f; }
    void SetVoltage(float v) { voltage = v; }

    void Run() {cout << "CPU开始运行!" << endl; }
	void Stop() {cout << "CPU停止运行!" << endl; }
};

int main()
{
	CPU a(P6,300,2.8);  
	a.Run();
	a.Stop();

	system("pause");
	return 0;
}



  • 示例3:

/*************************************************
**
** 代码描述: 通过类实例,熟悉构造函数、复制构造函数的声明与使用
** 创建人:慕灵阁-wupke
** 创建时间:2021-11-14
** 版本:3_3.cpp
** 
*
***************************************************/

#include <iostream>
using namespace std;

class Point {	//Point 类的定义
public:		//外部接口
	Point(int xx = 0, int yy = 0) {	//构造函数
	x = xx;
		y = yy;
	}
	Point(const Point &p);	//复制构造函数
	void setX(int xx) 
	{x=xx;}
	void setY(int yy) 
	{y=yy;}

	int getX() const {
		return x;
	}
	int getY() const {
		return y;
	}
private:	//私有数据
	int x, y;
};

//成员函数的实现
Point::Point(const Point &p) {
	x = p.x;
	y = p.y;
	cout << "Calling the copy constructor" << endl;
}

//形参为Point类对象的函数
void fun1(const Point& p) {
	cout << p.getX() << endl;
	//p.setX(1);
}

//返回值为Point类对象的函数
Point fun2() {
	Point a;
	return a;
}

//主函数
int main() {
	Point a;	//第一个对象A
	Point b(a);	//情况一,用A初始化B。第一次调用复制构造函数
	cout << b.getX() << endl;
	fun1(b);		//情况二,对象B作为fun1的实参。第二次调用复制构造函数
	b = fun2();		//情况三,函数的返回值是类对象,函数返回时,调用复制构造函数
	cout << b.getX() << endl;
	system("pause");
	return 0;
}


  • 示例4:

// 例4-4 
/***         ****/

/*************************************************
**
** 代码描述: 通过类的组合,线段(Line)类,利用复制构造函数建立一个新对象,了解构造一个组合类过程中参数的变化、传递 经理的过程
(复制/构造/初始化的过程)
**
** 创建人:慕灵阁-wupke
** 创建时间:2021-11-14
** 版本:3_4.cpp
** 
*
***************************************************/
#include<iostream>
#include<cmath>
using namespace std;

//定义point类
class 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::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::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);
}

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;
	system("pause");
	return 0;
}

  • 示例5:
/*************************************************
**
** 代码描述: 结构体的声明与使用
**
** 创建人:慕灵阁-wupke
** 创建时间:2021-11-14
** 版本:3_5.cpp
** 
*
***************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Student {	//学生信息结构体
	int num;		//学号
	string name;	//姓名
	char sex;		//性别
	int age;		//年龄
};

int main() {
	Student stu = { 9527001, "Lin Lin", 'F', 19 };
	cout << "Num:  " << stu.num << endl;
	cout << "Name: " << stu.name << endl;
	cout << "Sex:  " << stu.sex << endl;
	cout << "Age:  " << stu.age << endl;
	return 0;
}


  • 示例6:
/*************************************************
**
** 代码描述: 联合体的声明与使用
**
** 创建人:慕灵阁-wupke
** 创建时间:2021-11-14
** 版本:3_6.cpp
** 
*
***************************************************/
#include <string>
#include <iostream>
using namespace std;

class ExamInfo {
public:
	//三种构造函数,分别用等级、是否通过和百分来初始化
	ExamInfo(string name, char grade)
		: name(name), mode(GRADE), grade(grade) { }
	ExamInfo(string name, bool pass)
		: name(name), mode(PASS), pass(pass) { }
	ExamInfo(string name, int percent)
		: name(name), mode(PERCENTAGE), percent(percent) { }
	void show();

private:
	string name;	//课程名称
	enum {
		GRADE,
		PASS,
		PERCENTAGE
	} mode;			//采用何种计分方式
	union {
		char grade;		//等级制的成绩
		bool pass;		//是否通过
		int percent;	//百分制的成绩
	};
};

void ExamInfo::show() {
	cout << name << ": ";
	switch (mode) {
	case GRADE:
		cout << grade;
		break;
	case PASS:
		cout << (pass ? "PASS" : "FAIL");
		break;
	case PERCENTAGE:
		cout << percent;
		break;
	}
	cout << endl;
}

int main() {
	ExamInfo course1("English", 'B');
	ExamInfo course2("Calculus", true);
	ExamInfo course3("C++ Programming", 85);
	course1.show();
	course2.show();
	course3.show();
	return 0;
}


  • 示例7:
/*************************************************
**
** 代码描述: 构建一个储蓄账户类,整体上熟悉类与对象的实现
**
** 创建人:慕灵阁-wupke
** 创建时间:2021-11-14
** 版本:3_7.cpp
** 
*
***************************************************/
//4_9.cpp
#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;
    system("pause");
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值