程序设计基石与实践之C++语言接口与实现分离
来源:互联网 发布:mysql concat函数 编辑:程序博客网 时间:2024/05/21 06:12
我们将类定义和使用此类的客户代码分离,增强了软件的可复用性.而接口定义并标准化了人和系统等诸如此类事物彼此交互的方式.每个类定义包含了类的公有成员函数的完整定义及其私有数据成员声明.可是更好的软件工程是在类定义的外部定义成员函数,这样这些成员函数的实现细节对客户代码而言隐藏的,这种方式保证程序员不会写出依赖于类的实现细节的客户代码.否则,若类的实现更改,则客户代码将更可能"遭到损坏".
GradeBook.h:使用函数原型定义类接口
// GradeBook.h GradeBook class definition. This file presents GradeBook's public // interface without revealing the implementations of GradeBook's member // functions, which are defined in GradeBook.cpp. #include <string> // class GradeBook uses C++ standard string class using std::string; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor that initializes courseName void setCourseName( string ); // function that sets the course name string getCourseName(); // function that gets the course name void displayMessage(); // function that displays a welcome message private: string courseName; // course name for this GradeBook }; // end class GradeBook
GradeBook.cpp:在独立的源代码文件中定义成员函数
源代码GradeBook.cpp定义了GradeBook类的成员函数,这些函数声明位于GradeBook.h.
// GradeBook.cpp GradeBook member-function definitions. This file contains// implementations of the member functions prototyped in GradeBook.h.#include <iostream>using std::cout; using std::endl;#include "GradeBook.h" // include definition of class GradeBook// constructor initializes courseName with string supplied as argumentGradeBook::GradeBook( string name ){ setCourseName( name ); // call set function to initialize courseName} // end GradeBook constructor// function to set the course namevoid GradeBook::setCourseName( string name ){ courseName = name; // store the course name in the object} // end function setCourseName// function to get the course namestring GradeBook::getCourseName(){ return courseName; // return object's courseName} // end function getCourseName// display a welcome message to the GradeBook uservoid GradeBook::displayMessage(){ // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;} // end function displayMessage
测试GradeBook类
// GradeBook class demonstration after separating its interface from its implementation.#include <iostream>using std::cout; using std::endl;#include "GradeBook.h" // include definition of class GradeBook// function main begins program executionint main(){ // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; return 0; // indicate successful termination} // end main测试输出结果
编译和连接过程
下图所示是显示了生成可供教师使用的,可行的GradeBook应用程序的编译和连接过程.通常由一个程序员创建和编译类的接口和实现,而由不同的实现类客户代码的程序员使用它们.因此这个示意图显示了类实现程序员和客户代码程序员需要做的部分.图中虚线划分了类实现程序员,客户代码程序员和GradeBook应用程序用户各自需要做的部分.
负责创建可复用GradeBook类的类实现程序员首先创建两个文件,一个是头文件GradeBook.h,另一个是包含(#include)头文件的源代码文件GradeBook.cpp.然后,编译源代码文件,创建GradeBook对象的目标代码.
关于Program Language更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.
0 0
- 程序设计基石与实践之C++语言接口与实现分离
- 程序设计基石与实践之C语言概述与特点
- 程序设计基石与实践之C语言指针Point
- 程序设计基石与实践之C语言基本概念
- 程序设计基石与实践之C语言声明
- 程序设计基石与实践之C语言内存地址基础
- 程序设计基石与实践系列之最短的崩溃C语言程序
- 程序设计基石与实践之C语言指针和数组基础
- 程序设计基石与实践系列之失落的C语言结构体封装艺术
- 程序设计基石与实践系列之成为一名Top的C语言程序员
- 程序设计基石与实践系列之写一个C语言编译器
- 程序设计基石与实践系列之C语言函数指针基础
- 程序设计基石与实践系列之C语言程序员必读的5本书
- 程序设计基石与实践系列之C语言未定义行为一览
- 程序设计基石与实践之旋转方阵
- 程序设计基石与实践系列之能让你成为Top程序员的十个C语言资源
- 程序设计基石与实践系列之从开源项目学习C语言基本的编码规则
- 程序设计基石与实践系列之编写高效的C程序与C代码优化
- MFC模态对话框和非模态对话框
- IntelliJ Idea 常用快捷键列表
- Android源码的编译(4.4.2)
- 函数指针的使用
- 2015-1-8 开通了博客
- 程序设计基石与实践之C++语言接口与实现分离
- c语言解析XML字符串
- This proposal is bas
- 【web++_第四阶_中间学习阶段_精美前台】
- Silverlight中Webservice通讯
- 大型网站体系架构分析
- iOS开发- UICollectionView详解+实例
- 二分查找树(BST)
- Android开发者已经度过了初级、中级,如何成为一个Android高手呢?