一、对象数组
对象数组中的每一个元素都是同类的对象;
例1 对象数组成员的初始化
- #include<iostream>
- using namespace std;
- class Student
- {
- public:
- Student( ){ };
- Student(int n,string nam,char s):num(n),name(nam),sex(s){};
- void display()
- {
- cout<<"num:"<<num<<endl;
- cout<<"name:"<<name<<endl;
- cout<<"sex:"<<sex<<endl;
- cout<<"+++++++++++++++++"<<endl;
- }
- private:
- int num;
- string name;
- char sex;
- };
- int main()
- {
- Student Stud1;
- Stud1.display();//Stud1未初始化,打印的数据成员为内存脏数据
- Student Stud2(2078, "wang", 'm');
- Stud2.display();
- Student Stud3[20];
- Stud3[0].display();//Stud3[0]未初始化,打印的数据成员为内存脏数据
- Student Stud4[3]= //对象数组的初始化
- {
- Student(1001,"He",'m'), //对象数组成员的初始化
- Student(1002,"You",'f'), //对象数组成员的初始化
- Student(1003,"She",'f') //对象数组成员的初始化
- };
- Stud4[0].display();
- Stud4[1].display();
- Stud4[2].display();
- return 0;
- }
复制代码
例2 对象数组应用
- #include<iostream>
- using namespace std;
- class Box
- {
- public:
- Box(int h=10,int w=12,int len=15):height(h),width(w),length(len) {};//构造函数初始化
- int volume( );
- private:
- int height;
- int width;
- int length;
- };
- int Box::volume( )
- {
- return(height*width*length);
- }
- int main( )
- {
- Box a[3]=
- {
- Box(), //10*12*15 = 1800
- Box(15,18), //15*18*15 = 4050
- Box(16,20,26) //16*20*26 = 8320
- };
- cout<<"volume of a[0] is "<<a[0].volume( )<<endl;
- cout<<"volume of a[1] is "<<a[1].volume( )<<endl;
- cout<<"volume of a[2] is "<<a[2].volume( )<<endl;
- return 0;
- }
复制代码
二、对象指针
(一)指向对象的指针
在创建对象时,编译体系会为每一个对象分配一定的存储空间,以存放其成员。
可以定义一个指针变量,用来存放对象的指针。
- class Time{……};
- Time time1;
- Time *pt; //格式 类名 *对象指针
- pt = &time1; //格式 pt就是指向Time类对象的指针变量
复制代码 并且可以通过对象指针访问对象和对象的成员。
- *pt
- (*pt).hour或pt->hour //即t1.hour
- (*pt).showtime()或pt->showtime() //即time1.showtime( )
复制代码 (二)指向对象数据成员的指针
对象中的成员也有地址,存放对象成员地址的指针变量就是指向对象成员的指针变量。
定义指向对象数据成员的指针变量的方法和定义指向平凡变量的指针变量方法相同。
通过指向对象数据成员的指针变量访问成员
注:对象数据成员为私有时,则无法通过指针访问
- int *p1; //一般形式:数据类型名 *指针变量名;
- p1=&time1.hour;
- cout<<*p1<<endl;
复制代码 (三)指向对象成员函数的指针
指向一般函数的函数指针,如下:
- int (*pf)();
- p1=fun;
- cout<<(*pf)();
复制代码 1 “定义”指向类成员函数的指针变量
函数返回范例名 (类名∷*函数指针变量名)(参数表列);
2 “指向”一个成员函数的方法
函数指针变量名 = &类名∷成员函数名;【注意:这里用类名,而非函数名】
3 通过指向对象中成员函数的指针,调用员函数
(对象名.*函数指针变量名)();
例3 对象指针&对象数据成员指针&对象成员函数指针综合实例
- #include <iostream>
- using namespace std;
- class Time
- {
- public:
- Time(int,int,int);
- void get_time( );
- void showtime();
- //private:
- public: //若为private,则不可通过指针访问(对象指针,对象数据指针均不可访问private)
- int hour,minute,sec;
- };
- Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){};
- void Time::showtime()
- {
- cout<<"hour:"<<hour<<" minute:"<<minute<< " second:"<<sec<<endl;
- cout<<"++++++++++++"<<endl;
- }
- void Time::get_time( )
- {
- cout<<hour<<":"<<minute<<":" <<sec<<endl;
- }
- int main( )
- {
- Time time1(10,11,12);
- Time *pt1; //对象指针
- pt1 = &time1;
- pt1->showtime();
-
- pt1->hour = 9;
- pt1->minute = 10;
- (*pt1).sec = 11;
- (*pt1).showtime();
-
- int *p1,*p2,*p3; //指向对象数据成员的指针
- p1 = &time1.hour;
- p2 = &time1.minute;
- p3 = &time1.sec;
- *p1 = 8;
- *p2 = 9;
- *p3 = 10;
- time1.showtime();
-
- Time time2(7,8,9);
- void(Time::*pf)(); //指向对象成员函数的指针
- pf = &Time::showtime; //注意,这里是类名,而非对象名 &time1.showtime
- (time2.*pf)();
-
- return 0;
- }
复制代码
三、对象数组vs对象指针数组
为提高内存的使用率,内存须要被动态的创建和释放
new运算符:动态分配内存;
delete运算符:释放内存。
用new运算符动态地分配内存后,将返回一个指向新对象的指针的值(内存地址,大小4字节),用户通过这个地址来访问对象。
注:
1)通过new创建的对象,由于未定义对象名,只能通过指针来访问;
2)创建对象时会主动执行构造函数;
3)体系内存不足,会出现异常。
- Time *pt1=new Time;
- Time *pt2;
- pt2=new Time(8,29,59);
- pt2->show_time();
复制代码 不再须要由new创建的对象时,用delete运算符释放
注:在释放内存空间之前,主动调用析构函数,完成有关善后清理工作。
例4 对象的申请和释放
- #include<iostream>
- using namespace std;
- class Box
- {
- public:
- Box(int w,int l,int h);
- ~Box();
- int width;
- int length;
- int height;
- };
- Box::Box(int w,int l,int h)
- {
- width=w;
- length=l;
- height=h;
- cout<<"========调用构造函数=======\n";
- }
- Box::~Box()
- {
- cout<<"========调用析构函数=======\n";
- }
- int main()
- {
- Box * pt = new Box(12,13,14);
- cout<<pt->width<<"\n";
- cout<<pt->length<<"\n";
- cout<<pt->height<<endl;
- delete pt; //如果不delete pt,则不会执行析构函数
- return 0;
- }
复制代码
(一)使用new创建动态对象数组成员
- int n;
- Time *pt;
- cin>>n;
- pt = new Time[n];
- ……
- delete []pt;
复制代码 (二)对象指针数组
一个指针只占用4个字节,因此相比于创建数组或对象数组,指针数组的方式更节流空间;
创建占用空间小的指针数组,灵活处理占用空间大的对象聚集。
- const int N = 100;
- Time *pt[N]={NULL};
- int i;
- ……
- i=3;
- if(pt[i]==NULL)
- pt[i] = new Time(10, 10,10);
- ……
- if(pt[i]!=NULL)
- delete pt[i];
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |