C++ 学习之路(8):C++程序的多文件组成

/* 语言:C++      编译环境:Visual C++6.0   */
/*---------------------student.h----------------------*/
// 文件1 student.h(类的声明部分)
#include <iostream>
using namespace std;
class Student
{
   private:
       char *name;      // 学生姓名
       char *stu_no;    // 学生学号
       float score;     // 学生成绩

   // 类的外部接口
   public:      
       // 声明构造函数
       Student(char *name1,char *stu_no,float score1);  
       // 声明析构函数
       ~Student();                                     
       // 声明数据修改函数
       void modify(float score1);                       
       // 声明数据输出函数
       void show();                                     
};
/*---------------------student.cpp----------------------*/
// 文件2 student.cpp(类的实现部分)
#include "student.h"            // 包含类的声明文件
// 构造函数的实现
Student::Student(char *name1,char *stu_no1,float score1)    
{
    name = new char[strlen(name1)+1];
    strcpy(name,name1);
    stu_no = new char[strlen(stu_no1)+1];
    strcpy(stu_no,stu_no1);
    score = score1;
}
Student::~Student()        // 析构函数的实现
{
    delete []name;
    delete []stu_no;
}
void Student::modify(float score1)  // 数据修改函数的实现
{
    score = score1;
}
void Student::show()                // 数据输出函数的实现
{
    cout<<"name: "<<name<<endl;
    cout<<"stu_no: "<<stu_no<<endl;
    cout<<"score: "<<score<<endl;
}
/*---------------------studentmain.cpp----------------------*/
// 文件3 studentmain.cpp (类的使用部分)
#include "student.h"
int main()
{
    Student stu1("ChangRui","201601028",96);

    stu1.show();
    stu1.modify(95);
    stu1.show();
    return 0;
}
  • 请打开3个文件后再运行程序
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值