设置并实现一个类Course,他代表学校中的一门课程。按照实际情况,将这门课程的相关信息组织成它的属性。例如,属性包括课程名称、学分、学时、课程类别、授课专业等,并定义必要的访问函数。

代码:

/*
*	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();
}

运行结果:

设置并实现一个类Course,他代表学校中的一门课程。按照实际情况,将这门课程的相关信息组织成它的属性。例如,属性包括课程名称、学分、学时、课程类别、授课专业等,并定义必要的访问函数。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
C++C语言

C语言中math中的sqrt()用法

2023-10-4 11:33:58

C++

设计并实现二维坐标系下的点的类Point,类的每个对象含有横、纵坐标。为类Point添加必要的计算函数,例如,计算给定点到(0,0)的距离,计算给定点两点间的距离,判断给定点的三个点是否能构成一个三角形等。

2023-10-4 12:07:03

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索