C++实验4——C++程序的结构

大一写的代码,很菜

目录

实验目的:

实验任务:

实验步骤:

参考代码:


实验目的:

  1. 观察程序运行中变量的作用域、生存期和可见性;
  2. 学习类的静态成员的使用;
  3. 学习多文件结构在C++程序中的使用;

实验任务:

  1. 运行下面的程序,观察变量x、y的值。

//lab5_1.cpp

#include <iostream.h>

void fn1();

int x = 1, y = 2;

int main()

{

     cout << "Begin..." << endl;

cout << "x = " << x << endl;

cout << "y = " << y << endl;

     cout << "Evaluate x and y in main()..." << endl;

     int x = 10, y = 20;

cout << "x = " << x << endl;

cout << "y = " << y << endl;

     cout << "Step into fn1()..." << endl;

fn1();

cout << "Back in main" << endl;

cout << "x = " << x << endl;

cout << "y = " << y << endl;

return 0;

}

void fn1()

{

    int y = 200;

    cout << "x = " << x << endl;

    cout << "y = " << y << endl;

}

2.实现客户机(CLIENT)类。定义字符型静态数据成员ServerName,保存其服务器名称;整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。在头文件client.h中定义类,在文件client.cpp中实现,在文件test.cpp中测试这个类,观察相应的成员变量取值的变化情况。

实验步骤:

  1. 运行lab5_1程序,观察程序输出。
    全局变量的作用域为文件作用域,在整个程序运行期间有效,但如果在局部模块中定义了同名的变量,则在局部模块中,可见的是局部变量,此时,全局变量不可见;而局部变量的生存期只限于相应的程序模块中,离开相应的程序模块,局部变量x、y就不再存在,此时同名的全局变量重新可见。
  2. 新建一个空的项目lab5_2,添加头文件client.h,在其中定义类CLIENT,注意使用编译预处理命令;再添加源程序文件client.cpp,在其中实现CLIENT类,注意静态成员变量的使用方法;再添加文件lab5_cpp,在其中定义main()函数,测试CLIENT类,观察相应的成员变量取值的变化情况。

参考代码:

//实验4.1 运行下面的程序,观察变量x、y的值。(原程序是错的) 
//lab5_1.cpp
#include <iostream>
using namespace std;
void fn1();
int x = 1, y = 2;

int main()
{
	cout << "Begin..." << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "Evaluate x and y in main()..." << endl;
	int x = 10, y = 20;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "Step into fn1()..." << endl;
	fn1();
	cout << "Back in main" << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	return 0;
}
void fn1()
{
	int y = 200;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
}

/*实验4.2 实现客户机(CLIENT)类。定义字符型静态数据成员ServerName,保存其服务器名称;
整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。
在头文件client.h中定义类,在文件client.cpp中实现,在文件test.cpp中测试这个类,观察相应的成员变量取值的变化情况。*/
//client.h
#ifndef CLIENT_H //课本175页编译预处理,其实不加也可以?
#define CLIENT_H
#include<iostream>
using namespace std;
class CLIENT {
private:
	static char ServerName;
	static int ClientNum;
public:
	static void showname();
	static void shownum();
    static void ChangeServerName(char n);
	CLIENT();
	~CLIENT();
	CLIENT(CLIENT& c);
};
#endif

/*实验4.2 实现客户机(CLIENT)类。定义字符型静态数据成员ServerName,保存其服务器名称;
整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。
在头文件client.h中定义类,在文件client.cpp中实现,在文件test.cpp中测试这个类,观察相应的成员变量取值的变化情况。*/
//client.cpp
#include<iostream>
#include"client.h"
using namespace std;
CLIENT::CLIENT() {
	ClientNum++;
	cout << "调用构造函数,增加一个客户" << endl;
}
CLIENT::~CLIENT() {
	ClientNum--;
	cout << "调用析构函数,减少一个客户" << endl;
}
CLIENT::CLIENT(CLIENT& c) {
	ServerName = c.ServerName;
	ClientNum = c.ClientNum;
}
void CLIENT::ChangeServerName(char c) {
	ServerName = c;
	cout << "服务器名称改变为" << ServerName << endl;
}
void CLIENT::showname() {
	cout << "服务器名称:" << ServerName << endl;
}
void CLIENT::shownum() {
	cout << "客户数量:  " << ClientNum << endl;
}
char CLIENT::ServerName = 'S';
int CLIENT::ClientNum = 0;
/*实验4.2 实现客户机(CLIENT)类。定义字符型静态数据成员ServerName,保存其服务器名称;
整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。
在头文件client.h中定义类,在文件client.cpp中实现,在文件test.cpp中测试这个类,观察相应的成员变量取值的变化情况。*/
//test.cpp
#include<iostream>
#include"client.h"
using namespace std;
int main() {
	CLIENT::showname();//用类名限制以输出初始状态,下同。
	CLIENT::shownum();
	CLIENT::ChangeServerName('d');
	CLIENT a;//增加一个客户
	a.showname();
	a.shownum();
	CLIENT b;//增加一个客户
	b.ChangeServerName('L'); 
	b.showname();
	b.shownum();
	a.~CLIENT();//减少一个客户
	CLIENT::showname();
	CLIENT::shownum();
	return 0;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值