2020-12-5 数据结构课热身作业

一、目的和要求

  1. 掌握类、对象的基本概念,理解类中成员的访问权限,正确理解类与结构体的异同;
  2. 学习对象的说明和使用方法,掌握构造函数、析构函数的工作原理;
  3. 了解I/O流类的层次结构,掌握C++标准输入输出流的用法,能够使用操纵算子格式化输入输出;
  4. 了解、掌握文件流的用法。

二、实验环境

  1. Windows操作系统;
  2. DEV C++、Visual Studio等语言环境;

三、内容和步骤

l 完成实验项目

1.编写几何图形圆的类Circle,包括两个属性:圆心O(另定义Point(点)类实现)和半径R。成员函数包括:圆心位置获取函数GetO、半径获取函数GetR、半径位置设置函数SetR、圆的位置移动函数MoveTo以及圆的信息打印函数Display等。

说实话写得太粗糙了,有空再练一下类模板

//Circle.h
#include<iostream>
#include<cmath>
using namespace std;

class Point
{
public:
	Point(const float x1=0,const float y1=0):x(x1),y(y1)//构造函数,默认为(0,0)
	{
	}
	Point(const Point& P)                //拷贝构造函数
	{
		*this = P;
	}
	virtual ~Point()                     //析构函数
	{
		this->x = NULL;
		this->y = NULL;
	}
	void Output(ostream &out) const
	{
		cout << "(" << x << "," << y << ")";
	}
	float GetX()
	{
		return this->x;
	}
	float GetY()
	{
		return this->y;
	}
	void SetX(float x1)
	{
		this->x = x1;
	}
	void SetY(float y1)
	{
		this->y = y1;
	}
	Point& operator=(const Point &right)
	{
		this->x = right.x;
		this->y = right.y;
		return *this;
	}
	friend ostream& operator<<(ostream& out,const Point &P)
	{
		P.Output(out);
		return out;
	}
private:
	float x, y;
};

class Circle
{
public:
	Circle(Point& P, float r=1):O(Point(0,0)),R(r)            //构造函数,默认为单位圆→(0,0),半径1
	{
		//Point oo;
		//oo.x = P->x;
		//oo.y = P->y;
		/*if (P.GetX() != NULL|| P.GetY() != NULL || r != NULL) {
			this->MoveTo(P->GetX(),P->GetY());
			SetR(r);
		}*/
	}
	Circle(const Circle& C)
	{
		*this = C;
	}
	virtual ~Circle(){}
	Point GetO()
	{
		return this->O;
	}
	float GetR()
	{
		return this->R;
	}
	void SetR(float r)
	{
		R = r;
	}
	void MoveTo(float x1, float y1)
	{
		O.SetX(x1);
		O.SetY(y1);
	}
	void Display()
	{
		cout << "圆心:" << this->O << ",半径:" << this->R << endl;
	}
private:
	Point O;
	float R;
};

2.设计了课程类(class Course;)和学生类(class student;)简单模拟学生成绩系统。请根据程序的运行结果,在类体外完成所需定义的5个成员函数。

//Course.h

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;



class Course

{

public:

    Course(const string& Id = "", const string& Name = "", int Credit = 0) : id(Id), name(Name), credit(Credit) {}

    friend ostream& operator<<(ostream& out, const Course& c)

    {

        out << c.id << '\t' << setw(20) << left << c.name

            << '\t' << c.credit;

        return out;

    }

private:

    string id, name;           // 课程编号、课程名称

    int credit;                // 课程的学分

};



class Student

{

public:

    Student(const string& Id = "", const string& Name = "",
        int Num = 0, const Course* array = NULL)  // ① 构造函数
    {
        id = Id;
        name = Name;
        num = Num;
        course = new Course[Num];
        for (int i = 0; i < Num; i++)
        {
            course[i] = array[i];
        }
    }

    Student(const Student& s)             // ② 拷贝构造函数
    {
        *this = s;
    }

    virtual ~Student()                    // ③ 析构函数
    {
        delete[] course;
        id = "";
        name = "";
        num = 0;
        course = NULL;
    }

    void Set(const string& Id, const string& Name)
        // ④ 设置(修改)学生的学号、姓名
    {
        id = "";
        id = Id;
        name = "";
        name = Name;
    }

    void Set(int Num, const Course* array, const double* Score)
        // ⑤ 设置(修改)学生所选课程、各课程成绩
    {
        num = Num;
        if (course != NULL)delete[] course;
        if (score != NULL)delete[] score;
        course = new Course[Num];
        score = new double[Num];

        for (int i = 0; i < Num; i++)
        {
            course[i] = array[i];
            score[i] = Score[i];
        }
    }

    Student& operator=(const Student& s)  // 赋值运算符函数

    {

        if (&s == this) return *this;

        if (course != NULL) delete[] course;

        if (score != NULL) delete[] score;

        id = s.id;

        name = s.name;

        if (s.num == 0 || s.course == NULL || s.score == NULL)

        {

            num = 0;

            course = NULL;

            score = NULL;

        }

        else

        {

            num = s.num;

            course = new Course[num];

            score = new double[num];

            for (int i = 0; i < num; i++)

            {

                course[i] = s.course[i];

                score[i] = s.score[i];

            }

        }

        return *this;

    }

    friend ostream& operator<<(ostream& out, const Student& s)    // 重载输出运算符(友元)函数

    {

        out << "学号: " << s.id << ", 姓名: " << s.name << endl;

        for (int i = 0; i < s.num; i++)

            out << s.course[i] << '\t' << s.score[i] << '\t' << endl;

        return out;

    }

private:

    string id, name;        // 学生的学号、姓名

    int num;                // 本学期所选课程数量

    Course* course;         // 具体的课程信息所占资源空间的地址

    double* score;          // 各课程的成绩

};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值