代码:
/* * 04737 c++ 自学考试2019版 第二章课后练习 * 程序设计题 1 * 需求:设计并实现一个类Course.它代表学校中的一门课程..... */ //标准流 #include<iostream> //标准字符 #include<string> using namespace std; class Course { public: //无参数构造器 Course(); //有参数构造器 Course(string courseName, string category, string professional, int score, int hours); ~Course(); //声明getter string getCourseName(); string getCategory(); string getProfessional(); int getScore(); int getHours(); //声明setter void setCourseName(string str); void setCategory(string str); void setProfessional(string str); void setScore(int a); void setHours(int a); //声明 打印所有数据函数 void showInfo(); private: //课程名 string courseName; //课程类别 string category; //授课专业 string professional; //学分 int score; //学时数 int hours; }; Course::Course() { cout << "The no-argument constructor is running" << endl; } //定义有参构造 Course::Course(string courseName_, string category_, string professional_, int score_, int hours_) { courseName = courseName_; category = category_; professional = professional_; score = score_; hours = hours_; } Course::~Course() { } //定义getter string Course::getCourseName() { return courseName; } string Course::getCategory() { return category; } string Course::getProfessional() { return professional; } int Course::getScore() { return score; } int Course::getHours() { return hours; } //定义setter void Course::setCourseName(string str) { courseName = str; } void Course::setCategory(string str) { category = str; } void Course::setProfessional(string str) { professional = str; } void Course::setScore(int a) { score = a; } void Course::setHours(int a) { hours = a; } //定义 打印所有数据函数 void Course::showInfo() { cout << "print all the infomation" << endl; cout << "courseName=" << courseName << endl; cout << "category=" << category << endl; cout << "professional=" << professional << endl; cout << "score=" << score << endl; cout << "hours=" << hours << endl; } //定义主函数 int main() { Course test("c++程序设计", "c++", "计算机科学技术", 80, 60); test.showInfo(); }
运行结果:
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。