【c++思维导图与代码示例】04 数据共享与保护
思维导图:
代码示例:
#include
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;
}
#include
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;
}
#include
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:"<
#include
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;
}
#include
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;
}
#include
#include
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(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;
}
#include
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;
}
#include
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 a1(55), a2(0);
a1.print();
a2.print();
// system("pause");
return 0;
}
#include
#include
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(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;
}
#ifndef CLIENT_H_ // 避免重复声明
#define CLIENT_H_ // 避免重复声明
#include
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_ // 避免重复声明
#include "Dog.h"
#include
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 <
#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;
}
#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__
#include "account.h"
#include
#include
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;
}
//5_11.cpp
#include "account.h"
#include
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;
}