一,#include <limits>作用
相当于一个特化后的类
举例:
#include<iostream>
#include<limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
return 0;
}
运行结果:
二,用构造函数实现数据的初始化
#include<iostream>
using namespace std;
class Time
{
public:
Time() //类名和定义的函数名一致
{
hour=0;
minute=0;
sec=0; //用构造函数对对象中的数据成员赋初始值
}
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
void Time::set_time() //Time是构造的函数名
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
Time t1;
t1.set_time();
t1.show_time();
Time t2;
t2.show_time();
return 0;
}
从main()函数开始看,执行主函数时,首先建立对象t1,此时自动执行构造函数Time。在执行构造函数Time的过程中对t1对象的数据成员赋初值0.然后再执行主函数中的t1.set_time函数,从键盘输入新值赋给对象t1的数据成员,再输出t1的数据成员的值。接着建立对象t2,同时自动执行构造函数Time,对t2中的数据成员赋初值0.但主函数中没有对t2中的数据成员再赋新值,直接输出数据成员的初值。
运行结果
在类外定义构造函数,要加上类名Time和域限定符"::"
Time::Time()
{
hour=0;
minute=0;
sec=0;
}
注意:
1,构造函数没有返回值,它的作用只是对对象进行初始化,因此也不需要在构造函数时声明类型
不能写成;
int Time()
{……
}
或者
void Time()
{……
}
2,构造函数不需要被用户调用,也不能被用户调用。构造函数是在定义对象时系统自动执行的,而且只能执行一次。构造函数一般声明为public.
3,可以用一个类对象初始化另一个类对象。
Time t1;建立对象t1,同时调用构造函数t1.Time()
Time t2=t1;建立对象t2,t1初始化t2.
4,用户自己没有定义构造函数,c++系统会自动生成一个构造函数,只是这一个构造函数函数体是空的,也没有参数,不执行初始化操作。
用带参的构造函数对不同对象初始化:
#include<iostream>
using namespace std;
class Box
{
public:
Box(int,int,int);//声明带参数的构造函数
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)//类外定义带参数的构造函数
{
height=h;
width=w;
length=len;
}
int Box::volume()//定义计算体积的函数
{
return(height*width*length);
}
int main()
{
Box box1(12,23,22);
cout<<"box1的体积是:"<<box1.volume()<<endl;
Box box2(15,30,22);
cout<<"box2的体积是:"<<box2.volume()<<endl;
return 0;
}
在构造函数中用参数初始化表实现对数据成员的初始化
构造函数的重载
#include<iostream>
using namespace std;
class Box
{
public:
Box();//声明一个无参的构造函数
Box(int h,int w,int len):height(h),width(w),length(len){}//声明一个有参的构造函数
int volume();
private:
int height;
int width;
int length;
};
Box::Box()
{
height=10;//未给定具体实参时的默认值。
width=10;
length=10;
}
int Box::volume()
{
return(height*width*length);
}
int main()
{
Box box1;//建立对象box1,不指定实参
cout<<"The volume of box1 is"<<box1.volume()<<endl;
Box box2(15,30,25);//建立对象box2,指定3个实参
cout<<"The volume of box2 is"<<box2.volume()<<endl;
return 0;
}
建立对象box1时没有给出参数,系统找到与之对应的无参构造函数box,执行此构造函数的结果是使三个数据成员的值均为10,然后输出box1的体积。建立对象box2时给出了三个实参,系统进行执行。
建立对象时不必给出实参的构造函数,称为默认构造函数。一个类只能有一个默认构造函数,若用户未定义构造函数,则系统会自动提供一个默认函数,但他的函数体是空的,不起初始化作用。
构造函数不能被用户显式调用。
构造函数可以使用默认参数
#include<iostream>
using namespace std;
class Box
{
public:
Box(int h=10,int w=10,int len=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{
return(height*width*length);
}
int main()
{
Box box1;
cout<<"The volume of box1 is"<<box1.volume()<<endl;
Box box2(15);
cout<<"The volume of box2 is"<<box2.volume()<<endl;
Box box3(15,30);
cout<<"The volume of box3 is"<<box3.volume()<<endl;
Box box4(15,30,20);
cout<<"The volume of box4 is"<<box4.volume()<<endl;
return 0;
}
Box::Box(int h,int w,int len):height(h),width(w),length(len){ }
一个类只能有一个默认构造函数,可以不使用参数构造的默认函数,一个类只能有一个。
归纳:
1,在类中定义构造函数的函数体中对数据赋初值。
public:
Time()
{
hour=0;
minute=0;
sec=0;
}
2,用带参的构造函数,可以使同类的不同对象中的数据具有不同的初值。
在定义对象时指定实参。
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
Box box1(12,23,22);
3,在构造函数中用参数初始化表实现对数据赋初值。
Box(int h,int w,int len):height(h),width(w),length(len){ };
Box box1(12,23,22);
4,定义构造函数时可以使用默认参数。
Box(int h=10,int w=10,int len=10)
{
height=h;
width=w;
length=len;
}
改成参数初始化表:
Box(int h=10,int w=10,int len=10):height(h),width(w),length(len){ };
5,构造函数可以重载,即在一个类中定义多个同名的构造函数。
Box()//定义无参构造函数Box
{
height=10;
width=10;
length=10;
}
Box (int h,int w,int len):height(h),width(w),length(len){ };//定义有参构造函数Box,用初始化表对数据初始化。
Box box1;//不指定实参,调用无参构造函数Box
Box box2(15,30,25);;//指定三个实参,调用有参构造函数Box.
三,对象指针-c++
指针存的是地址
1,指向对象数据成员的指针
int *p1;
p1=&t1.hour;//将地址赋给p1
cout<<*p1<<endl;输出t1.hour 的值
2,指向对象成员函数的指针
void(*p)();//p是指向void型函数的指针变量
p=fun;
(*p)();//调用fun函数
p2=&Time::get_time;
#include<iostream>
using namespace std;
class Time
{
public:
Time(int,int,int);
int hour;
int minute;
int sec;
void get_time();
};
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void Time::get_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
Time t1(10,13,56);
int *p1=&t1.hour;
cout<<*p1<<endl;
t1.get_time();
Time *p2=&t1;
p2->get_time();
void(Time::*p3)();
p3=&Time::get_time;
(t1.*p3)();
return 0;
}
3,指向当前对象的this指针
this指针是隐式使用的
例:
int box::volume()
{return(height*width*length);}
c++处理
int box::volume(box*this)
{return(this->height*this->width*this->length);}