【c++思维导图与代码示例】04 数据共享与保护

【c++思维导图与代码示例】04 数据共享与保护

思维导图:

在这里插入图片描述

代码示例:

  • 示例1:

/*************************************************
**
** Description: 理解全局变量与局部变量 作用域 与 可见性
**
** Author:慕灵阁-wupke
** Time:2021-11-11
** Versions:4_1.cpp
** 
*
***************************************************/

#include <iostream>
using namespace std;

int i;			//在全局命名空间中的全局变量

namespace Ns {
	int j;		//在Ns命名空间中的全局变量
}

int main() {
	i = 5;			//为全局变量i赋值
	Ns::j = 6;		//为全局变量j赋值
	{				//子块1
		using namespace Ns; //使得在当前块中可以直接引用Ns命名空间的标识符
		int i;		//局部变量,局部作用域
		i = 7;
		cout << "i = " << i << endl;//输出7
		cout << "j = " << j << endl;//输出6
	}
	cout << "i = " << i << endl;	//输出5
    //system("pause");
	return 0;
}


  • 示例2:

/*************************************************
**
** Description: 通过赋值 实例,理解不同作用域的对象的生存周期
**
** Author:慕灵阁-wupke
** Time:2021-11-14
** Versions:4_2.cpp
** 
*
***************************************************/

#include <iostream>
using namespace std;
int i = 1;	// i 为全局变量,具有静态生存期

void other() {
	//a, b为静态局部变量,具有全局寿命,局部可见,只第一次进入函数时被初始化
	static int a = 2;
	static int b;
	//c为局部变量,具有动态生存期,每次进入函数时都初始化
	int c = 10;
	a += 2;
	i += 32;
	c += 5;
	cout << "---OTHER---" << endl;
	cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
	b = a;
}

int main() {
	//a为静态局部变量,具有全局寿命,局部可见
	static int a;
	//b, c为局部变量,具有动态生存期
	int b = -10;	
	int c = 0;

	cout << "---MAIN---" << endl;
	cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
	c += 8;
	other();
	cout << "---MAIN---" << endl;
	cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
	i += 10;
	other();
	//system("pause");
	return 0;
}




  • 示例3:

/*************************************************
**
**Description: 理解类作用域、静态生存周期
**
** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_3.cpp
** 
*
***************************************************/

#include<iostream>
using namespace std;

class Clock {	//时钟类定义
public:	//外部接口
	Clock();
	void setTime(int newH, int newM, int newS);   //三个形参均具有函数原型作用域
	void showTime();
private:	//私有数据成员
	int hour, minute, second;
};

//时钟类成员函数实现
Clock::Clock() : hour(0), minute(0), second(0) { }	//构造函数

void Clock::setTime(int newH, int newM, int newS) {//三个形参均具有局部作用域
	hour = newH;
	minute = newM;
	second = newS;
}

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

Clock globClock;  //声明对象globClock,具有静态生存期,命名空间作用域
//由缺省构造函数初始化为0:0:0
int main() {	//主函数
	cout << "First time output:" << endl;	
	//引用具有命名空间作用域的对象globClock:
	globClock.showTime();	//对象的成员函数具有类作用域
	//显示0:0:0
	globClock.setTime(8,30,30);	//将时间设置为8:30:30

	Clock myClock(globClock);	//声明具有局部作用域的对象myClock
	//调用拷贝构造函数,以globClock为初始值
	cout<<"Second time output:"<<endl;	
	myClock.showTime();	//引用具有局部作用域的对象myClock
	//输出8:30:30
    // system("pause");
	return 0;
}

  • 示例4:


/*************************************************
**
**Description: 理解类中的静态数据成员
**
** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_4.cpp
** 
*
***************************************************/

#include <iostream>
using namespace std;

class Point {	//Point类定义
public:	//外部接口
	Point(int x = 0, int y = 0) : x(x), y(y) { //构造函数
		//在构造函数中对count累加,所有对象共同维护同一个count
		count++;
	}	
	Point(Point &p) {	//拷贝构造函数
		x = p.x;
		y = p.y;
		count++;
	}
	~Point() {  count--; }
	int getX() { return x; }
	int getY() { return y; }

	void showCount() {		//输出静态数据成员
		cout << "  Object count = " << count << endl;
	}
private:	//私有数据成员
	int x, y;
	static int count;	//静态数据成员声明,用于记录点的个数
};

int Point::count = 0;	//静态数据成员定义和初始化,使用类名限定

int main() {	//主函数
	Point a(4, 5);	//定义对象a,其构造函数回使count增1
	cout << "Point A: " << a.getX() << ", " << a.getY();
	a.showCount();	//输出对象个数

	Point b(a);	//定义对象b,其构造函数回使count增1
	cout << "Point B: " << b.getX() << ", " << b.getY();
	b.showCount();	//输出对象个数
    system("pause");
	return 0;
}


  • 示例5:


/*************************************************
**
**Description: 理解类中的静态函数成员
**
** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_5.cpp
** 
*
***************************************************/

#include <iostream>
using namespace std;

class Point {	//Point类定义
public:	//外部接口
	Point(int x = 0, int y = 0) : x(x), y(y) { //构造函数
		//在构造函数中对count累加,所有对象共同维护同一个count
		count++;
	}	
	Point(Point &p) {	//拷贝构造函数
		x = p.x;
		y = p.y;
		count++;
	}
	~Point() {  count--; }
	int getX() { return x; }
	int getY() { return y; }

	static void showCount() {		//静态函数成员
		cout << "  Object count = " << count << endl;
	}
private:	//私有数据成员
	int x, y;
	static int count;	//静态数据成员声明,用于记录点的个数
};

int Point::count = 0;	//静态数据成员定义和初始化,使用类名限定

int main() {	//主函数
	Point::showCount();	//输出对象个数
	Point a(4, 5);	//定义对象a,其构造函数回使count增1
	cout << "Point A: " << a.getX() << ", " << a.getY();
	Point::showCount();	//输出对象个数
	a.showCount();	//输出对象个数

	Point b(a);	//定义对象b,其构造函数回使count增1
	cout << "Point B: " << b.getX() << ", " << b.getY();
	b.showCount();	//输出对象个数
	Point::showCount();	//输出对象个数
    system("pause");
	return 0;
}


  • 示例6:

/*************************************************
**
**Description: 理解类中友元函数
**
** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_6.cpp
** 
*
***************************************************/

#include <iostream>
#include <cmath>
using namespace std;

class Point {	//Point类定义
public:	//外部接口
	Point(int x = 0, int y = 0) : x(x), y(y) { }
	int getX() { return x; }
	int getY() { return y; }
	friend float dist(Point &p1, Point &p2);	//友元函数声明
private:	//私有数据成员
	int x, y;
};

float dist(Point &p1, Point &p2) {	//友元函数实现
	double x = p1.x - p2.x;	//通过对象访问私有数据成员
	double y = p1.y - p2.y;
	return static_cast<float>(sqrt(x * x + y * y));
}

int main() {	//主函数
	Point myp1(1, 3), myp2(5, 5);	//定义Point类的对象
	cout << "The distance is: ";
	cout << dist(myp1, myp2) << endl;	//计算两点间的距离
    // system("pause");
	return 0;
}


  • 示例7:

/*************************************************
**
**Description: 理解类中 常成员函数的声明与调用
**
** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_7.cpp
** 
*
***************************************************/

#include<iostream>
using namespace std;

class R {
public:
	R(int r1, int r2) : r1(r1), r2(r2) { }
	void print();
	void print() const;  // 常成员函数
private:
	int r1, r2;
};

void R::print() {   
	cout << r1 << ":" << r2 << endl;
}

void R::print() const {   
	cout << r1 << ";" << r2 << endl;
}

int main() {
	R a(5,4);
	a.print();  //调用void print()
	const R b(8,24);  
	b.print();  //调用void print() const
    // system("pause");
	return 0;
}


  • 示例8:

/*************************************************
**
**Description: 理解类中 常数据成员举例 
 常数据成员,只有在函数初始化的时候才能给其赋值,其他在地方都不能改变其数值
** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_8.cpp
** 
*
***************************************************/
#include <iostream>
using namespace std;

class A {
public:
	A(int i);
	void print();
private:
	const int a;
	static const int b;   //静态常数据成员
};

const int A::b = 10;	//静态常数据成员在类外说明和初始化

//常数据成员只能通过初始化列表来获得初值
A::A(int i) : a(i) { }

void A::print() {
	cout << a << ":" << b << endl;
}
int main() {
/*建立对象a和b,并以100和0作为初值,分别调用构造函数,通过构造函数的初
	始化列表给对象的常数据成员赋初值*/
	A a1(55), a2(0);  
	a1.print();
	a2.print();
    // system("pause");
	return 0;
}



  • 示例9:

/*************************************************
**
**Description: 常 引用 做形参 ,只能拿来用,不会反向修改数据
** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_9.cpp
** 
*
***************************************************/

#include <iostream>
#include <cmath>
using namespace std;

class Point {	//Point类定义
public:	//外部接口
	Point(int x = 0, int y = 0) : x(x), y(y) { }
	int getX() { return x; }
	int getY() { return y; }
	friend float dist(const Point &p1, const Point &p2);
private:	//私有数据成员
	int x, y;
};

float dist(const Point &p1, const Point &p2) {	//常引用作形参
	double x = p1.x - p2.x;	
	double y = p1.y - p2.y;
	return static_cast<float>(sqrt(x * x + y * y));
}

int main() {	//主函数
	const Point myp1(1, 1), myp2(8, 24);	//定义Point类的对象
	cout << "The distance is: ";
	cout << dist(myp1, myp2) << endl;	//计算两点间的距离
    // system("pause");
	return 0;
}




  • 示例10:
/*************************************************
**
**Description: 使用c++的多文件工程,实现一个Dog类的调用
其实就是把原来整体结构的代码,按照不同的功能,拆分为3个不同的文件,分别放置代码

** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_10.cpp
** 
*
***************************************************/

/*  头文件  Dog.h  */

#ifndef CLIENT_H_   // 避免重复声明
#define CLIENT_H_   // 避免重复声明
#include <string>
using namespace std;

// 声明类及其成员函数
class Dog{
private:
    string name;
    string weight;
    string color;

public:
    void Info(string name,string weight,string color);
    void show_info();
    //动作可以再细分类
    void sit();
    void roll();

};

#endif //CLIENT_H_    // 避免重复声明








/*  类的实现文件(各种成员函数功能实现)  Dog.cpp  */

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

// 类中函数的实现

void Dog::Info(string new_name,string new_weight,string new_color){
     Dog::name = new_name;
    Dog::weight = new_weight;
    Dog::color = new_color;
}

void Dog::show_info(){
    cout<<" This dog's name is "<< name <<"and its "<< weight <<" with "<< color <<endl;
}
void Dog::sit(){
    cout<<" This dog has sitted down. " <<endl;
}
void Dog::roll(){
    cout<<" This dog's  is  rolling now!"<<endl;
}





/*  主函数调用文件(类的调用 )  main.cpp  */

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

// 主函数调用

int main(){

    Dog myDog;
    myDog.Info("Timi","30","write");
    myDog.show_info();
    myDog.sit();
    myDog.roll();
    
    // system("pause");
    return 0;
}


  • 示例12:

/*************************************************
**
**Description: 将第三章实例中创建的银行存储账户代码 ,进行文件拆分

** Author:慕灵阁-wupke
** Time:2021-11-15
** Versions :4_11.cpp
** 
*
***************************************************/

/*  (类的声明 )  account.h  */

#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__

class SavingsAccount { //储蓄账户类
private:
	int id;				//账号
	double balance;		//余额
	double rate;		//存款的年利率
	int lastDate;		//上次变更余额的时期
	double accumulation;	//余额按日累加之和
	static double total;	//所有账户的总金额

	//记录一笔帐,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() const { return id; }
	double getBalance() const { return balance; }
	double getRate() const { return rate; }
	static double getTotal() { return total; }

	//存入现金
	void deposit(int date, double amount);
	//取出现金
	void withdraw(int date, double amount);
	//结算利息,每年1月1日调用一次该函数
	void settle(int date);
	//显示账户信息
	void show() const;
};

#endif //__ACCOUNT_H__






/*  (类的实现 )  account.cpp  */

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

double SavingsAccount::total = 0;

//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;
	total += 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() const {
	cout << "#" << id << "\tBalance: " << balance;
}





/*  主函数调用文件(类的调用 )  main.cpp  */

//5_11.cpp
#include "account.h"
#include <iostream>
using namespace std;

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;
	cout << "Total: " << SavingsAccount::getTotal() << endl;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值