C++实验三——类和对象(1)

实验报告

【实验名称】 实验三 类和对象(1)
【实验内容】

题目1

设计一个用于描述三维空间中的点的类,为其设计必要的成员变量和函数,并尽量增强其功能。

#include<iostream>
#include<math.h>
using namespace std;

class Point {
public:
    Point(){}
    Point (string name,double x,double y,double z){
        this->name = name;
        this->x = x;
        this->y = y;
        this->z = z;
    }

    Point(Point &p);    //复制构造函数

    void setName(string name){ this->name = name; }
    void setX(double x){ this->x = x; }
    void setY(double y){ this->y = y; }
    void setZ(double z){ this->z = z; }

    string getName(){ return name; }
    double getX(){return x;}
    double getY(){return y;}
    double getZ(){return z;}

    double distance1(){    //求该点离原点的距离
        double result = sqrt(pow(x,2) + pow(y,2) + pow(z,2));
        return result;
    }

    double distance2(Point p1){  //求两点之间的距离
    double result = sqrt(pow(fabs(x - p1.x), 2) + pow(fabs(y - p1.y), 2) + pow(fabs(z - p1.z), 2));
    return result;
    }

private:
    string name;   //点的名字
    double x;   //横坐标
    double y;   //纵坐标
    double z;   //Z轴坐标
};

Point::Point(Point &p){    //复制构造函数的实现
    x = p.x;
    y = p.y;
    z = p.z;
    name = p.name;
}


int main (){
    Point point1("A",4,5,6);
    Point point2;
    point2.setName("B");
    point2.setX(1);
    point2.setY(2);
    point2.setZ(3);
    cout << "点" << point1.getName() << "的横坐标为:" << point1.getX() << " 纵坐标为:" << point1.getY() << " Z轴坐标为:" << point1.getZ() <<endl;
    cout << "点" << point2.getName() << "离原点的距离为:" <<point2.distance1() <<endl;
    cout << "点" << point1.getName() << "和点" << point2.getName() << "之间的距离为:" <<point1.distance2(point2) <<endl;
    Point point3(point2);
    cout << "将点" << point2.getName() << "的内容复制给一个新的点对象" <<endl;
    cout << "点" << point3.getName() << "的横坐标为:" << point3.getX() << " 纵坐标为:" << point3.getY() << " Z轴坐标为:" << point3.getZ() <<endl;
    return 0;
}

【实验结果】
在这里插入图片描述

题目2

设计一个描述动物的类,为其设计必要的成员变量和函数,并尽量增强其功能。

#include<iostream>
#include<windows.h>
using namespace std;

class Animal {
public:
    Animal(){}  //默认构造函数
    Animal(string name,int age,string color,string voice,string food){  //带所有成员变量的构造函数
        this->name = name;
        this->age = age;
        this->color = color;
        this->voice = voice;
        this->food = food;
    }
    void setName(string name){ this->name = name; }     //设置每个成员变量的值
    void setAge(int age){ this->age = age; }
    void setColor(string color){ this->color = color; }
    void setVoice(string voice){ this->voice = voice; }
    void setFood(string food){ this->food = food; }

    string getName(){ return name; }         //得到每个成员变量的值
    int getAge(){ return age; }
    string getColor(){ return color; }
    string getVoice(){ return voice; }
    string getFood(){ return food; }

    void shout(){        //动物的叫
    cout << "我是一只" << name << endl;
    cout << "我的叫声是:" << voice << endl;
    }

    void eat(){    //动物吃东西
        cout << name << "开始吃" << food <<endl;
    }

    void beginSleep(){  //动物睡觉
        cout <<"哄" << name << "开始睡觉,输入睡觉的时间:(秒)" <<endl;
        int sleepTime;
        cin >> sleepTime;
        Sleep(sleepTime * 1000);
        cout << name <<"睡醒了" <<endl;
    }

private:
    string name;    //名字
    int age;        //年龄
    string color;   //颜色
    string voice;   //叫声
    string food;    //食物
};

int main() {
    Animal dog("小黑狗",2,"黑色","汪汪汪","骨头");
    dog.eat();
    Animal cat;
    cat.setName("小花猫");
    cat.setAge(1);
    cat.setColor("花色");
    cat.setVoice("喵喵喵");
    cat.setFood("鱼");
    cat.shout();
    cat.beginSleep();
}

【实验结果】
在这里插入图片描述

题目3

设计一个矩形类,为其设计必要的成员变量和函数,并尽量增强其功能。

#include<iostream>
#include<math.h>
using namespace std;

class Rectangle {
public:
    Rectangle(){}
    Rectangle(int a , int b){
        length = a;
        width = b;
    }

    void setLength(int x){ length = x;}
    void setwidth(int y){ width = y; }

    int getLength(){ return length; }
    int getwidth(){ return width; }

    int area(){    //矩形的面积
        int result = length * width;
        return result;
    }

    int perimeter(){  //矩形的周长
        return 2 * (length + width);
    }

    double diagonal(){   //对角线的长度
        double result = sqrt(pow(length , 2) + pow(width , 2));
        return result;
    }

    void exchangle(){   //转置,长宽交换
        length = length + width;
        width = length - width;
        length = length - width;
    }
    Rectangle(Rectangle &r);
private:
    int length;
    int width;
};

Rectangle::Rectangle(Rectangle &r){  //复制构造函数
    length = r.length;
    width = r.width;
}

int main(){
    Rectangle rectangle1(4,5);
    cout << "rectangle1的长为:" <<rectangle1.getLength()<<" 宽为:"<<rectangle1.getwidth()<<endl;
    cout << "rectangle1的面积为:" <<rectangle1.area()<<" 周长为:"<<rectangle1.perimeter()<<" 对角线长为:"<<rectangle1.diagonal()<<endl;
    Rectangle rectangle2;
    rectangle2.setLength(2);
    rectangle2.setwidth(3);
    cout << "rectangle2的长为:" <<rectangle2.getLength()<<" 宽为:"<<rectangle2.getwidth()<<endl;
    rectangle2.exchangle();
    cout << "转置后,rectangle2的长为:" <<rectangle2.getLength()<<" 宽为:"<<rectangle2.getwidth()<<endl;
    Rectangle rectangle3(rectangle1);
    cout << "将rectangle1的内容复制给新的对象rectangle3,rectangle3的长为:" <<rectangle3.getLength()<<" 宽为:"<<rectangle3.getwidth()<<endl;
}

【实验结果】
在这里插入图片描述

【小结或讨论】
实验三的主要内容是类和对象(1),这次的实验比较简单,就是单个类的成员变量及其成员函数的实现,每个类也就是一些基本的属性和功能,感觉思考它有什么属性和功能,比实现还难。第一题点类实现的功能有成员变量的生成器(Setter)和构造器(Getter),复制构造函数,点到原点的距离,两个点之间的距离等;第二题动物类实现的功能有成员变量的生成器(Setter)和构造器(Getter),吃东西,叫声,睡觉等,其中睡觉函数调用了windows.h头文件里的Sleep函数来模拟动物的睡觉;第三题矩形类实现的功能有成员变量的生成器(Setter)和构造器(Getter),复制构造函数,矩形的面积,矩形的周长,矩形的对角线长,矩形的转置(长和宽互换)。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值