第5章数据的共享与保护
约 2445 字大约 8 分钟
2026-11-10
5-1 P152 作用域实例
源文件:5_01.cpp
/*
5-1 P152
作用域实例
*/
#include<iostream>
using namespace std;
int i; //全局变量,文件作用域
int main()
{
i=5; //为全局变量i赋值
{ //子块1
int i; //局部变量,局部作用域
i=7;
cout<<"i="<<i<<endl; //输出7,全局变量i被隐藏了(不可见)
}
cout<<"i="<<i<<endl; //输出5
getchar();//程序暂停
return 0;
}5-2 P154 变量的生存期与可见性
源文件:5_02.cpp
/*
5-2 P154
变量的生存期与可见性
*/
#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<<"---OUTER---"<<endl;
cout<<"i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;
b=a;
}
int main()
{
static int a;
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;
}5-3 P155 具有静态、动态生存期对象的时钟程序
源文件:5_03.cpp
/*
5-3 P155
具有静态、动态生存期对象的时钟程序
*/
#include<iostream>
using namespace std;
class Clock{
private:
int hour,minute,second;
public:
Clock();//默认构造函数
void setTime(int newH,int newM,int newS);//设置时间
void showTime();//显示时间
~Clock();//析构函数
};
//Clock类的函数实现
//构造函数
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::~Clock(){}
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;
}5-4 P158 具有静态数据成员的Point类
源文件:5_04.cpp
/*
5-4 P158
具有静态数据成员的Point类
*/
#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;
}
void showorigin(){ //输出静态数据成员
cout<<" Object origin="<<origin<<endl;
}
private: //私有数据成员
int x,y;
static int count; //静态数据成员声明,用于记录点的个数
constexpr static int origin=0; //常量静态成员类内初始化
};
int Point::count=0; //静态数据成员的定义和初始化,使用类名限定
constexpr int Point::origin; //类外定义常量静态成员,但不可二次初始化
int main()
{
Point a(4,5); //定义对象a,其构造函数会使count增1
cout<<"Point A: "<<a.getX()<<", "<<a.getY();
a.showCount(); //输出对象个数
a.showorigin(); //输出常量静态成员
Point b(a); //定义对象b,其构造函数会使count增1
cout<<"Point B: "<<b.getX()<<", "<<b.getY();
b.showCount(); //输出对象个数
b.showorigin(); //输出常量静态成员
system("pause");//程序暂停
return 0;
}5-5 P160 具有静态数据、函数成员的Point类
源文件:5_05.cpp
/*
5-5 P160
具有静态数据、函数成员的Point类
*/
#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;
}
static void showorigin()
{ //输出静态数据成员
cout << " Object origin=" << origin << endl;
}
private: //私有数据成员
int x, y;
static int count; //静态数据成员声明,用于记录点的个数
constexpr static int origin = 0; //常量静态成员类内初始化
};
int Point::count = 0; //静态数据成员的定义和初始化,使用类名限定
constexpr int Point::origin; //类外定义常量静态成员,但不可二次初始化
int main()
{
Point a(4, 5); //定义对象a,其构造函数会使count增1
cout << "Point A: " << a.getX() << ", " << a.getY();
Point::showCount(); //输出对象个数
Point::showorigin(); //输出常量静态成员
Point b(a); //定义对象b,其构造函数会使count增1
cout << "Point B: " << b.getX() << ", " << b.getY();
Point::showCount(); //输出对象个数
Point::showorigin(); //输出常量静态成员
system("pause"); //程序暂停
return 0;
}5-6 P163 使用友元函数计算两点间的距离
源文件:5_06.cpp
/*
5-6 P163
使用友元函数计算两点间的距离
*/
#include<iostream>
#include<cmath>//sqrt()
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,1),myp2(4,5);
cout<<"The distence is: ";
cout<<dist(myp1,myp2)<<endl;
system("pause");//程序暂停
return 0;
}5-7 P167 常成员函数举例
源文件:5_07.cpp
/*
5-7 P167
常成员函数举例
*/
#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(20,52);
b.print(); //调用void print() const
system("pause");//程序暂停
return 0;
}5-8 P168 常数据成员举例
源文件:5_08.cpp
/*
5-8 P168
常数据成员举例
*/
#include<iostream>
using namespace std;
class A{
public:
A(int i);
void print();
const int ss;
private:
const int a;
static const int b; //静态常数据成员
//类的静态常量若是整型或枚举,也可在类内定义
//static const int b=11; //静态常数据成员
};
const int A::b=10; //静态常数据成员在类外说明和初始化
//常数据成员只能通过初始化列表来获得初值
A::A(int i):a(i),ss(i){}
void A::print()
{
cout<<a<<":"<<b<<endl;
}
int main()
{
/*建立对象a和对象b,并以100和0作为初值,分别调用构造函数,
通过构造函数的初始化列表给对象的常数据成员赋初值*/
A a1(100),a2(0);
a1.print();
a2.print();
cout<<a1.ss<<":"<<a2.ss<<endl;
system("pause");//程序暂停
return 0;
}5-9 P169 常引用做形参
源文件:5_09.cpp
/*
5-9 P169
常引用做形参
*/
#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;
};
//dist函数中,无需修改两个传入对象的值,因此将传参方式改为传递常引用更合适,
//这样,调用dist函数时,就可以用常对象作为其参数
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(4,5);
cout<<"The distence is: ";
cout<<dist(myp1,myp2)<<endl;
system("pause");//程序暂停
return 0;
}5_10\5_10.cpp
源文件:5_10\5_10.cpp
/*
安装cmake,mingw后,在VSCode的TERMINAL输入命令:
//先到这三个文件所在的文件夹
cd chapt5
cd 5_10
//编译链接
g++ -g .\5_10.cpp .\Point.cpp -o my5_10
//此时文件夹中就会有my5_10.exe文件,输入指令执行:
.\my5_10.exe
*/
#include"Point.h"
#include<iostream>
using namespace std;
int main()
{
Point a(4,5);
cout<<"Point A: "<<a.getX()<<", "<<a.getY();
Point::showCount();
Point b(a);
cout<<"Point B: "<<b.getX()<<", "<<b.getY();
Point::showCount();
return 0;
}5_10\Point.cpp
源文件:5_10\Point.cpp
#include"Point.h"
#include<iostream>
using namespace std;
int Point::count=0; //使用类名初始化静态数据成员
Point::Point(const Point &p):x(p.x),y(p.y) //复制构造函数体
{
count++;
}
void Point::showCount()
{
cout<<" Object count="<<count<<endl;
}5_10\Point.h
源文件:5_10\Point.h
class Point{
public:
Point(int x=0,int y=0):x(x),y(y){count++;}
Point(const Point &p);
~Point(){count--;}
int getX() const{return x;}
int getY() const{return y;}
static void showCount(); //静态函数成员
private:
int x,y;
static int count;
};5_11\5_11.cpp
源文件:5_11\5_11.cpp
#include"account.h"
#include<iostream>
using namespace std;
int main()
{
system("pause");
return 0;
}5_11\account.cpp
源文件:5_11\account.cpp
#include"account.h"
#include"cmath"
#include<iostream>
using namespace std;
double SavingsAccount::total=0;
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;
}5_11\account.h
源文件:5_11\account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
class SavingsAccount{ //
private:
int id;
double balance;
double rate;
int lastDate;
double accumulation;
static double total;//静态数据成员,
//
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 getTate()const{return rate;}
static double getTotal(){return total;}//通过静态成员函数访问静态数据成员
void deposit(int date,double amount);
void withdraw(int date,double amount);
//
void settle(int date);
//
void show()const;
};
#endif