最新内蒙古科技大学面向对象的程序设计实验6-静态成员和友元
- 格式:docx
- 大小:244.29 KB
- 文档页数:18
实验五静态成员、友元、对象成员1、实验目的:1)掌握静态数据成员的定义及使用;2)掌握静态成员函数的定义及使用。
3)掌握友员函数的定义和使用。
4)理解对象成员的概念5)掌握对象成员的初始化方法2、实验内容:2.1 分析下面的程序,指出程序运行的结果。
1) P132习题3.14,3.15,3.20,3.23,3.24,3.25,3.26代码:3.14#include<iostream.h>class B{public:B(){}B(int i,int j){x=i;y=j;}void printb(){cout<<x<<","<<y<<endl;}private:int x,y;};class A{public:A(){}A(int I ,int j);void printa();private:B c;};A::A(int i,int j):c(i,j){}void A::printa(){c.printb();}int main(){A a(7,8);a.printa();return;}程序运行结果:7,83.15程序代码:#include<iostream.h>class A{public:void set(int i,int j){x=i;y=j;}int get_y(){return y;}private:int x,y;};class box{public:void set(int l,int w,int s,int p){length=l;width=w;label.set(s,p);}int ger_area(){return length*width;}private:int length,width;A label;};int main(){box b;b.set(4,6,1,20);cout<<b.ger_area()<<endl;return 0;}程序运行结果:243.20程序代码:#include<iostream.h>class aclass{public:aclass(){total++;}~aclass(){total--;}int gettotal(){return total;}private:static int total;};int aclass::total=0;int main(){aclass o1,o2,o3;cout<<o1.gettotal()<<"objects in existence\n"; aclass *p;p=new aclass;if(!p){cout<<"Allocation error\n";return 1;}cout<<o1.gettotal();cout<<"objects in existence after allocation\n"; delete p;cout<<o1.gettotal();cout<<"objects in existence after allocation\n"; return 0;}程序运行结果:3 objects in existence4 objects in existence after allocation 3 objects in existence after allocation3.24程序代码:#include<iostream.h>class N{private:int A;static int B;public:N(int a){A=a;B+=a;}static void f1(N m);};void N::f1(N m){cout<<"A="<<m.A<<endl;cout<<"B="<<B<<endl;}int N::B=0;int main(){N P(5),Q(9);N::f1(P);N::f1(Q);return 0;}程序运行结果:A=5B=14A=9B=143.26程序代码:#include<iostream.h>class M{int A;static int B;public:M(int a){A=a;B+=a;cout<<"Constructing"<<endl;}static void f1(M m);~M(){cout<<"Destructing\n";}};void M::f1(M m){cout<<"A="<<m.A<<endl;cout<<"B="<<B<<endl;}int M::B=0;int main(){M P(5),Q(9);M::f1(P);M::f1(Q);return 0;}程序运行结果:ConstructingConstructingA=5B=14DestructingA=9B=14DestructingDestructingDestructing2)#include<iostream.h>class Sample{int a,b;static int c;public:Sample(){a=b=0;c++;}void show(){cout<<"a="<<a<<','<<"b="<<b<<','<<"c="<<c<<endl;}};int Sample::c=1;void main(){Sample a1,a2;a1.show();a2.show();}程序运行结果如下:a=0,b=0,c=3a=0,b=0,c=33)#include<iostream.h>#include<stdio.h>class B{private:int a,b;public:B(){}B(int i,int j){a=i;b=j;}void showB(){cout<<"a="<<a<<","<<'\t'<<"b="<<b<<endl;}};class A{private:B c;public:A(){}A(int i,int j):c(i,j){}void showA(){c.showB();}};void main(){A a1(5,6);a1.showA();}程序运行结果:a=5,b=62.2编写并调试程序。
实验六静态成员与友元实验六静态成员与友元【实验目的】1.掌握友元函数的定义、使用方法以及特点。
2.握静态成员函数和静态数据成员的功能。
【实验内容】1.调试下列程序,写出输出结果,并分析输出结果。
#include "iostream.h"class My{public:My(int aa){A=aa;B-=aa;}static void fun(My m);private:int A;static int B;};void My::fun(My m){cout<<"A="<<m.a<<endl;< bdsfid="86" p=""></m.a<<endl;<>cout<<"B="<<b<<endl;< bdsfid="88" p=""></b<<endl;<> }int My::B=100;int main(){My P(6),Q(8);My::fun(P);Q.fun(Q);return 0;}2.设计一个含有静态成员、静态成员函数的程序,分析程序结果,理解静态成员(函数)与类和对象的关系。
参考程序:#includeclass A{friend class B;//友元类的声明public:void Set(int i){x=i;}friend int add(A & f1);// 友元函数的声明void Display()cout<<"x="<<x<<",y="<<y<<endl;< bdsfid="110" p=""></x<<",y="<<y<<endl;<>}private:int x;static int y;};int add(A & f1){return f1.x+1;}class B{public:B(int i,int j);void Display();private:A a;//A类的对象a作为类B的私有成员};int A::y=1;//静态成员必须先赋初值B::B(int i,int j){a.x=i;A::y=j;}void B::Display(){cout<<"x="<<a.x<<",y="<<a::y<<endl;< bdsfid="135" p=""></a.x<<",y="<<a::y<<endl;<>}void main(){A b;b.Set(5);//友元函数的调用cout<<add(b)<<endl;< bdsfid="142" p=""></add(b)<<endl;<>b.Display();B c(6,9);//a.x=6,X::y=9;c.Display();b.Display();}问题:⑴、分析友元函数add()的定义、调用与成员函数的区别。
内蒙古科技大学面向对象的程序设计实验报告一、实验目的(1) 掌握声明类的方法,类和类的成员的概念以及定义对象的方法。
(2) 初步掌握用类和对象编制基于对象的程序。
(3) 学习检查和调试基于对象的程序。
二、实验环境编译器:Visual C++ 6.0.操作系统:Windows 7 旗舰版三、实验内容2.1练习(一):1. 新建一个项目。
2. 输入以下程序:#include <iostream>using namespace std;class Time{public: int hour; int minute; int sec;};int main(){Time t1;cin >> t1.hour;cin >> t1.minute;cin >> t1.sec;cout << t1.hour << “:”<< t1.minute << “:” << t1.sec <<endl;return 0;}3. 按要求编写程序,生成后执行,并分析输出的信息。
按要求改写程序:1. 将数据成员改为私有的;2. 将输入输出的功能改为由成员函数实现;3. 在类体内定义成员函数。
4. 编译和运行程序。
请分析什么成员应指定为公用的?什么成员应指定为私有的?什么函数最好放在类中定义?什么函数最好放在类外定义?2.2练习(二):1. 新建一个项目。
2. 在项目中,添加头文件student.h,并输入以下代码。
class Student{public:void display();private:int num; char name[20];char sex;};3. 在项目中,添加源文件student.cpp,包含成员函数定义。
#include <iostream>#include “student.h”void Student::display(){cout << “num:” << num << endl;cout << “name:” << name << endl;cout << “sex:” << sex << endl;}4. 在包含主函数的源文件main.cpp,添加如下代码。
面向对象程序设计实验指导书(适用:电子信息11级)彭召意陶立新编写计算机与通信学院2014.9目录实验一 C++基础的应用 (1)实验二类和对象的应用 (3)实验三类的构造函数、析构函数的应用 (4)实验四友员和运算符重载 (5)实验五类的继承与派生 (6)实验六类的多态性与虚函数 (7)附录:各实验的程序代码 (8)实验一 C++基础的应用(实验课时:2 实验性质:设计)实验名称: C++基础的应用实验目的: (1)进一步学习VC++6.0开发环境及程序调试方法。
(2)练习C++函数的定义及使用;(3)练习C++数组的定义及使用;(4)练习C++指针的定义及使用;(5)练习C++结构体的定义及使用;(6)练习多文件的程序的编译和运行方法;实验设备:(1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual C++6.0。
实验内容: (1)熟悉Visual C++6.0编译系统的常用功能,特别是debug调试功能;(2)编程1:编写一个程序c1.cpp,用来求2个或3个整数的最大数。
要求:用重载函数的方法来求最大数;函数原型:int max( int a, int b) 和int max( int a, int b,int c)。
(3)编程2:编写一个程序c2.cpp,求:a!+ b! + c!的值。
要求:使用递归函数。
主程序和函数分开到两个源程序文件中,分别进行编译后,再运行;(4)编程3:有一个3*4的矩阵,要求编程求出其中值最大的那个元素的值,以及其所在的行号和列号;(5)编程4:建立一个动态链表并进行输出和删除管理。
链表的每个节点为学生信息,包括:学号,姓名,性别,下一学生信息的指针。
程序的工作:(a)建立三个学生信息的节点,然后顺序输出该三个学生信息;(b)删除中间的节点,再顺序输出学生信息。
内蒙古科技大学面向对象的程序设计实验报告一、实验目的1.理解静态成员(静态数据成员、静态成员函数)的作用与使用;2.理解友元(友元函数、友元类)的作用于使用。
二、实验环境编译器:Visual C++ 6.0.操作系统:Windows 7旗舰版三、实验内容二、实验内容2.1练习(一):1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include <iostream.h>#include <string.h>class CStudent{public:CStudent(char *n, int a);~CStudent();static void SetAge(int age);private:char *name;int age;static int nTotalObj;};int CStudent::nTotalObj = 0;CStudent::CStudent(char *n, int a):age(a){int nLen = strlen(n);name = new char[nLen+1];strcpy(name,n);name[nLen] = ’\0’;nTotalObj++;}CStudent::~CStudent(){delete[] name;nTotalObj--;}void CStudent::SetAge(int age){this->age = age;}void main(){CStudent stu1("张三",25);CStudent str2("李四",26);cout<<"CStudent::nTotalObj="<<CStudent::nTotalObj<<endl;}问题一:以上程序编译能通过吗,为什么?问题二:成员变量nTotalObj在程序中起什么作用,它是如何实现的?问题三:如果规定该程序的主函数和类CStudent中的成员变量的属性不允许改变,应该如何改正该程序?2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
内蒙古科技大学面向对象的程序设计实验报告一、实验目的1.理解静态成员(静态数据成员、静态成员函数)的作用与使用;2.理解友元(友元函数、友元类)的作用于使用。
二、实验环境编译器:Visual C++ 6.0.操作系统:Windows 7旗舰版三、实验内容二、实验内容2.1练习(一):1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include <iostream.h>#include <string.h>class CStudent{public:CStudent(char *n, int a);~CStudent();static void SetAge(int age);private:char *name;int age;static int nTotalObj;};int CStudent::nTotalObj = 0;CStudent::CStudent(char *n, int a):age(a){int nLen = strlen(n);name = new char[nLen+1];strcpy(name,n);name[nLen] = ’\0’;nTotalObj++;}CStudent::~CStudent(){delete[] name;nTotalObj--;}void CStudent::SetAge(int age){this->age = age;}void main(){CStudent stu1("张三",25);CStudent str2("李四",26);cout<<"CStudent::nTotalObj="<<CStudent::nTotalObj<<endl;}问题一:以上程序编译能通过吗,为什么?问题二:成员变量nTotalObj在程序中起什么作用,它是如何实现的?问题三:如果规定该程序的主函数和类CStudent中的成员变量的属性不允许改变,应该如何改正该程序?2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include <iostream.h>#include <string.h>class CStudent{public:CStudent(char *n, int a);~CStudent();private:char *name;int age;};CStudent::CStudent(char *n, int a) :age(a){int nLen = strlen(n);name = new char[nLen+1];strcpy(name,n);name[nLen] = ’\0’;}CStudent::~CStudent(){delete[] name;}class CTeacher{public:CTeacher(char *tn, int ta);~CTeacher();void SetStuAge(int a);private:char *name;int age;CStudent stu;};CTeacher::CTeacher(char *tn, int ta) :age(ta){int nLen = strlen(tn);name = new char[nLen+1];strcpy(name,tn);name[nLen] = ’\0’;}CTeacher::~CTeacher(){delete[] name;}void CTeacher::SetStuAge(int a){stu.age = a;}void main(){CStudent stu("张三",25);CTeacher tea("李四",26);}问题一:以上程序有两大错误,试指出来,并改正之?2.2练习(二):1.某商店经销一种货物。
货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,因此,商店需要记录下货物库存的总重量。
试用C++模拟商店货物购进和卖出的情况。
(提示:将总重量定义为静态成员)四、实验过程练习1 题目提供源代码如下编译结果如下分析之后得出结论首先静态成员函数SetAge中不可以使用指针,也不可以直接使用非静态数据成员age 此处应该使用对象名或者类名间接引用age其次nTotalObj为私有属性,不可以直接访问,且题目要求不可以修改其属性,则添加一个成员函数,使其返回值等于nTotalObj,则代替了原先nTotalObj的功能修改之后的代码如下然后再次编译运行结果如下运行结果正确,且不影响nTotalObj的计数器功能,代码修改正确练习2题目提供源代码如下编译之后结果如下分析错误原因有两点错误1 CTeacher类中的成员函数SetStuAge不能直接访问CStudent类内嵌成员stu的数据成员age错误2 CTeacher类构造函数的初始化含有错误,内嵌对象stu的数据成员也应该包含在内修改之后代码如下运行结果如下成功运行,修改正确练习3根据题意,所编写代码如下尝试测试运行结果该代码包含题目要求的所有功能,运行完毕五、实验总结实验源代码:练习(一)//修改之后代码#include <iostream.h>#include <string.h>class CStudent{public:CStudent(char *n, int a);~CStudent();static void SetAge(CStudent stuN);static int CSnTota10bj();private:char *name;int age;static int nTotalObj;};int CStudent::nTotalObj = 0;CStudent::CStudent(char *n, int a):age(a){int nLen = strlen(n);name = new char[nLen+1];strcpy(name,n);name[nLen] = ’\0’;nTotalObj++;}CStudent::~CStudent(){delete[] name;nTotalObj--;}int CStudent::CSnTota10bj(){int n;n=nTotalObj;return n;}void CStudent::SetAge(CStudent stuN){int a;stuN.age = a;}void main(){CStudent stu1("张三",25);CStudent str2("李四",26);cout<<"CStudent::nTotalObj="<<CStudent::CSnTota10bj()<<endl; }练习(二)#include <iostream.h>#include <string.h>class CStudent{public:CStudent(char *n, int a);~CStudent();private:char *name;public:int age;};CStudent::CStudent(char *n, int a):age(a){int nLen = strlen(n);name = new char[nLen+1];strcpy(name,n);name[nLen] = ’\0’;}CStudent::~CStudent(){delete[] name;}class CTeacher{public:CTeacher(char *tn,int ta,char *sn,int sa);~CTeacher();void SetStuAge(int a);private:char *name;int age;CStudent stu;};CTeacher::CTeacher(char *tn,int ta,char *sn="noname",int sa=0) :age(ta),stu(sn,sa){int nLen = strlen(tn);name = new char[nLen+1];strcpy(name,tn);name[nLen] = ’\0’;}CTeacher::~CTeacher(){delete[] name;}void CTeacher::SetStuAge(int a){stu.age = a;}void main(){CStudent stu("张三",25);CTeacher tea("李四",26);}/*错误1 CTeacher类中的成员函数SetStuAge不能直接访问CStudent类内嵌成员stu的数据成员age错误2 CTeacher类构造函数的初始化含有错误,内嵌对象stu的数据成员也应该包含在内*/练习(三)#include <iostream.h>class Goods{private:int weight;static int totalWeight;public:Goods(int wei);static void PrintTotalWeight();void sell();void buy();};int Goods::totalWeight = 0;Goods::Goods(int twei){totalWeight = twei;cout<<"初始总重量为:"<<totalWeight<<endl;}void Goods::PrintTotalWeight(){cout<<"总重量为:"<<Goods::totalWeight<<endl;}void Goods::buy(){cout<<"购入"<<endl;cout<<"请输入购入货物的重量:";cin>>weight;Goods::totalWeight += weight;weight= 0;}void Goods::sell(){cout<<"售出"<<endl;cout<<"请输入售出货物的重量:";cin>>weight;Goods::totalWeight -= weight;weight= 0;}void main(){int twei=0;int swit=1;cout<<"请设置初设总重量"<<endl;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;}}}六、参考文献参考的资料:C++面向对象程序设计。