首页 > 其他分享 >每天打卡一小时 第十五天 多态

每天打卡一小时 第十五天 多态

时间:2023-04-24 19:46:05浏览次数:33  
标签:const 函数 Point double void 多态 打卡 第十五天 yv

6-2 【CPP0025】以点类Point及平面图形类Plane为基础设计圆类Circle 分数 10 作者 C++多态编程 单位 石家庄铁道大学

以点类Point及平面图形类Plane为基类公有派生圆类Circle,main(void)函数完成对其的测试。

 

 

设计思路

 

类Cirlce 公有继承 两个类 

在构造函数中,用setX,setY进行赋值,并对r进行赋值

Plane 中存在虚拟函数,与Circle中的函数重名

基类 表示 派生类 时有同名函数用 虚函数 可以直接调用 派生类 中的 函数,否则就是调用基类中的函数

 

代码

#include <iostream>
using namespace std;
//点类Point
class Point{
private:
    double x;
    double y;
public:
    Point(double xv=0,double yv=0);/*构造函数*/
    Point(const Point &p);         /*拷贝构造*/
    ~Point();                      /*析构函数*/
    virtual void show()const;      /*显示*/
    void setX(double xv);          /*设置X坐标*/
    void setY(double yv);          /*设置Y坐标*/
    double getX() const;           /*获取X坐标*/
    double getY() const;           /*获取Y坐标*/
};
Point::Point(const double xv,const double yv){/*构造函数*/
    x=xv;
    y=yv;
    cout<<"Point Constructor run"<<endl;
}
Point::Point(const Point &p){/*拷贝构造*/
    x=p.x;
    y=p.y;
    cout<<"Point CopyConstructor run"<<endl;
}
Point::~Point(){/*析构函数*/
    cout<<"Point Destructor run"<<endl;
}
void Point::show() const{/*显示*/
    cout<<"Point(X="<<x<<",Y="<<y<<")";
}
void Point::setX(double xv){/*设置X坐标*/
    x=xv;
}
void Point::setY(double yv){/*设置Y坐标*/
    y=yv;
}
double Point::getX() const{/*获取X坐标*/
    return x;
}
double Point::getY() const{/*获取Y坐标*/
    return y;
}
class Plane{/*平面图形基类*/
public:
    virtual double length()const=0;/*周长*/
    virtual double area()const=0;  /*面积*/
};

class Circle : public Point, public Plane
{
    const double PI = 3.14159;
private:
    double radius;
public:
    Circle(double x = 0.0, double y = 0.0, double r = 0.0) 
    {
        setX(x);
        setY(y);
        radius = r;
        cout << "Circle Constructor run" << endl;
    }
    
    Circle(Circle &b):Point(b)
    {
       cout<<"Circle CopyConstructor run"<<endl;
    }

    ~Circle()
    {
        cout << "Circle Destructor run" << endl;
    }

    void setR(double r)
    {
        radius = r;
    }

    double getR() const
    {
        return radius;
    }

    void show() const
    {
        cout << "Circle(Point(" << "X=" << getX() << "," << "Y=" << getY() << ")," << "Radius=" << radius << ")";
    }

    double area() const
    {
        return radius * PI * radius;
    }

    double length() const
    {
        return 2 * radius * PI;
    }
};




void show(Point *p){/*点基类的显示函数*/
    p->show();
}
void length(Plane *p){/*平面图形的周长函数*/
    cout<<"Length="<<p->length()<<endl;
}
void area(Plane &p){/*平面图形的面积函数*/
    cout<<"Area="<<p.area()<<endl;
}
//主函数
int main(void){
    double x,y,r;
    Circle c1,c2(c1);
    show(&c1);
    cout<<endl;
    area(c1);
    length(&c1);
    cin>>x>>y>>r;
    c2.setX(x);
    c2.setY(y);
    c2.setR(r);
    show(&c2);
    cout<<endl;
    area(c2);
    length(&c2);
}

 

标签:const,函数,Point,double,void,多态,打卡,第十五天,yv
From: https://www.cnblogs.com/youxiandechilun/p/17350662.html

相关文章

  • 打卡1
    问题描述:编写用牛顿迭代法求方程根的函数。方程为ax^3+bx^2+cx+d=0,系数a,b,c,d由主函数输入。求x在1附近的一个实根。求出根后由主函数输出。牛顿迭代法的公式是:x=x0-f(x0)/f’(x0),设迭代到|x-x0|<=10^-5时结束。流程图: 伪代码:inputa,b,c,dx0<-1.1forvoidx<-x0-(a*x0......
  • 每日打卡一小时(第十五天)多态
    一.问题描述补充下列代码,使得程序的输出为:A:3A:15B:53155类和函数接口定义: 参见裁判测试程序样例中的类和函数接口。 裁判测试程序样例: #include<iostream>usingnamespacestd;classCMyClassA{intval;public:CMyClassA(int);voidvirtualprin......
  • 打卡9
    折半查找#include<stdio.h>#defineN10intmain(){ inta[N]={-3,4,7,9,13,45,67,89,100,180},low=0,high=N-1,mid,k=-1,m; printf("a数组中的数据如下:\n"); for(inti=0;i<N;i++) printf("%d",a[i]); printf("\n"); printf("Ente......
  • 周一打卡
    问题描述:有一百块钱,要买100只鸡。公鸡5元一只,母鸡3元一只,小鸡1元三只。问要买几只公鸡、母鸡和小鸡?设计思路:这是一道数学问题,可以用穷举法解决。首先,循环公鸡的数量,然后在循环母鸡的数量,最后计算小鸡的数量,判断是否符合题目要求(钱数和数量)。程序流程图:1.循环公鸡的数......
  • 编程打卡:来玩玩Ruby语言吧!
    编程打卡:来玩玩Ruby语言吧!打印字符串"Hello,World."puts('Hello,World')Hello,World=>nil在字符串"Hello,Ruby."中,找到"Ruby."的所在下标'Hello,Ruby.'.index('Ruby')=>7打印你的名字十遍foriin1..10puts('......
  • 打卡11
    2.8猜数牌 基本框架for(inti=1;i<=13;i++)//循环13次,每次将一张牌放进盒子 { intn=1; do//内循环找盒子,将i号牌放入 { //如果盒子非空,继续找下一个盒子 //如果盒子空,判断盒子序号和牌序号是否相同,相同则存入 }while(n<=i); }do { if......
  • 打卡第十天
    读入一系列整数,统计出正整数的个数i和负整数个数j,读入0则结束一、1.定义变量用于存储整数2.运用while循环语句进行统计二、三、#include<iostream>usingnamespacestd;intmain(){ inti=0,j=0,n; cout<<"输入一系列整数:"<<endl; cin>>n; while(n!=0) { if(n>0)i+=1; el......
  • 2022.4.23编程一小时打卡
    一、问题描述:定义一个基类,派生出子类,基类有fn1(),fn2(),fn1()是虚函数;子类也有这俩个函数,在主函数中声明子类的一个对象,并通过指针调用这俩个函数。观察程序运行过程。二、解题思路:首先,定义一个基类BaseClass类,其派生出子类DerivedClass类,在主函数中定义基类的指针,调用这俩个函......
  • 天天打卡一小时第八天
    天天打卡一小时第八天问题描述实验2-3找出三位水仙花数本题要求编写程序,输出给定正整数M和N区间内的所有三位水仙花数。三位水仙花数,即其个位、十位、百位数字的立方和等于该数本身。输入格式:输入在一行中给出两个正整数M和N(100≤M≤N≤999)。输出格式:顺序输出M和N区间内所有三......
  • 每日打卡
    /*模块格式:template<classT>返回值类型函数名(参数形参表){ 函数体;}*///#include<iostream>//usingnamespacestd;//template<classT>//TGetMax(Ta,Tb)//{// Tresult;// result=(a>b?a:b);// returnresult;//}//intmain()//{// inti=5,j=6,k;//......