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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性