zoukankan      html  css  js  c++  java
  • 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 
    "<<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 0; // indicates successful termination.
    }// end main
    View Code

    代码拆分结构:

    定义出接口(头文件/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 
    "<<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 0; // indicates successful termination.
    }// end main

    输出结果:

  • 相关阅读:
    区块链解读
    如何在ASP.NET Core项目启动时执行异步定时任务
    深入理解ASP.NET Core中的Program类和Startup类
    VS 2017 .Net Core Error : 项目文件不完整,缺少预期导入。
    dotnet不是内部或外部的命令,也不是可运行的程序或批处理文件
    win10系统卸载matlab时出现exeption calling main怎么解决?
    解决mui错误:Unable to preventDefault inside passive event listener due to target being treated as passive.
    类型"*.Properties.Resources" 没有名为"*"的属性
    C#生成唯一不重复订单号帮助类
    Day16.参数传递(token传递,接口关联等)
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/3714840.html
Copyright © 2011-2022 走看看