#include <iostream>
#include "类内声明写在头文件中.h"
using namespace std;
//只有在多文件中这样的写法才有意义,不然的话反而让源文件很乱
cstu::cstu(int a,char b)
{
age=a;
name=b;
}
int cstu::func1()
{
cout<<"普通函数也是这样写类内声明,类外定义的"<<endl;
return 0;
}
int main()
{
cstu stu(12);//普通栈区变量
stu.func1();
cstu* stu1=new cstu(13);
cout<<stu.age<<' '<<stu.name<<endl;
cout<<stu1->age<<' '<<stu1->name<<endl;
system("pause");
return 0;
#include "类内声明写在头文件中.h"
using namespace std;
//只有在多文件中这样的写法才有意义,不然的话反而让源文件很乱
cstu::cstu(int a,char b)
{
age=a;
name=b;
}
int cstu::func1()
{
cout<<"普通函数也是这样写类内声明,类外定义的"<<endl;
return 0;
}
int main()
{
cstu stu(12);//普通栈区变量
stu.func1();
cstu* stu1=new cstu(13);
cout<<stu.age<<' '<<stu.name<<endl;
cout<<stu1->age<<' '<<stu1->name<<endl;
system("pause");
return 0;
}
以上是cpp文件的代码。
class Cstu
{
public:
int age;
char name;
Cstu(int a,char b='A');//类内声明.也被称为函数原型
int func1();
};
以上是h文件中的代码。
这种方法多用于多文件程序中