还拿上一遍中定义的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++怎么将一个类,拆分出接口类,和实现类的更多相关文章

  1. C#找出接口的所有实现类并遍历执行这些类的公共方法

    //这里找出了实现IOutputArray接口的所有类 private void FindAllClass() { var types = AppDomain.CurrentDomain.GetAss ...

  2. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  3. OpenJDK源码研究笔记(十四):三种经典的设计方法,接口,接口-抽象类-具体实现类,接口-具体实现类

    在研究OpenJDK源码过程中,我发现常用的设计方法就是2种:接口,接口-抽象类-具体实现类 . 在一些其它开源框架和业务开发中,经常存在着第3种设计,接口-具体实现类. 1.只有接口,没有实现类. ...

  4. JAVA中Collection接口和Map接口的主要实现类

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...

  5. Java的Stack类实现List接口真的是个笑话吗

        今天在网上闲逛时看到了这样一个言论,说“Java的Stack类实现List接口的设计是个笑话”.   当然作者这篇文章的重点不是这个,原本我也只是一笑置之,然而看评论里居然还有人附和,说“Ja ...

  6. java 数据类型:集合接口Collection之队列Queue:PriorityQueue ;Dequeue接口和ArrayDeque实现类:

    什么是Queue集合: Queue用于模拟队列这种数据结构,队列通常是"先进先出"(FIFO)的容器.队列的头部保存在队列中存放时间最长的元素,尾部保存存放时间最短的元素.    ...

  7. 在Eclipse中生成接口的JUnit测试类

    在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...

  8. java基础知识回顾之javaIO类--File类应用:过滤器接口FilenameFilter和FileFilter

    FilenameFilter和FileFilter都是用来过滤文件,例如过滤,以.jpg或者.java结尾的文件,通过看他们的源码:通过使用File类中String[] list(FilenameFi ...

  9. java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?

    java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种 ...

随机推荐

  1. php获得网站根目录的几个方法

    php获得网站根目录的几个方法 电脑软硬件应用网 45IT.COM 时间:2015-01-08 12:54 作者:佚名 在php中我们要得到网站根目录可以用很多全局变量实现了,如可以利用__file_ ...

  2. Yii源码阅读笔记(七)

    接上次的组件(component)代码: /** * Returns a list of behaviors that this component should behave as. * 定义该对象 ...

  3. sqlserver log

    DBCC LOGINFODBCC log('QSSys', TYPE=2)goselect * from sys.fn_dblog(null,null)select [Dirty Pages],[Mi ...

  4. memcached 命中率问题 分析 **

    Memcached, 人所皆知的remote distribute cache(不知道的可以javaeye一下下,或者google一下下,或者baidu一下下,但是鉴于baidu的排名商业味道太浓(从 ...

  5. 让Storm插上CEP的翅膀 - Siddhi调研和集成

    什么是 Siddhi? Siddhi 是一种 lightweight, easy-to-use, open source CEP(Complex Event Processing)引擎,由wso2公司 ...

  6. Address localhost:1099 is already in use 的错误

    http://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683

  7. mssql全文索引

    在使用全文索引的时候例如: SELECT [PRID] ,[PRCode] ,[PRDesc] FROM [test1].[dbo].[PerformanceIssue] where contains ...

  8. 设计模式:抽象工厂模式(Abstract Factory)

    定   义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 结构图: 示例结构图: 实体类: class User { public int Id { get; set; } p ...

  9. Majority Element || leetcode

    编程之美上一样的题目.寻找发帖水王. 利用分治的思想. int majorityElement(int* nums, int numsSize) { int candidate; int nTimes ...

  10. JavaScript实现在页面上的文本框中输入小写字母自动变为大写字母

    <script language="javascript" type="text/javascript"> $(function () { $(&q ...