return0;
}
5.对象指针
#include
usingnamespacestd;
classTime
{public:
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
inthour;
intminute;
intsec;
voidshow_time();
};
voidTime:
:
show_time()
{
cout<"<"<}
intmain()
{Timet1(16,43,20);
int*p1=&t1.hour;
cout<<*p1<t1.show_time();
Time*p2=&t1;
p2->show_time();//(*p2).show_time();
void(Time:
:
*p3)();
p3=&Time:
:
show_time;
(t1.*p3)();
return0;
}
6.对象的常引用
#include
usingnamespacestd;
classTime
{public:
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
inthour;
intminute;
intsec;
voidfun(Time&t);//形参t是Time类对象的引用//函数声明
voidshow_time();
};
voidTime:
:
show_time()
{
cout<"<"<}
voidfun(Time&t)
{t.hour=18;
t.minute=12;
}
intmain()
{Timet1(16,43,20);
fun(t1);
t1.show_time();
return0;
}
7.静态数据成员引用
#include
usingnamespacestd;
classBox
{public:
Box(intl=10,intw=10):
length(l),width(w){}
intvolume();
staticintheight;
intlength,width;
};
intBox:
:
volume()
{
return(length*width*height);
}
intBox:
:
height=12;//静态数据成员可以初始化,但只能在类外进行初始化
intmain()
{Boxbox1,box2(12,10);
cout<<"Thevolumeofbox1is:
"<cout<<"Thevolumeofbox2is:
"<return0;
}
8.静态成员函数引用
#include
usingnamespacestd;
classStudent
{public:
Student(intn,inta,floats):
num(n),age(a),score(s){}
voidtotal();
intvolume();
staticfloataverage();
private:
intnum;
intage;
floatscore;
staticfloatsum;
staticintscount;
};
floatStudent:
:
sum=0;
intStudent:
:
scount=0;
voidStudent:
:
total()
{
sum+=score;//sum=sum+score;
scount++;
}
floatStudent:
:
average()
{
return(sum/scount);
}
intmain()
{Studentstu[4]={
Student(1101,18,70),
Student(1103,17,69),
Student(1104,18,78),
Student(1106,19,80)};
intn;
cout<<"pleaseinputthenumberofstudents:
";
cin>>n;
for(inti=0;istu[i].total();
cout<<"Theaveragescoreof"<"<:
average()<return0;
}
9.将普通函数声明为友元函数
(1)
#include
usingnamespacestd;
classTime
{public:
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
friendvoiddisplay(Time&t);
private:
inthour;
intminute;
intsec;
};
voiddisplay(Time&t)
{
cout<"<"<}
intmain()
{Timet1(22,55,30);
display(t1);
return0;
}
(2)
#include
usingnamespacestd;
classDate;
classTime
{public:
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
friendvoiddisplay(Time&);
private:
inthour;
intminute;
intsec;
};
voiddisplay(Time&t)
{
cout<"<"<}
classDate
{public:
Date(inty,intm,intd):
year(y),month(m),day(d){}
friendvoiddisplay(Date&);
private:
intyear;
intmonth;
intday;
};
voiddisplay(Date&d)
{
cout<}
intmain()
{Timet1(22,55,30);
Dated1(2012,5,18);
display(d1);
display(t1);
return0;
}
10.友元成员函数
#include
usingnamespacestd;
classDate;
classTime
{public:
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
voiddisplay(Date&);
private:
inthour;
intminute;
intsec;
};
classDate
{public:
Date(inty,intm,intd):
year(y),month(m),day(d){}
friendvoidTime:
:
display(Date&);
private:
intyear;
intmonth;
intday;
};
voidTime:
:
display(Date&d)
{
cout<cout<"<"<}
intmain()
{Timet1(22,55,30);
Dated1(2012,5,18);
t1.display(d1);
return0;
}
11.运算符重载
(1)#include
usingnamespacestd;
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(intr,inti):
real(r),imag(i){}
Complexoperator+(Complex&c2);
voiddisplay();
private:
intreal;
intimag;
};
ComplexComplex:
:
operator+(Complex&c2)//operator+是一个函数名,是类Complex的成员函数
{Complexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
returnc;
}
voidComplex:
:
display()
{
cout<<"("<}
intmain()
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return0;
}
(2)重载流插入运算符“<<”:
#include
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(intr,inti){real=r;imag=i;}
Complexoperator+(Complex&c2);
friendostream&operator<<(ostream&,Complex&);
private:
intreal;
intimag;
};
ComplexComplex:
:
operator+(Complex&c2)
{
returnComplex(real+c2.real,imag+c2.imag);
}
ostream&operator<<(ostream&output,Complex&c)
{output<if(c.imag>=0)output<<"+";
output<returnoutput;
}
intmain()
{Complexc1(2,4),c2(6,-10),c3;
c3=c1+c2;
cout<<"c1="<return0;
}
12.派生类的构造函数和析构函数:
#include
#include
usingnamespacestd;
classStudent
{public:
Student(intn,stringnam,floatm)
{num=n;
name=nam;
mark=m;
}
~Student(){cout<protected:
intnum;
stringname;
floatmark;
};
classStudent1:
protectedStudent
{public:
Student1(intn,stringnam,floatm,inta,chars):
Student(n,nam,m)
{age=a;
sex=s;
}
voidshow()
{cout<<"学号:
"<cout<<"姓名:
"<cout<<"分数:
"<cout<<"年龄:
"<cout<<"性别:
"<}
~Student1(){cout<private:
intage;
charsex;
};
intmain()
{Student1stud1(1101,"zhuxuhong",100,18,'m');
Student1stud2(1102,"zhangxu",80,20,'m');
cout<<"学生1信息:
"<stud1.show(),cout<cout<<"学生2信息:
"<stud2.show(),cout<return0;
}
13.有子对象的派生类的构造函数:
#include
#include
usingnamespacestd;
classStudent
{public:
Student(intn,stringnam,floatm)
{num=n;
name=nam;
mark=m;
}
voiddisplay()
{
cout<<"学号:
"<"<"<}
protected:
intnum;
stringname;
floatmark;
};
classStudent1:
protectedStudent
{public:
Student1(intn,stringnam,floatm,intn1,stringnam1,floatm1,inta,chars):
Student(n,nam,m),monitor(n1,nam1,m1)
{age=a;
sex=s;
}
voidshow()
{cout<<"学生信息:
"<display();
cout<<"年龄:
"<cout<<"性别:
"<}
voidshow_monitor()
{
cout<"<monitor.display();
}
private:
Studentmonitor;
intage;
charsex;
};
intmain()
{Student1stud1(1101,"zhuxuhong",98,1107,"xiaoyin",100,18,'m');
stud1.show();
stud1.show_monitor();
return0;
}
14.多层派生时的构造函数:
#include
#include
usingnamespacestd;
classStudent
{public:
Student(intn,stringnam)
{num=n;
name=nam;}
voiddisplay()
{cout<<"学号:
"<cout<<"姓名:
"<