c++怎么将一个类,拆分出接口类,和实现类
还拿上一遍中定义的GradeBook类来实现:
#include <iostream>
using std::cout;
using std::endl; #include <string> // program uses C++ standard string class.
using std::string; #include <conio.h> // GradeBook class definition
class GradeBook
{
public:
// constructor initializes courseName with string supplited as argument.
GradeBook(string name)
{
this->courseName=name;
} // end GradeBook constructor // function to set the course name
void setCourseName(string name)
{
this->courseName=name;// store the course name in the object.
}// end function setCourseName // function to get the course name.
string getCourseName()
{
return courseName;// return object's courseName
}// end function getCourseName // display a welcome message to the GradeBook user
void displayMessage()
{
// call getCourseName to get the courseName
cout<<"Welcome to the grade book for \n"<<getCourseName()<<"|"<<endl;
}// end function displayMessage
private :
string courseName;
};// end class GradeBook // function main begins program execution
int main()
{
// create two GradeBook objects.
GradeBook gradeBook1("CS101 Introduction to C++ programing");;
GradeBook gradeBook2("CS102 Data Structures in C++"); // display initial value of courseName for each GradeBook
cout<<"gradeBook1 created for course:"<<endl;
gradeBook1.displayMessage();
cout<<"gradeBook2 created for course:"<<endl;
gradeBook2.displayMessage(); cout<<"Test the get function"<<endl;
cout<<"gradeBook1 call getCourseName"<<gradeBook1.getCourseName()<<endl;
cout<<"gradeBook2 call getCourseName"<<gradeBook2.getCourseName()<<endl;
std::cin.get(); return ; // indicates successful termination.
}// end main
代码拆分结构:

定义出接口(头文件/GradeBook.h):
#include<string>
using std::string; // GradeBook class definition
class GradeBook
{
public:
// constructor initializes courseName with string supplited as argument.
GradeBook(string name);
// function to set the course name
void setCourseName(string name);
// function to get the course name.
string getCourseName();
// display a welcome message to the GradeBook user
void displayMessage();
private :
string courseName;
};// end class GradeBook
定义出实现类(源文件/GradeBook.cpp):
#include <iostream>
using std::cout;
using std::endl; #include "GradeBook.h" // include definition of class GradeBook // constructor initializes courseName with string supplited as argument.
GradeBook::GradeBook(string name)
{
this->courseName=name;
} // end GradeBook constructor // function to set the course name
void GradeBook::setCourseName(string name)
{
this->courseName=name;// store the course name in the object.
}// end function setCourseName // function to get the course name.
string GradeBook::getCourseName()
{
return this->courseName;// return object's courseName
}// end function getCourseName // display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout<<"Welcome to the grade book for \n"<<getCourseName()<<"|"<<endl;
}// end function displayMessage
在“源文件“下定义Main.cpp写main测试函数:
#include <iostream>
using std::cout;
using std::endl; #include <conio.h> #include "GradeBook.h" // include definition of class GradeBook // function main begins program execution
int main()
{
// create two GradeBook objects.
GradeBook gradeBook1("CS101 Introduction to C++ programing");;
GradeBook gradeBook2("CS102 Data Structures in C++"); // display initial value of courseName for each GradeBook
cout<<"gradeBook1 created for course:"<<endl;
gradeBook1.displayMessage();
cout<<"gradeBook2 created for course:"<<endl;
gradeBook2.displayMessage(); cout<<"Test the get function"<<endl;
cout<<"gradeBook1 call getCourseName:"<<gradeBook1.getCourseName()<<endl;
cout<<"gradeBook2 call getCourseName:"<<gradeBook2.getCourseName()<<endl;
std::cin.get(); return ; // indicates successful termination.
}// end main
输出结果:

c++怎么将一个类,拆分出接口类,和实现类的更多相关文章
- C#找出接口的所有实现类并遍历执行这些类的公共方法
//这里找出了实现IOutputArray接口的所有类 private void FindAllClass() { var types = AppDomain.CurrentDomain.GetAss ...
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- OpenJDK源码研究笔记(十四):三种经典的设计方法,接口,接口-抽象类-具体实现类,接口-具体实现类
在研究OpenJDK源码过程中,我发现常用的设计方法就是2种:接口,接口-抽象类-具体实现类 . 在一些其它开源框架和业务开发中,经常存在着第3种设计,接口-具体实现类. 1.只有接口,没有实现类. ...
- JAVA中Collection接口和Map接口的主要实现类
Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...
- Java的Stack类实现List接口真的是个笑话吗
今天在网上闲逛时看到了这样一个言论,说“Java的Stack类实现List接口的设计是个笑话”. 当然作者这篇文章的重点不是这个,原本我也只是一笑置之,然而看评论里居然还有人附和,说“Ja ...
- java 数据类型:集合接口Collection之队列Queue:PriorityQueue ;Dequeue接口和ArrayDeque实现类:
什么是Queue集合: Queue用于模拟队列这种数据结构,队列通常是"先进先出"(FIFO)的容器.队列的头部保存在队列中存放时间最长的元素,尾部保存存放时间最短的元素. ...
- 在Eclipse中生成接口的JUnit测试类
在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...
- java基础知识回顾之javaIO类--File类应用:过滤器接口FilenameFilter和FileFilter
FilenameFilter和FileFilter都是用来过滤文件,例如过滤,以.jpg或者.java结尾的文件,通过看他们的源码:通过使用File类中String[] list(FilenameFi ...
- java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种 ...
随机推荐
- Android开发的七大环节
Android开发的七大环节 浏览:25 发布日期:2015/10/27 分类:职场感悟 一个完整的Android 开发流程主要包括策划.软件.交互.视觉.测试.运营维护这七大环节,其中的每一个环 ...
- python实践3:cursor() — 数据库连接操作
python 操作数据库,要安装一个Python和数据库交互的包MySQL-python-1.2.2.win32-py2.5.exe,然后我们就可以使用MySQLdb这个包进行数据库操作了. 操作步骤 ...
- laravel paginate动态分页
1.router Route::get('product', function(){ $products = App\Product::paginate(10); return view('produ ...
- Andrew Ng机器学习公开课笔记 – Factor Analysis
网易公开课,第13,14课 notes,9 本质上因子分析是一种降维算法 参考,http://www.douban.com/note/225942377/,浅谈主成分分析和因子分析 把大量的原始变量, ...
- jquery 事件 多次绑定,多次触发,怎么清除历史绑定事件
Jquery在处理事件的时候有时候会遇到预想不到的结果 如下 <a id="link_foo">Foo</a> <script type=" ...
- Qt中单例模式的实现(4种方法)
最简单的写法: 12345 static MyClass* MyClass::Instance(){ static MyClass inst; return &inst;} 过去很长一段时间一 ...
- 新的开始--Python
第一周——python基础 1.Python简介 1.1Python简史 1.2安装Python 1.3“hello world” 在运行第一个程序“hello world”之前,先来看看运行Pyth ...
- asp.net字符串的数学表达式计算结果
using System; using System.Collections.Generic; using System.Web; using System.CodeDom.Compiler; usi ...
- LightOj1418 - Trees on My Island(Pick定理)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1418 题意:给你多边形中的顶点,n个点按顺时针或逆时针方向给出,然后求出多边形内部有多 ...
- Sql server中左连接语句
数据库中学生表和课程表如下: 左连接sql语句: select a.studentName,a.studentAge,b.courseName from student a left join cou ...