Design a member function using a constant demonstration program - a simple

Source:

// p114-page program, a constant member function

#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); // const object can only call a member function normally

  b.Show1();

  a.Show();

  a.Show1 (); // To illustrate the ordinary objects which can call ordinary member functions, you can also call the member function normally

  system("pause");

}

 

 operation result:

Guess you like

Origin www.cnblogs.com/duanqibo/p/11889558.html