现在的位置: 首页 > 综合 > 正文

[C++]实验六:类的静态成员的使用、多文件结构在C++程序中的使用

2013年02月26日 ⁄ 综合 ⁄ 共 4201字 ⁄ 字号 评论关闭

/*
1. 定义一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,
有两个公有成员函数run、stop。其中,rank为枚举类型CPU_Rank,
定义为enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7},frequency为单位是MHz的整型数,
voltage为浮点型的电压值。观察构造函数和成员函数的调用顺序。
*/

/*
2.定义一个Employee类,其中包括姓名、街道地址、城市和邮编等属性,
以及change_name()和display()等函数。display()显示姓名、街道地址、城市和邮编等属性,
change_name()改变对象的姓名属性,实现并测试这个类。
要求:
在employee.h文件中定义Employee类。Employee类具有姓名、街道地址、城市和邮编等私有数据成员,
请分别用string类型和char*类型来表示。在主程序中定义这个类的对象并对其进行操作。
*/

#include <iostream>
#include "Employee.h"

void main()
{
 Employee new_person ("mjm","xixia","nanjin",210046);
 new_person.change_name("Jiaming Mao");
 new_person.display();
}

/*
3. 实现客户机(Client)类。定义静态数据成员ServerName,
保存其服务器名称;整型静态数据成员ClientNum,记录已定义的客户数量;
定义静态函数ChangeServerName()改变服务器名称。在头文件client.h中定义类,
在文件client.cpp中实现,在文件test.cpp中测试这个类,观察相应的成员变量取值的变化情况。
要求:
新建一个空的项目lab6_3,添加头文件client.h,在其中定义类Client,
再添加源程序文件client.cpp,在其中实现Client类,注意静态成员变量的使用方法;
再添加文件lab6_3.cpp,在其中定义main()函数,测试Client类,观察相应的成员变量取值的变化情况。
*/

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

void main()
{
 Client NewTest1;
 Client NewTest2;
 cout << NewTest1.ServerName << endl;
 Client::ChangeServerName ( "Server 1" );
 cout << NewTest1.ServerName << endl;
 cout << NewTest2.ServerName << endl;
 cout << Client::Num() << endl;
}

/*
4.设计一个日期类Date,尽量完善该类提供的操作,然后设计主函数main( )演示日期类的用法。
(提示:日期有不同的显示格式,如中国为yy.mm.dd,美国为mm.dd.yy,欧洲为dd.mm.yy,
甚至中间的分隔符也有“。”、“/”、“-”等多种。由于多个日期对象均应采用共同的格式,
所以这一属性可采用静态数据成员表示)
*/

#include "Date.h"

void main()
{
 Date today;
 today.CHtime_display(); //显示中国格式的时间
 Date::set_dd(40); //设置日期40,返回error信息
 Date::set_dd(20); //设置日期20,正确
 Date::change_joint_mark(2); //改变中间分隔符样式为2
 today.EUtime_display(); //显示欧洲格式的时间
 Date::set_yy(2006); //设置年份为2006
 Date::set_mm(12); //设置月份为12
 Date::change_joint_mark(3); //改变中间分隔符样式为3
 today.UStime_display(); //显示美国格式的时间
}

//client.cpp

#include "client.h"

int Client::ClientNum = 0;
std::string Client::ServerName = "No Server";

void Client::ChangeServerName( std::string NewName )
{
  ServerName = NewName;
}

//client.h

#include <string>

class Client {
public:
 Client (){ ClientNum++; };
 static int Num (){ return ClientNum; };
 static std::string ServerName;
 static void ChangeServerName( std::string NewName );
private:
 static int ClientNum;
};

//CPU.cpp
#include <iostream>
using namespace std;

enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
private:
 CPU_Rank rank;
 int frequency;
 float voltage;
public:
   CPU (CPU_Rank r, int f, float v)
 {
  rank = r;
  frequency = f;
  voltage = v;
  cout << "构造了一个CPU!" << endl;
}
~CPU () { cout << "析构了一个CPU!" << endl; }

    CPU_Rank GetRank() const { return rank; }
    int GetFrequency() const { return frequency; }
  float GetVoltage() const { return voltage; }

    void SetRank(CPU_Rank r) { rank = r; }
    void SetFrequency(int f) { frequency = f; }
    void SetVoltage(float v) { voltage = v; }

    void Run() {cout << "CPU开始运行!" << endl; }
  void Stop() {cout << "CPU停止运行!" << endl; }
};

void main()
{
 CPU a(P6,300,2.8);
 a.Run();
 a.Stop();
}

//Date.cpp

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

int Date::yy = 2005;
int Date::mm = 11;
int Date::dd = 19;
char Date::joint_mark = '/';

void Date::change_joint_mark (int mark)
{
 switch (mark)
 {
 case 1 :joint_mark = '/';
  break;
 case 2 :joint_mark = '-';
  break;
 case 3 :joint_mark = '/';
  break;
 }
}

void Date::set_dd ( int val )
{
 if ( val >=1 && val <= 31 )
  dd = val;
 else
  cout << "error of setting day./n";
}

void Date::set_yy ( int val )
{
  yy = val;
}

void Date::set_mm ( int val )
{
 if ( val >=1 && val <= 12 )
  mm = val;
 else
  cout << "error of setting mmonth./n";
}

void Date::CHtime_display ()
{
 cout << yy << joint_mark << mm << joint_mark << dd << endl;
}

void Date::UStime_display ()
{
 cout << mm << joint_mark << dd << joint_mark << yy << endl;
}

void Date::EUtime_display ()
{
 cout << dd << joint_mark << mm << joint_mark << yy << endl;
}

//Date.h

class Date {
public:
 void static set_yy ( int ) ;
 void static set_mm ( int ) ;
 void static set_dd ( int ) ;
 void CHtime_display();
 void UStime_display();
 void EUtime_display();
 void static change_joint_mark(int);
private:
 static char joint_mark;
 static int yy;
 static int mm;
 static int dd;
};

//Employee.h

#include <string>
using namespace std;

class Employee{
public:
 Employee (char* name, char* street_name, char* city_name, int postal_code)
  :_name(name), _street_name(street_name), _city_name(city_name), _postal_code(postal_code){};
 void display();
 void change_name(string);
private:
 string _name;
 string _street_name;
 string _city_name;
 int _postal_code;
};

inline void Employee::display()
{
 cout << _name << "/n"
   << _street_name << "/n"
   << _city_name << "/n"
   << _postal_code << endl;
}

inline void Employee::change_name(string new_name)
{
 _name = new_name;
}

【上篇】
【下篇】

抱歉!评论已关闭.