您的位置:首页 > 编程语言 > C语言/C++

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

2016-03-17 14:18 405 查看
/* 语言: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个文件后再运行程序
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++