(一)类成员的可访问范围
1.可访问范围关键字:public,protectted,private。缺省认为是private。在类的成员函数内部,能够访问当前对象的全部属性,函数以及同类其他对象的全部属性和函数。在类的成员函数以外的地方,只能够访问该类对象的公有成员。
2.设置私有成员的机制,叫做隐藏。隐藏的目的是强制对成员变量的访问一定要通过成员函数进行,那么以后成员变量的类型等属性修改后,只需要更改成员函数即可,否则,所有直接访问成员变量的语句都需要修改。;另外,一定要通过成员函数进行,有没有安全方面的考虑呢。
#include<iostream>
using namespace std;
class employee{
private:
char szname[30];
public:
long salary;
employee(long sal){
salary=sal;
}
void setname(char*name);
void getname(char*name);
void averagesalary(employee e1,employee e2);
};
void employee::setname(char*name){
strcpy(szname,name);
}
void employee::getname(char*name){
strcpy(name,szname);
}
void employee::averagesalary(employee e1,employee e2){
cout<<e1.szname<<endl;//c++输出字符串的方法。
salary=(e1.salary+e2.salary)/2;
cout<<"average salary is "<<salary<<endl;
}
int main(){
employee e1(200000000);
e1.setname("miao");
employee e2(200000000);
e2.setname("heheda");
e1.averagesalary(e1,e2);
return 0;
}
3.用struct定义类,和class唯一的区别是缺省为public而不是private。
(二)成员函数的重载及参数缺省
#include<iostream>
using namespace std;
class location{
private:
int x,y;
public:
void init(int x=0,int y=0);
void valuex(int val){x=val;}
int valuex(){return x;}
};
void location::init(int x,int y){
//函数参数缺省声明时赋值,定义时注意此处x,y不可再赋值。
this->x=x;
this->y=y;
//注意,this后面是指针。
}
void main(){
location p;
}
另外,注意,如果"p.valuex();",会引发二义性。
(三)构造函数
1.基本概念:和类同名,可以有参数,无返回值,void也没有。作用是对对象进行初始化,如给成员变量赋初值。如果定义类时没写构造函数,则编译器生成一个默认的无参数的构造函数。默认构造函数无参数且不做任何操作。当然,如果定义了构造函数,则编译器不生成默认的无参数的构造函数。对象生成时构造函数自动被调用。对象一旦生成,就再也不能在其上执行构造函数 。一个类可以有多个构造函数。专门执行初始化操作。构造函数最好是public的。
#include<iostream>
using namespace std;
class complex{
private:
double real,imag;
public:
void set(double r,double i);
complex(double r,double i=0);
};
complex::complex(double r,double i){
real=r,imag=i;
}
void main(){
complex c1(2,4),c2(3,5);
complex *pc=new complex(3,4);
}
构造函数在数组中的应用。
#include<iostream>
using namespace std;
class Test{
public:
Test(int n){}//(1)
Test(int n,int m){}//(2)
Test(){}//(3)
};
void main(){
Test array1[3]={1,Test(1,2)};
//三个元素分别用(1),(2),(3)初始化。
Test array2[3]={Test(2,3),Test(1,2),1};
//三个元素分别用(2),(2),(1)初始化。
Test*parray[3]={new Test(4),new Test(1,2)};
//两个元素分别用(1),(2)初始化。
}
(四)复制构造函数。
1.只有一个参数,即对同类对象的引用。形如 X::X( X& )或X::X(const X &), 二者选一。后者能以常量对象作为参数如果没有定义复制构造函数,那么编译器生成默认复制构造函数。默认的复制构造函数完成复制功能。所谓复制构造函数,就是构造一个一样的类的对象,如下图代码中最后一行。
class Complex {
private :
double real,imag;
};
Complex c1; //调用缺省无参构造函数
Complex c2(c1);//调用缺省的复制构造函数,将 c2 初始化成和c1一样
如果定义了自己的复制构造函数,则默认的复制构造函数不存在。
class Complex {
public :
double real,imag
;
Complex(){ }
Complex( const Complex & c )
{
real = c.real
;
imag = c.imag
;
cout << “Copy Constructor called”;
}
};
Complex c1;
Complex c2(c1);
复制构造函数起作用三种情况。
(1)当用一个对象去初始化同类的另一个对象时。
complex c2(c1);
complex c2=c1;//初始化语句,非赋值语句
(2)如果某函数有一个参数是类A的对象,那么该函数被调用时,类A额复制构造函数将被调用。
class A
{
public:
A() { };
A( A & a) {
cout << "Copy constructor called" <<endl;
}
};
void Func(A a1){ }
int main(){
A a2;
Func(a2);
return 0;
}
输出结果: Copy constructor called。
(3)如果函数的返回值是类A的对象时,则函数返回时,A的复制构造函数被调用。
#include<iostream>
using namespace std;
class A
{
public:
int v;
A(int n) { v = n; };
A( const A & a) {
v = a.v;
cout << "Copy constructor called" <<endl;
}
};
A Func() {
A b(4);
return b;
}
int main() {
cout << Func().v << endl; return 0;
}
注意:对象间赋值并不导致复制构造函数被调用。
#include<iostream>
using namespace std;
class CMyclass {
public:
int n;
CMyclass() {};
CMyclass( CMyclass & c) { n = 2 * c.n ; }
};
int main() {
CMyclass c1,c2;
c1.n = 5; c2 = c1; CMyclass c3(c1);
cout <<"c2.n=" << c2.n << ",";
cout <<"c3.n=" << c3.n << endl;
return 0;
}
//输出: c2.n=5,c3.n=10
重点:
void fun(CMyclass obj_ ) {
cout << "fun" << endl;
}
// 这样的函数,调用时生成形参会引发复制构造函数调用,开销比较大。
//所以可以考虑使用 CMyclass & 引用类型作为参数。
// 如果希望确保实参的值在函数中不应被改变,那么可以加上const 关键字:
void fun(const CMyclass & obj) {
//函数中任何试图改变 obj值的语句都将是变成非法
}
(五)类型转换构造函数
定义转换构造函数的目的是实现类型的自动转换。只有一个参数,而且不是复制构造函数的构造函数,一般就可以看作是转换构造函数。当需要的时候,编译系统会自动调用转换构造函数,建立一个无名的临时对象(或临时变量)。
#include<iostream>
using namespace std;
class Complex { public:
double real, imag;
Complex( int i) {
//类型转换构造函数
cout << "IntConstructor called" << endl;
real = i;
imag = 0;
}
Complex(double r,double i) {
real = r;
imag = i;
}
};
int main (){
Complex c1(7,8);
Complex c2 = 12;
// 12被自动转换成一个临时Complex对象
c1 = 9;
// 9被自动转换成一个临时Complex对象
cout<<c1.real << "," << c1.imag << endl;
cout<<c2.real << "," << c2.imag << endl;
return 0;
}
/*输出为:
IntConstructor called
IntConstructor called
9,0
12,0
*/
重点是A选项,注意理解一下类型转换构造函数的含义。
类A定义如下:
class A {
int v;
public:
A(int i) { v = i; }
A() { }
};
下面段程序不会引发类型转换构造函数被调用?
A) A a1(4)
B) A a2 = 4;
C) A a3; a3 = 9;
D) A a1,a2; a1 = a2;
答案:D
(六)析构函数
名字与类名相同,在前面加‘~’, 没有参数和返回值,一个类最多只能有一个析构函数。析构函数对象消亡时即自动被调用。可以定义析构函数来在对象消亡前做善后工作,比如释放分配的空间等。如果定义类时没写析构函数,则编译器生成缺省析构函数。缺省析构函数什么也不做。如果定义了析构函数,则编译器不生成缺省析构函数。
例一:
#include<iostream>
using namespace std;
class String{
private :
char * p;//变量
public:
String () {
p = new char[10];//很典型的在构造函数中对变量进行定义。
}
~ String () ;
};
String ::~ String(){
delete [] p;//很典型的在析构函数中删除变量所占空间,不,是释放空间。
}
例二:对象数组生命期结束时,对象数组的每个元素的析构函数都会被调用。
delete 运算导致析构函数调用。 参数对象消亡也会导致析构函数被调用。函数调用返回时生成临时对象返回,然后函数调用的返回值(临时对象)被用过后,该临时对象析构函数被调用。
#include<iostream>
using namespace std;
class Ctest {
public:
~Ctest() {
cout<< "destructor called" << endl;
}
};
int main () {
Ctest array[2];
cout << "End Main" << endl;
return 0;
}
/*输出:
End Main
destructor called
destructor called
*/
#include<iostream>
using namespace std;
class CMyclass {
public:
~CMyclass() {
cout << "destructor" << endl;
}
};
CMyclass obj;
CMyclass fun(CMyclass sobj ) {
//参数对象消亡也会导致析构函数被调用
return sobj; //函数调用返回时生成临时对象返回
}
int main(){
obj = fun(obj); //函数调用的返回值(临时对象)被
return 0; //用过后,该临时对象析构函数被调用
}
/*输出:
destructor
destructor
destructor
*/
(七)构造函数和析构函数什么时候被调用?
(P90开始)。
(八)复制构造函数在不同编译器中的表现。
(P116开始)。