C++程序设计(第四版郑莉)-------第五章

本文详细阐述了C++中的数据标识符作用域、可见性规则,静态和动态对象的生存期,类的静态成员和友元功能,以及多文件编程结构和编译预处理指令的使用。通过实例讲解了如何管理数据共享和避免重复定义问题。
摘要由CSDN通过智能技术生成

 数据的共享与保护

5.1   标识符的作用域与可见性

5.1.1作用域

        :一个标识符在程序正文中有效的区域

1函数原型作用域(最小的作用域)

        :在函数原型声明时形参的作用范围

2局部作用域

        :函数参数从声明到}结束

3类的作用域

        :x类的成员m具有类的作用域,访问有三种方式

(1)如果x的成员函数中没有声明同名的局部作用域标识符,那么在该函数内可以直接访问成员m

(2)x.m/x::m(访问静态成员)两种基本访问方式

(3)ptr->m,ptr为指向类的一个对象的指针

4命名空间作用域

语法:namespace 命名空间名

{声明}

如果要引用其他命名空间的标识符

语法:

命名空间名::标识符名

using语句

1using 命名空间名 ::标识符名;

2using namespace 命名空间名;

内嵌:命名空间名::命名空间名::标识符名

两种特殊的命名空间

1全局命名空间(默认的命名空间)

2匿命名空间

语法:namespace{

匿命名空间的声明}

用来屏蔽不希望被暴露给其他源文件的标识符

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int i;
namespace ns {
	int j;
}
int main() {
	i = 5;
	ns::j = 6;
	{
		using namespace ns;
		int i;
		i = 7;
		cout << "i" << i << endl;
		cout << "j" << j << endl;

	}
	cout << "i" << i << endl;
	return 0;
}

具有命名空间作用域的变量有称全局变量

5.1.2可见性

程序运行到某一点,能够引用到的标识符,就是该处可见的标识符

命名空间作用域》类的作用域》局部作用域

标识符声明前,引用后

多个作用域声明同名的标识符,则外层存在内层不可见

5.2对象的生存期

5.2.1静态生存期(static)

与程序的运行期相同 

静态变量未初始化自动为0;

5.2.2动态生存期

类的成员对象也各自的生存期,不同static修饰的成员对象,其生存期都与它们所属对象的生存期保持一致

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int i = 1;//全局变量,静态生存期】
void other() {
	//a,b静态局部变量,第一次进入初始化
	static int a = 2;
	static int b;
	//c动态(生存期)局部变量
	int c = 10;
	a += 2;
	i += 32;
	c += 5;
	cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
	b = a;
}
int main() {
	static int a;
	int b = -10, c = 0;
	//i=1,b=-10,c=0,a=0
	cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
	//c=8
	c += 8;
	//a=4,b=0,c=15,i=33
	other();
	//a=0,b=-10,c=8,i=33
	cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;

	//i=43
	i += 10;
	//a=6,b=4,c=15,i=75
	other();
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using  namespace std;
class clock {
public:
	clock();
	void settime(int newh, int newm, int news);
	void showtime();
private:
	int h, m, s;
};
clock::clock() :h(0), m(0), s(0){}
void clock::settime(int newh, int newm, int news) {
	h = newh;
	m = newm;
	s = news;
}
void clock::showtime() {
	cout << h << ":" << m << ":" << s << ":" << endl;
}
 class clock globclock;


int main() {
	globclock.showtime ();
	globclock.settime(8, 30, 30);
	class clock myclock(globclock);
	myclock.showtime();
}
 

5.3类的静态成员

解决一个类中不同对象之间的数据与函数共享

5.3.1静态数据成员 

类属性:描述类的所有对象共同特征的一个数据项,对任何对象的实例,他的属性值时i相同的

类名::标识符

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class point {
public:
	point(int x = 0, int y = 0) :x(x), y(y) {
		count++;
	}
	point(point& p) {
		x = p.x;
		y = p.y;
		count++;
	}
	~point() { count--; }
	int getx() { return x; }
	int gety() { return y; }

	void showcount() { cout << "count" << count; }
private:
	int x, y;
	static int count;
};
//静态数据成员
int point::count=0;
int main() {
	point p(4, 5);
	cout << p.getx() <<p.gety()<< endl;
	p.showcount(); 
	point b(p);
	b.showcount();
	return 0;
}

5.3.2静态函数成员

静态成员函数可以直接访问类的静态数据和函数成员,非静态成员,必须通过对象名

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class point {
public:
	point(int x = 0, int y = 0) :x(x), y(y) {
		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 << "count:" << count << endl;
	}
private:
	int x, y;
	static int count;
};
int point::count = 0;
int main() {
	point a(4, 5);
	cout << a.getx() << a.gety();
	point::showcount();

	point b(a);
	cout << b.getx() << b.gety();
	point::showcount();

}

5.4类的友元

不同类或者对象的成员函数之间,类的成员函数与一般函数之间进行数据共享的机制。

5.4.1友元函数

friend修饰的非成员函数

可以通过对象名来访问类的私有和保护成员

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
using namespace std;
class 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 << dist(myp1, myp2) << endl;
}

5.4.2友元类

A类为B类的友元类,则A类的所有成员函数都是B类的友元函数,都可以访问b的私有和保护成员

注意:

1友元关系不能传递

2友元关系是i单向的

3友元关系不能被继承

5.5共享数据的保护

防止改变数据应声明为常量

5.5.1常对象

必须进行初始化

改变对象的数据成员有两个途径

1通过对象名访问其成成员对象

2在类的成员函数中改变数据成员的值,不能通过常对象调用普通的成员函数

5.5.2const修饰的类成员

1常函数成员

类型说明        函数名(参数列表)const

如果说明一个对象是常对象,只能调用常成员函数

const可以用于对重载函数的区分

void print():

void print()   const

通过非const对象调用该函数,优先非const

在调用常成员函数期间,目的对象都被视为常对象

#define _CRT_SECURE_NO_WARNINGS
#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 << r2 << "   " << r1;
}
int main() {
	r a(5, 4);
	a.print();
	const r b(5, 4);
	b.print();
}
2常数据成员
#define _CRT_SECURE_NO_WARNINGS
#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 a1(100), a2(0);
	a1.print();
	a2.print();
}

5.5.3常引用

常引用所引用的对象不能再更新

const 类型说明符 & 引用名

常引用的对象只能当作常对象

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
using namespace std;
class 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(4, 5);
	cout << dist(myp1, myp2) << endl;
}

5.6多文件结构和编译预处理命令

5.6.1C++程序一般组织结构

一个项目三个文件:类定义文件,类实现文件,类的使用文件

头文件

class point {
public:
	point(int x=0,int y=0):x(x),y(y){}
		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;
};

point.cpp

#define _CRT_SECURE_NO_WARNINGS
#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 <<  count << endl;
}

1.cpp

#define _CRT_SECURE_NO_WARNINGS
#include"point.h"
#include<iostream>
using namespace std;

int main() {
	point a(4, 5);
	cout << a.getx() << a.gety() << endl;
	point::showcount();
	point b(a);
	cout << b.getx() << b.gety() << endl;
	point::showcount();
}

5.6.2外部变量与外部函数

1外部变量

exter int i;

2外部函数

有无extern都一样,所有类之外的函数,都具有命名空间作用域的

3将变量与函数限制在编译单元内

static

在局部作用域,类作用域和命名空间作用域

作用差不多都具有静态生存期

5.6.3C++库

5.6.4编译预处理

1#include指令
2#define与#undef指令

#undef用来删除#define定义的宏

3条件编译指令

(1)#if        常量表达式

程序1

#endif

(2)#if        常量表达式

程序1

#else        常量表达式

程序2

#endif

(3)#if        常量表达式

程序1

#elif        常量表达式

程序2

#elif        常量表达式

程序3

#else

程序n+1

#endif

(4)#ifdef 标识符

程序1

#else

程序2

#end00if

如果标识符经#defind定义而且没有被#undef删除编译程序1,否则程序2,如果没有程序2则不需要#else

(5)#ifndef     标识符

程序1

#else

程序2

#endif

如果标识符没有被定义执行1,否则执行2

4defined操作符(预处理)不是指令

defined(标识符)

如果标识符被#define定义而且没有被#undef删除,表达式为非零,否则为0;

两个写法等价

#iifndef   MYHEAD_H

#define MYHEDA_H

.......

#endif

等价

#if!defined(MYHEAD_H)

#define MYHEAD_H

........

#endif

例子

由于文件包含指令可以嵌套使用,在设计程序时要避免多次重复包含同一个头文件,否则会引起变量与类的重复定义

//main.cpp

#include"file1.h"

#include"file2.h"

int main(){

...}

//file1.h

#include"head.h"

....

//file2.h

#include"head.h"

....

//head.h

....

class point {

...

}

...

#endif

  • 24
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值