/* file = main.cpp */
#include <iostream>
#include "testClass2.h"
using namespace std;
// 这段代码演示如何为类的非静态常量或者引用成员赋值
int main()
{
testClass2 tc1(2, 3);
cout << "End!" << endl;
return 0;
}
/* file = testClass2.h */
#ifndef TESTCLASS2_H
#define TESTCLASS2_H
#include <iostream>
using std::cout;
using std::endl;
/* 注意:类成员如果是非静态常量或者引用
* 必须采用特殊的初始化方法,
* 下面是含有非静态成员的示例
*/
class testClass2
{
public:
testClass2(int m,int n);
virtual ~testClass2();
protected:
private:
const int m_a;
const int m_b;
};
#endif // TESTCLASS2_H
/* testClass2.cpp */
#include "testClass2.h"
/* c++ 为构造函数提供一种数据成员初始化的特殊语句,
由冒号和逗号分隔,位于构造函数参数的右括号后,
函数体左括号前,以冒号开头,用逗号分隔变量;
:变量1(变量值1),变量2(变量值2)...
在构造函数体执行前先执行初始化
*/
testClass2::testClass2(int m,int n):m_a(m),m_b(n)
{
cout << "m_a := " << m_a << endl;
cout << "m_b := " << m_b << endl;
}
testClass2::~testClass2()
{
}
#include <iostream>
#include "testClass2.h"
using namespace std;
// 这段代码演示如何为类的非静态常量或者引用成员赋值
int main()
{
testClass2 tc1(2, 3);
cout << "End!" << endl;
return 0;
}
/* file = testClass2.h */
#ifndef TESTCLASS2_H
#define TESTCLASS2_H
#include <iostream>
using std::cout;
using std::endl;
/* 注意:类成员如果是非静态常量或者引用
* 必须采用特殊的初始化方法,
* 下面是含有非静态成员的示例
*/
class testClass2
{
public:
testClass2(int m,int n);
virtual ~testClass2();
protected:
private:
const int m_a;
const int m_b;
};
#endif // TESTCLASS2_H
/* testClass2.cpp */
#include "testClass2.h"
/* c++ 为构造函数提供一种数据成员初始化的特殊语句,
由冒号和逗号分隔,位于构造函数参数的右括号后,
函数体左括号前,以冒号开头,用逗号分隔变量;
:变量1(变量值1),变量2(变量值2)...
在构造函数体执行前先执行初始化
*/
testClass2::testClass2(int m,int n):m_a(m),m_b(n)
{
cout << "m_a := " << m_a << endl;
cout << "m_b := " << m_b << endl;
}
testClass2::~testClass2()
{
}