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"
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"
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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值