内蒙古科技大学信息工程学院计算机系
《面向对象程序设计》实验报告
姓名 班级 项目号、实验名称 一、实验目的 1.理解静态成员(静态数据成员、静态成员函数)的作用与使用; 2.理解友元(友元函数、友元类)的作用于使用。 二、实验内容 2.1练习(一): 1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。 #include #include 实 验 要 求 (任课 教 师 提 供) class CStudent { public: private: }; int CStudent::nTotalObj = 0;1
学号 实验日期 第 九 周(星期四) 4 月 25 日第 四 节 实验6 静态成员和友元 CStudent(char *n, int a); ~CStudent(); static void SetAge(int age); char *name; int age; static int nTotalObj; CStudent::CStudent(char *n, int a) :age(a) { } CStudent::~CStudent() { } void CStudent::SetAge(int age) { } void main() { } CStudent stu1(\"张三\CStudent str2(\"李四\cout<<\"CStudent::nTotalObj=\"<age = age; nTotalObj--; delete[] name; nTotalObj++; int nLen = strlen(n); name = new char[nLen+1]; strcpy(name,n); name[nLen] = '\\0'; 2问题一:以上程序编译能通过吗,为什么? 问题二:成员变量nTotalObj在程序中起什么作用,它是如何实现的? 问题三:如果规定该程序的主函数和类CStudent中的成员变量的属性不允许改变,应该如何改正该程序? 2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。 #include #include class CStudent { public: private: }; CStudent::CStudent(char *n, int a) :age(a) { } CStudent::~CStudent() { delete[] name; int nLen = strlen(n); name = new char[nLen+1]; strcpy(name,n); name[nLen] = '\\0'; char *name; int age; CStudent(char *n, int a); ~CStudent(); 3} class CTeacher { public: void SetStuAge(int a); CTeacher(char *tn, int ta); ~CTeacher(); private: }; CTeacher::CTeacher(char *tn, int ta) :age(ta) { } CTeacher::~CTeacher() { } delete[] name; int nLen = strlen(tn); name = new char[nLen+1]; strcpy(name,tn); name[nLen] = '\\0'; CStudent stu; char *name; int age; 4
void CTeacher::SetStuAge(int a) { } void main() { } 问题一:以上程序有两大错误,试指出来,并改正之? 2.2练习(二): 1.某商店经销一种货物。货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,因此,商店需要记录下货物库存的总重量。试用C++模拟商店货物购进和卖出的情况。(提示:将总重量定义为静态成员) CStudent stu(\"张三\CTeacher tea(\"李四\stu.age = a; 5
练习一: #include #include using namespace std; class CStudent { public: 实 验 内 容 (由学 生 填 写) CStudent(const char *n, int a); ~CStudent(); //析构函数 //static void SetAge(CStudent stuN); void SetAge(int age); static int CSnTotalObj(); private: }; int CStudent::nTotalObj = 0; //静态数据成员的初始化,只能在类体外初始化。 CStudent::CStudent(const char *n, int a):age(a) { } CStudent::~CStudent() { delete[] name; int nLen = strlen(n); name = new char[nLen + 1]; strcpy_s(name,20,n); name[nLen] = '\\0'; nTotalObj++; char *name; int age; static int nTotalObj; //定义静态数据成员 6} int CStudent::CSnTotalObj() { } //void CStudent::SetAge(CStudent stuN) void CStudent::SetAge(int age) { } void main() { } 练习二: #include #include class CStudent { public: CStudent(const char *n, int a); CStudent stu1(\"张三\CStudent str2(\"李四\cout << \"CStudent::nTotalObj=\" << CStudent::CSnTotalObj() << endl; //int a = 0; this->age = age; //静态成员函数没有this指针 //stuN.age = a; int n; n = nTotalObj; return n; nTotalObj--; 7~CStudent(); int age; friend class CTeacher; private: }; CStudent::CStudent(const char *n, int a) { } CStudent::~CStudent() { } class CTeacher { public: CTeacher(const char *tn, int ta,const char* sn, int sa); ~CTeacher(); void SetStuAge(int a); delete[] name; int nLen = strlen(n); name = new char[nLen + 1]; strcpy_s(name,20, n); name[nLen] = '\\0'; :age(a) char *name; //int age; private: char *name; int age; CStudent stu; 8
}; CTeacher::CTeacher(const char *tn, int ta,const char* sn = \"noname\:age(ta),stu(sn,sa) { } CTeacher::~CTeacher() { } void CTeacher::SetStuAge(int a) { } void main() { } 练习三: #include using namespace std; class Goods {public: Goods(int wei); static void PrintTotalWeight(); CStudent stu(\"张三\CTeacher tea(\"李四\stu.age = a; delete[] name; int nLen = strlen(tn); name = new char[nLen + 1]; strcpy_s(name,20,tn); name[nLen] = '\\0'; 9void sell(); void buy(); private: }; int Goods::totalWeight = 0; Goods::Goods(int twei) { } void Goods::PrintTotalWeight() { } void Goods::buy() { } void Goods::sell() { cout << \"售出:\" << endl; cout << \"请输入售出货物的重量:\"; cin >> weight; cout << \"购入:\" << endl; cout << \"请输入购入货物的重量:\" << endl; cin >> weight; Goods::totalWeight += weight; weight = 0; cout << \"总重量为:\" << Goods::totalWeight << endl; totalWeight = twei; cout << \"初总重量:\" << totalWeight << endl; int weight; static int totalWeight; 10
} Goods::totalWeight -= weight; weight = 0; void main() { } int twei = 0; int swit = 1; cout << \"请设置初始总重量:\"; cin >> twei; Goods goods1(twei); while (swit) { } cout << \"请选择操作类型:0.退出 1.购入 2.售出\" << endl; cin >> swit; switch (swit) { case 0: cout << endl; break; case 1:goods1.buy(); goods1.PrintTotalWeight(); cout << endl; break; case 2:goods1.sell(); } goods1.PrintTotalWeight(); cout << endl; break; 11
实验结果: 练习一: CStudent::nTotalObj=2 练习二(改错): //CTeacher类中的成员函数SetStuAge不能直接访问CStudent类内嵌成员stu的数据成员age //CTeacher类构造函数的初始化含有错误,内嵌对象stu的数据成员也应该包含在内 实 验 总 结 (由学 生 填 写) 练习三: 请设置初始总重量:100 初总重量:100 请选择操作类型:0.退出 1.购入 2.售出 1 购入: 请输入购入货物的重量: 10 总重量为:110 请选择操作类型:0.退出 1.购入 2.售出 2 售出: 请输入售出货物的重量:90 总重量为:20 说明:
1、每个实验项目填写一份实验报告,电子版命名方式为:学号姓名项目号.doc。例如:1167111182张三3.doc表示张三做的第3个项目的实验报告。
2、实验报告电子版应该在实验后一周内提交到教务处网站的网络综合平台对应课程下。 3、任课教师要求在收到实验报告的一周内进行批阅,并给出成绩及评语。 4、实验报告电子版由教务处网站服务器保存。 5、表格宽度可以根据实际情况伸缩。
12