|
本帖最后由 szy2870 于 2025-6-2 20:23 编辑
下面是我在c++中测试的代码:目的就是想实现“”类中声明,类外实现”
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
class ceshi {
public:
int a;
int b;
ceshi(int x, int y);//声明成员函数(构造函数)
void testfunc() //定义成员函数
{
cout << "testfunc" << endl;
}
void testfunc2(); //声明成员函数 这个是类中声明! 要在类外实现!
};
ceshi::ceshi(int x, int y) {//定义成员函数
a = x;
b = y;
}
void ceshi::testfunc2()//定义成员函数 这个是类外实现!
{
cout << "testfunc2" << endl;
}
int main()
{
ceshi c(1, 2);//实例化同时要传入参数,构造函数在实例化时被调用
cout << "c.a = " << c.a << endl;
cout << "c.b = " << c.b << endl;
c.testfunc(); //调用成员函数
c.testfunc2(); //调用成员函数
return 0;
}
|
|