#include <iostream>
using namespace std;
class test
{
const double pi;
public:
test(): pi(3.14) { }
void show()
{
cout << "The low-precision pi is " << pi << endl;
pi = 3.14159;
cout << "The high-precision pi is " << pi << endl;
}
};
int main()
{
test A;
A.show();
}
预期结果:
The low-precision pi is 3.14
The high-precision pi is 3.14159
const double pi;
这是常量,不能修改,去掉 const
#include <iostream>
using namespace std;
class test
{
double pi;
public:
test(): pi(3.14) { }
void show()
{
cout << "The low-precision pi is " << pi << endl;
pi = 3.14159;
cout << "The high-precision pi is " << pi << endl;
}
};
int main()
{
test A;
A.show();
}
#include <iostream>
using namespace std;
class Test
{
public:
Test(int);
Test(Test&);
~Test();
private:
int a;
};
Test::Test(int i)
{
a = i;
cout << "constructing " << a << endl;
}
Test::Test(Test& test)
{
a = test.a;
cout << "copying " << a << endl;
}
Test::~Test()
{
cout << "destructing " << a << endl;
}
void outPut(Test test1, Test test2);
int main() {
Test* a = new Test(3);
Test test1(1);
Test test2(2);
outPut(test1, test2);
system("pause");
return 0;
}
void outPut(Test test1, Test test2) {
}