C++学习(三)_常成员函数

常成员函数

声明:<类型标志符>函数名(参数表)const;

说明:

  1. const是函数类型的一部分,在实现部分也要带该关键字。const关键字可以用于对重载函数的区分.
  2. 如果将一个对象定义为常对象,则通过该对象只能调用它的常成员函数,而不能调用其他普通函数。但普通函数可以调用常成员函数。
  3. 常成员函数不能更新类的成员变量,也不能调用该类中没有用const修饰的成员函数,只能调用常成员函数。

例一:定义一个square类,这个类有w和h两个私有属性,通过调用getArea()方法可以得到矩形的面积.

#include <iostream>

using namespace std;

class square{
private:
    int w, h;
public:
    int getArea() const; //带const修饰的方法
    int getArea();		 //不带const修饰的方法
    square(int w, int h) : w(w), h(h){};	//构造函数
};

int square::getArea() const    //实现部分也带该关键字
{
    return w * h;
}

int square::getArea()
{
    return w * h;
}

int main()
{
    const square a(3,4);
    cout << a.getArea() << endl;
    square b(2,4);
	cout << b.getArea() << endl;
    
    return 0;
}

运行上述代码,发现程序正常运行.

从上方程序可以看出,我们在square类中定义了两个getArea()方法,一个用const修饰,一个没有。

所以,可得结论1:const是函数类型的一部分,在实现部分也要带该关键字。const函数可以用于重载函数的区分.


例二:将带有const的getArea()方法注释掉,观察程序运行结果

#include <iostream>

using namespace std;

class square{
private:
    int w, h;
public:
//	int getArea() const; //带const修饰的方法
    int getArea();		 //不带const修饰的方法
    square(int w, int h) : w(w), h(h){};	//构造函数
};

//int square::getArea() const    //实现部分也带该关键字
//{
//    return w * h;
//}

int square::getArea()
{
    return w * h;
}

int main()
{
    const square a(3,4);
    cout << a.getArea() << endl;
//  square b(2,4);
//	cout << b.getArea() << endl;
    
    return 0;
}

程序无法正常运行.

因此,我们可得结论2:常对象无法调用没有用const修饰的成员函数.


例三:我们分别将带有const的函数和不带const函数注释掉,观察程序运行结果

#include <iostream>

using namespace std;

class square{
private:
    int w, h;
public:
    int getArea() const; //带const修饰的方法
    int getArea();		 //不带const修饰的方法
    square(int w, int h) : w(w), h(h){};	//构造函数
};

int square::getArea() const    
{
    return w * h;
}

int square::getArea()
{
    return w * h;
}

int main()
{
    //const square a(3,4);
    //cout << a.getArea() << endl;
    square b(2,4);
	cout << b.getArea() << endl;
    
    return 0;
}

程序在两种情况下均可正常运行

所以,得到结论3:普通对象既可以调用有const修饰的函数也可以调用没有const修饰的成员函数.


例四:分别在带const修饰和不带const修饰的getArea()函数下添加上赋值语句"w = 10, h = 10;"

#include <iostream>

using namespace std;

class square{
private:
    int w, h;
public:
    int getArea() const; //带const修饰的方法
    int getArea();		 //不带const修饰的方法
    square(int w, int h) : w(w), h(h){};	//构造函数
};

int square::getArea() const    
{
    w = 10;
    h = 10;
    return w * h;
}

int square::getArea()
{
    w = 10;
    h = 10;
    return w * h;
}

int main()
{
    const square a(3,4);
    cout << a.getArea();
    square b(3,4);
    cout << b.getArea();

    return 0;
}

程序运行错误.

所以得结论4:常成员函数不能更新类的成员变量.


例5:在getArea函数中调用const修饰的函数和非const修饰的函数

#include <iostream>

using namespace std;

class square{
private:
    int w, h;
public:
    int getArea() const; //带const修饰的方法
    int getArea();		 //不带const修饰的方法
    void show() const;	 //带const修饰的show
    void show2();		 //不带const修饰的show2
    square(int w, int h) : w(w), h(h){};	//构造函数
};

void square::show() const {
    cout << "show.." << endl;
}

void square::show2(){
    cout << "show2.." << endl;
}

int square::getArea() const   
{

    show();
    //show2();
    return w * h;
}

int square::getArea()
{
    w = 10, h = 10;
    return w * h;
}

int main()
{
    const square a(3,4);
    cout << a.getArea();

    return 0;
}

运行发现程序能够调用用const修饰的show()函数,无法调用没有用const修饰的show2()函数

结论5:常成员函数不能调用该类中没有用const修饰的成员函数,只能调用常成员函数.

以上即为我对c++常成员函数的理解。

如有错误,敬请指正.

posted @ 2019-09-29 14:06  美团骑手阿霖  阅读(466)  评论(0编辑  收藏  举报