DongqianxiaoC++ day02
2021/6/5 22:21:52
本文主要是介绍DongqianxiaoC++ day02,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C++构造函数
#include <iostream> #include <string> using namespace std; class GradeBook { public: // constructor initializes courseName with string supplied as argument // 构造函数 用于这个类被创建时要做的事情 GradeBook( string name ,int money ) { setCourseName( name ); // call set function to initialize courseName setCourseMoney(money); } // end GradeBook constructor // function to set the course name void setCourseName( string name ) { 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 void setCourseMoney(int money){ courseMoney = money; } int getCourseMoney(){ return courseMoney; } private: string courseName; int courseMoney; }; // function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "C++", 1000 ); GradeBook gradeBook2( "Python" , 2000); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; // gradeBook1.setCourseMoney(1000); cout << "gradeBook1 CourseMoney: " << gradeBook1.getCourseMoney() << endl;; cout << "gradeBook2 CourseMoney: " << gradeBook2.getCourseMoney() << endl;; } ### 包含用户定义类的头文件
编写GradeBook.h头文件
#include <iostream> #include <string> // class GradeBook uses C++ standard string class using namespace std; class GradeBook { public: // constructor initializes courseName with string supplied as argument GradeBook( string name ) { setCourseName( name ); // call set function to initialize courseName } // end GradeBook constructor // function to set the course name void setCourseName( string name ) { 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; // course name for this GradeBook }; // end class GradeBook
编写测试cpp文件
注意引入 #include “GradeBook.h”
#include <iostream> #include "GradeBook.h" using namespace std; int 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; } // end main
接口与实现分离
接口的作用:只做声明, 不做实现
编写接口 GradeBook.h
#include <string> // class GradeBook uses C++ standard string class using namespace std; // 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
实现类:做具体的实现
#include <iostream> #include "GradeBook.h" // include definition of class GradeBook using namespace std; // constructor initializes courseName with string supplied as argument GradeBook::GradeBook( string name ) { setCourseName( name ); // call set function to initialize courseName } // end GradeBook constructor // function to set the course name void GradeBook::setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function to get the course name string GradeBook::getCourseName() { return 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 `
测试类将接口include即可
// Fig. 3.13: fig03_13.cpp // GradeBook class demonstration after separating // its interface from its implementation. #include <iostream> #include "GradeBook.h" // include definition of class GradeBook using namespace std; // function main begins program execution int 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; } // end main
这篇关于DongqianxiaoC++ day02的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23DevExpress 怎么实现右键菜单(Context Menu)显示中文?-icode9专业技术文章分享
- 2024-12-22怎么通过控制台去看我的页面渲染的内容在哪个文件中呢-icode9专业技术文章分享
- 2024-12-22el-tabs 组件只被引用了一次,但有时会渲染两次是什么原因?-icode9专业技术文章分享
- 2024-12-22wordpress有哪些好的安全插件?-icode9专业技术文章分享
- 2024-12-22wordpress如何查看系统有哪些cron任务?-icode9专业技术文章分享
- 2024-12-21Svg Sprite Icon教程:轻松入门与应用指南
- 2024-12-20Excel数据导出实战:新手必学的简单教程
- 2024-12-20RBAC的权限实战:新手入门教程
- 2024-12-20Svg Sprite Icon实战:从入门到上手的全面指南
- 2024-12-20LCD1602显示模块详解