C++ 包括类的声明、成员函数的实现
#include<iostream.h>
#include<string.h>
class CScore{
private:
int ID;
char Name[8];
int Math;
int Phi;
int Total;
public:
void Input(int,char*,int,int);
void Sum();
void Show();
};
void CScore::Input(int id,char* name,int math,int phi){
ID=id;
strcpy(Name,name);
Math=math;
Phi=phi;
}
void CScore::Sum(){
Total=Math+Phi;
}
void CScore::Show(){
cout<<"学生姓名:"<<Name<<" "<<"学生总成绩:"<<Total<<'\n';
}
void main(){
CScore stu;
stu.Input(1,"tom",80,80);
stu.Sum();
stu.Show();
}
转自:http://zhidao.baidu.com/question/124107520.html
//类的成员函数 声明与定义 可以在类内声明,类外定义。例如:
class A
{
public:
A(int a) : n(a){}
/*A(int a)
{
n = a;
}*/
int Tex(int); //类内声明,这句还可以这样写 int Tex(int i);
private:
int n;
int abc(int);
};
int A::Tex(int i) //类外定义。
{
return i;
}
或者 不声明,直接在类内定义。例如:
class A
{
public:
A(int a) : n(a){}
/*A(int a)
{
n = a;
}*/
//就是以下该函数了。
int Tex(int i)
{
return i;
}
//和上面的对比一下。
private:
int n;
int abc(int);
};