• 5-7 点到原点的距离(多态)


    给出下面的一个基类框架:

    class Point
    {
     protected:
    int dimension;//点的维数,最多不超过100维
    private:
     int point_length[100];//点数组
    public:
    Point ();//构造函数根据需要重载
    float distance( );//计算当前点到原点的距离
    void display();//输出点
    }
    

    以Point为基类建一个派生类Point_2D,增加以下数据成员:

    float x;//2D平面上点的x坐标
    float y;//2D平面上点的y坐标
    

    增加以下成员函数:

    Point_2D类的无参和参数化构造函数
    float distance( );//计算当前点到原点的距离
    void display();//输出点
    

    以Point为基类建一个派生类Point_3D,增加以下数据成员:

    float x;//3D平面上点的x坐标
    float y;//3D平面上点的y坐标
    float y;//3D平面上点的z坐标
    

    增加以下成员函数:

    Point_3D类的无参和参数化构造函数
    float distance( );//计算当前点到原点的距离
    void display();//输出点
    

    生成上述类并编写主函数,要求主函数有一个基类Point指针数组pt,数组元素不超过10个

    Point *pt [10];
    

    主函数根据输的点信息,相应建一个广义点对象类对象或Point_3D类对象或Point_2D类,并且取址按序赋给基类指针数组元素,最后遍历基类Point指针数组 pt,计算每一个点到原点的距离。

    输入格式:

    测试输包含一个测试用例,每个测试用例占一行,每一行给出一个点的基本信息,每行的第一个数字为当前点的类型,1为广义点,1后面输入一个数字m表示该点维数,后面跟随m个数字为m个坐标,2为2维点,后面跟随两个数字,分别为x和y,3为3维点,后面跟随三个数字分别为x、y、z。

    提示:应用虚函数实现多态

    输入样例:

    1 5 1 1 1 1 1
    2 3 4
    3 1 2 2
    0
    

    输出样例:

    Distance from Point(1,1,1,1,1) to original point is 2.23607
    Distance from Point(3,4) to original point is 5
    Distance from Point(1,2,2) to original point is 3


    • 时间限制:400ms
    • 内存限制:64MB
    • 代码长度限制:16kB
    • 判题程序:系统默认
    • 作者:余春艳
    • 单位:浙江大学
    #include<iostream>
    #include<cmath>
    using namespace std;
    //广义点 
    class Point
    {
    protected:
        int dimension;//点的维数,最多不超过100维
    private:
        float point_length[100];//点数组
    public:
        Point ( ){};//构造函数根据需要重载
        Point (int a , float b[]) : dimension(a){
            //这里实现传指针赋值给数组
            for(int i=0;i<a;i++)
            {
                point_length[i] = b[i];
            }
        };
        virtual float distance( ){
            float ans=0;
            for(int i=0;i<dimension;i++)
            {
                ans += (point_length[i] * point_length[i]);
            }
            return sqrt(ans);
        };
        virtual void display( ){
        //Distance from Point(1,1,1,1,1) to original point is 2.23607
        if(dimension == 1 ){
            cout<<"Distance from Point"<<point_length[0]<<" to original point is " << fabs(point_length[0]) <<endl;
        }
        else{
            cout<<"Distance from Point(";
            for(int i=0;i<dimension-1;i++)
            {
                cout<<point_length[i]<<",";
            }
            cout<<point_length[dimension-1]<<") to original point is "<<distance()<<endl;
            };        
        }
    
    };
    //二维点 
    class Point_2D : public Point{
    protected:
        float x;//2D平面上点的x坐标
        float y;//2D平面上点的y坐标
    public:    
        Point_2D( ) { };//无参和参数化构造函数
        Point_2D(float a,float b) : Point( ) , x(a) , y(b){ dimension = 2; };
        float distance( ) { return sqrt(x*x + y*y); };//计算当前点到原点的距离
        void display( ) { 
        //Distance from Point(3,4) to original point is 5
            cout<<"Distance from Point("<<x<<","<<y<<") to original point is "<<distance()<<endl;
        };//输出点
    }; 
    //以Point为基类建一个派生类Point_3D,增加以下数据成员:
    //三维点 
    
    class Point_3D : public Point{
    protected:
        float x;//3D平面上点的x坐标
        float y;//3D平面上点的y坐标
        float z;//3D平面上点的z坐标
    public:/**/
        Point_3D( ){ };//无参和参数化构造函数
        Point_3D(float a,float b,float c) : Point( ) , x(a) , y(b) , z(c) { dimension = 3; };
        float distance( ) { return sqrt( x*x + y*y + z*z ); };//计算当前点到原点的距离
        void display( ){
        //Distance from Point(1,2,2) to original point is 3
        cout<<"Distance from Point("<<x<<","<<y<<","<<z<<") to original point is "<<distance()<<endl;
        } ;//输出点
    };
    //****注意!派生类的成员成员函数要有定义,不然编译不过“returned 1” 
    
    
    int main()
    {
        int type,count=0;
        Point *pt [10];
        
        while(cin>>type){
            if(type == 0)break;                                            
            switch(type){
            case 1:{
                int len;
                float hello[100];
                cin>>len;
                for(int i=0;i<len;i++){
                    cin>>hello[i];
                } 
                pt[count++] = new Point(len,hello);             
                break;
            }
            case 2:{
                float a,b;
                cin>>a>>b;
                pt[count++] = new Point_2D(a,b);            
                break;
            }
            case 3:{
                float a,b,c;
                cin>>a>>b>>c;
                pt[count++] = new Point_3D(a,b,c);
                break;
            }
            }
        }
        for(int i=0;i<count;i++) 
        {
            pt[i]->display();
        }
        return 0;
    }
    个人分享,欢迎指导,未经允许,请勿转载。谢谢!
  • 相关阅读:
    http header Contenttype
    SharePoint 2010 文档库中直接打开文档
    玩转Google开源C++单元测试框架Google Test系列(gtest)(总)
    最近感兴趣的二三事
    最近遭遇的两个VS配置
    环游世界,走遍读过的每一个国家和城镇
    趣题一则:如何快速过桥?
    NASA庆祝地球日:50年地球最精美图片亮相(转载)
    Silverlight,Windows 8应用开发实例教程系列汇总
    Windows 8实例教程系列 数据绑定高级实例
  • 原文地址:https://www.cnblogs.com/hello-OK/p/7045737.html
Copyright © 2020-2023  润新知