c++:一个程序多个源/头文件


1、一个程序,一个源文件的做法

#include<iostream>  
#include<cstring>  
using namespace std;  
class Student  
{  
private:  
    char Name[20];  //学生姓名  
    double Chinese; //语文成绩  
    double Math;        //数学成绩  
public:  
    double Average( );//计算平均成绩  
    double Sum( );  //计算总分  
    void Show( );   //打印信息  
    void SetStudentdent(char*,double,double);//为对象置姓名、成绩  
    void SetName(char *);   //为对象置姓名  
    char *GetName( );   //取得学生姓名  
};  
double Student::Average( )  
{  
    return (Chinese+Math)/2;  
}//平均成绩  
  
  
double Student::Sum( )  
{  
    return Chinese+Math;  
}        //总分  
void Student::Show( )   //打印信息  
{  
    cout<<"Name:  "<<Name<<endl<<"Score:  "<<Chinese<<'\t'<<  
        Math<<'\t'<<"average:  "<<Average()<<'\t'<<"Sum:   "<<Sum()<<endl;  
}  
void Student::SetStudentdent(char *name,double chinese,double math)  
{  
    strcpy(Name,name);  //置姓名  
    Chinese=chinese;    //置语文成绩  
    Math=math;  //置数学成绩  
}  
  
  
void Student::SetName(char *name)  
{  
    strcpy(Name,name);  //置姓名  
}  
  
  
char * Student::GetName( )  
{  
    return Name;  
}//返回姓名  
  
  
int main( )  
{  
    Student  p1,p2;  
    p1.SetStudentdent("Li qing",98,96); //对象置初值  
    p2.SetStudentdent("Wang Gang",90,88); //对象置初值  
    p1.Show();//打印信息  
    p2.Show();//打印信息  
    p1.SetName("Zhao jian");//重新置p1对象的名字  
    p1.Show();  
    cout<<"p1.Name: "<<p1.GetName()<<endl;//打印对象的名字  
    cout<<"p1.average: "<<p1.Average()<<endl;//打印对象的成绩  
    return 0;  
}  


2、一个程序,多个源/头文件的做法

student.h

#ifndef STUDENT_H_INCLUDED  
#define STUDENT_H_INCLUDED  
  
  
class Student  
{  
private:  
    char Name[20];  //学生姓名  
    double Chinese; //语文成绩  
    double Math;        //数学成绩  
public:  
    double Average( );//计算平均成绩  
    double Sum( );  //计算总分  
    void Show( );   //打印信息  
    void SetStudentdent(char*,double,double);//为对象置姓名、成绩  
    void SetName(char *);   //为对象置姓名  
    char *GetName( );   //取得学生姓名  
};  
  
  
#endif // STUDENT_H_INCLUDED  

student.cpp

#include<iostream>  
#include<cstring>  
#include "student.h" //预处理代码,相当于将student.h原模原样copy过来了 
using namespace std;  
  
  
double Student::Average( )  
{  
    return (Chinese+Math)/2;  
}//平均成绩  
  
  
double Student::Sum( )  
{  
    return Chinese+Math;  
}        //总分  
  
  
void Student::Show( )   //打印信息  
{  
    cout<<"Name:  "<<Name<<endl<<"Score:  "<<Chinese<<'\t'<<  
        Math<<'\t'<<"average:  "<<Average()<<'\t'<<"Sum:   "<<Sum()<<endl;  
}  
void Student::SetStudentdent(char *name,double chinese,double math)  
{  
    strcpy(Name,name);  //置姓名  
    Chinese=chinese;    //置语文成绩  
    Math=math;  //置数学成绩  
}  
  
  
void Student::SetName(char *name)  
{  
    strcpy(Name,name);  //置姓名  
}  
  
  
char * Student::GetName( )  
{  
    return Name;  
}//返回姓名  


main.cpp

#include<iostream>  
#include "student.h"  //预处理代码,相当于将student.h原模原样copy过来了
using namespace std;  
int main( )  
{  
    Student  p1,p2;  
    p1.SetStudentdent("Li qing",98,96); //对象置初值  
    p2.SetStudentdent("Wang Gang",90,88); //对象置初值  
    p1.Show();//打印信息  
    p2.Show();//打印信息  
    p1.SetName("Zhao jian");//重新置p1对象的名字  
    p1.Show();  
    cout<<"p1.Name: "<<p1.GetName()<<endl;//打印对象的名字  
    cout<<"p1.average: "<<p1.Average()<<endl;//打印对象的成绩  
    return 0;  
}  


何时用头文件.h:


在头文件中,划横线的代码部分主要防止其class函数体部分被在其他文件重复#include的时候重复定义发生错误,所以如果已经被定义过的class就不再被定义了!



类库:

一个类声明放在一个头文件中,所有的头文件形成一个类库;






  • 33
    点赞
  • 109
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值