源程序:
//p114页的程序,一个常量成员函数
#include <iostream>
using namespace std;
class Base
{
private:
double x, y;
const double p;
public:
Base(double m, double n, double d) :p(d)
{
x = m;
y = n;
}
void Show();
void Show1() const;
};
void Base::Show()
{
cout << x << "," << y << "p=" << p << endl;
}
void Base::Show1() const
{
cout << x << "," << y << "const p=" << p << endl;
}
void main()
{
Base a(8.9, 2.5, 3.1416);
const Base b(2.5, 8.9, 3.14);//常量对象只能调用常成员函数
b.Show1();
a.Show();
a.Show1();//为了说明普通对象即可以调用普通成员函数,也可以调用常成员函数
system("pause");
}
运行结果: