实验七:类的继承机制与派生类的定义(二)

一、实验目的:

1、熟悉声明和使用类的继承关系,声明派生类;
2、掌握多继承的定义和访问方法;

二、实验环境:

1、 PC计算机一台;
2、 Dev-C++开发环境。

三、实验原理:

C++派生类的构造函数和析构函数,在派生类对象中包含基类对象,因此派生类对象在创建时,除了要调用自身的构造函数进行初始化外,还要调用基类的构造函数初始化其包含的基类对象。因此,程序中任何能够生成派生类对象的语句,都要说明其包含的基类对象是如何初始化的。

四、实验内容:

1、使用C++中的字符串函数或字符串类,完成以下操作:
首先从键盘输入字符串A,显示该字符串长度;再将该字符串A与键盘输入的另一字符串B拼接,显示拼接结果;最后比较两字符串大小,并显示比较结果。

2、定义基类图形类shape,由shape类派生出子类矩形类rectangle,再由rectangle类派生出子类长方体类box,(每个类均定义有参构造函数),定义一个长方体,输出它的表面积和体积。

3、设有一个点类 Point 的定义如下:
Point {
public:
Point() {x = 0; y = 0; }
Point(double xv,double yv) {}
Point(Point& pt) { }
double getx() { }
double gety() { }
double Area() { }
void Showxy() { }
private:
double x,y;
};
补充Point类的内容,再以点 point 类为基类,派生出矩形类 Rectangle和圆类Circle。矩形由左上角的顶点和长、宽定义。圆由圆心和半径定义。派生类中新增的成员函数 position(Point &pt)用于判断任一坐标点是在图形内、还是在图形的边缘上,还是在图形外。

五、实验代码及结果(程序运行结果请以截屏图给出):

1、使用C++中的字符串函数或字符串类,完成以下操作:

首先从键盘输入字符串A,显示该字符串长度;再将该字符串A与键盘输入的另一字符串B拼接,显示拼接结果;最后比较两字符串大小,并显示比较结果。

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

int length(char *s){
    char *p;
    p=s;
    int count;
    while(*p!='\0'){
        count++;
        p++;
    }
    return count;
}

int main(){
    int lenA,lenB;
    char strA[50],strB[50];
    cout<<"输入字符串A:";
    cin>>strA;
    lenA=length(strA);
    cout<<"strA长度为:"<<lenA<<endl;

    cout<<"输入字符串B:";
    cin>>strB;
    
    lenB=length(strB);
    cout<<"strB长度为:"<<lenA<<endl;

    cout<<"拼接字符串:"<<strcat(strA,strB)<<endl;
    if (lenA>lenB){
        cout<<"strA大"<<endl;
    }else if(lenA<lenB){
        cout<<"strB大"<<endl;
    }else{
        cout<<"strA,strA相等"<<endl;
    }
    
    system("pause");
    return 0;
}

在这里插入图片描述

2、定义图形类

定义基类图形类shape,由shape类派生出子类矩形类rectangle,再由rectangle类派生出子类长方体类box,(每个类均定义有参构造函数),定义一个长方体,输出它的表面积和体积。

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

class Shape{
    public:
        double perimeter=0;
        double vol=0;
        double area=0;
    private:
        string name;
    public:
        Shape(string na):name(na){}
        void showShape(){
            cout<<"Shape: "<<name<<endl;
        }
};

class Rectangle:public Shape{
    private:
        double length,width,height;
    public:
        Rectangle(string na):Shape(na){}
        Rectangle(string na,double l,double w,double h):Shape(na),length(l),width(w),height(h){}
        void showRectangle(){
            showShape();
            perimeter=2*(length+width);
            area=length*width;
            cout<<"周长:"<<perimeter<<"\t面积:"<<area<<endl<<endl;
        }
};

class Box:public Rectangle{
private:
    double len, wid,hei;
public:
    Box(string na,double l,double w,double h):Rectangle(na,l,w,h){
        len=l;
        wid=w;
        hei=h;
    }
    void showBox(){
            showShape();
            vol=len*wid*hei;
            area=2*(len*wid+len*hei+wid*hei);
            cout<<"表面积:"<<area<<"\t体积:"<<vol<<endl<<endl;
        }
};

int main(){
    Box re("一个长方体",2,3,2);
    re.showBox();

    system("pause");
    return 0;
}

在这里插入图片描述

3、设一个点类 Point 定义

设有一个点类 Point 的定义如下:
Point {
public:
Point() {x = 0; y = 0; }
Point(double xv,double yv) {}
Point(Point& pt) { }
double getx() { }
double gety() { }
double Area() { }
void Showxy() { }
private:
double x,y;
};
补充Point类的内容,再以点 point 类为基类,派生出矩形类 Rectangle和圆类Circle。矩形由左上角的顶点和长、宽定义。圆由圆心和半径定义。派生类中新增的成员函数 position(Point &pt)用于判断任一坐标点是在图形内、还是在图形的边缘上,还是在图形外。

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

class Point{
    public:
        double x,y;
    public:
        Point(){
            x=0;
            y=0;
        }
        Point(double xx,double yy){
            x=xx;
            y=yy;
        }
        Point(Point &p){
            x=p.x;
            y=p.y;
        }
        double getx(){
            return x;
        }
        double gety(){
            return y;
        }
        double Area(){
            return 0;
        }
        void Showxy(){
            cout<<"("<<x<<","<<y<<")";
        }
};

class Rectangle:public Point{
    private:
        Point pointR;
        double length, width;
    public:
        Rectangle(Point p,double len,double wid){
            pointR.x=p.x;
            pointR.y=p.y;
            length=len;
            width=wid;
        }
        void position(Point& pt){
            cout<<"点";
            pt.Showxy();
            if((pt.getx()>pointR.getx() && pt.getx()<pointR.getx()+length)&&(pt.gety()>pointR.getx()-width && pt.gety()<pointR.gety())){
                cout<<"在rectangle内!"<<endl;
            }else if((pt.getx()==pointR.getx() && (pt.gety()>=pointR.gety()-width && pt.gety()<=pointR.gety()))||
                    (pt.getx()==pointR.getx()+length && (pt.gety()>=pointR.gety()-width && pt.gety()<=pointR.gety()))||
                    (pt.gety()==pointR.gety() && (pt.getx()>=pointR.getx() && pt.getx()<=pointR.getx()+length))||
                    (pt.gety()==pointR.gety()-width && (pt.getx()>=pointR.getx() && pt.getx()<=pointR.getx()+length))){
                cout<<"在rectangle边上!"<<endl;
            }
            else cout<<"在rectangle外!"<<endl;
        }
        void showRectangle(){
            cout<<"矩形左上角顶点坐标:";
            pointR.Showxy();
            cout<<"长:"<<length<<"\t宽:"<<width<<endl;
        }
};

class Circle:public Point{
    private:
        Point pointC;
        double rad;
    public:
        Circle(Point p,double r){
            pointC.x=p.x;
            pointC.y=p.y;
            rad=r;
        }
        void position(Point& pt){
            cout<<"点";
            pt.Showxy();
            double distance=sqrt((pt.getx()-pointC.getx())*(pt.getx()-pointC.getx())+(pt.gety()-pointC.gety())*(pt.gety()-pointC.gety()));
            if(distance<rad){
                cout<<"在circle内!"<<endl;
            }else if(distance==rad){
                cout<<"在circle!上"<<endl;
            }else{
                cout<<"在circle外!"<<endl;
            }
        }
        void showCircle(){
            cout<<"圆心坐标:";
            pointC.Showxy();
            cout<<"半径:"<<rad<<endl;
        }
};

int main(){
    Point point(1,2);
    
    Point p1(2,2);
    Circle c1(p1,23);
    c1.showCircle();
    c1.position(point);
    
    cout<<endl;
    Point p2(2,2);
    Rectangle r1(p2,4,3);
    r1.showRectangle();
    r1.position(point);

    system("pause");
    return 0;
}

在这里插入图片描述

六、实验小结:

在 C++ 中,派生可以是多层次的.

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值