| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1334 人关注过本帖
标题:新人求助,Point &p=NULL;
取消只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
结帖率:90%
收藏
已结贴  问题点数:10 回复次数:0 
新人求助,Point &p=NULL;
程序代码:
#include<iostream>
using namespace std;
class Point
{
    public:
    Point(){}
    Point(int X,int Y)
    {
        x=X;
        y=Y;
    }

   virtual void set(int X,int Y)
    {
       x=X;
       y=Y;
    }
     
    virtual void display()
     {
         cout<<"x="<<x<<endl;
         cout<<"y="<<y<<endl;
     }
    protected:
    int x;
    int y;
};
class Circle:public Point
{
    public:
    Circle(){}
    Circle(int X,int Y,int Radius):Point(X,Y)
    {
        x=X;
        y=Y;
        radius=Radius;
    }
    void set(int X,int Y,int Radius)
    {
        x=X;
        y=Y;
        radius=Radius;
    }
    void display()
        {
         cout<<"x="<<x<<endl;
         cout<<"y="<<y<<endl;
         cout<<"半径="<<radius<<endl;
         }
    protected:
    int radius;
};
class Cylinder:public Circle
{
    public:
    Cylinder(){}
    Cylinder(int X,int Y,int Radius, int Height):Circle(X,Y,Radius)
    {
        x=X;
        y=Y;
        radius=Radius;
        height=Height;
    }
    void set(int X,int Y,int Radius, int Height)
    {
        x=X;
        y=Y;
        radius=Radius;
        height=Height;

    }
    void display()
    {
         cout<<"x="<<x<<endl;
         cout<<"y="<<y<<endl;
         cout<<"半径="<<radius<<endl;
         cout<<"高="<<height<<endl;
    }
    private:
    int height;
};
int main()
{
    Point &p=NULL;
    Point t;
    
    t.set(1,2);
    t.display();

    Circle c;
    Cylinder d;

    p=&c;
    c->set(1,2,3);
    c->display();

    p=&d;
    d->set(1,2,3,4);
    d->display();
    
    return 0;
}




运行结果:
/tmp/815282660/main.cpp:84:12: error: non-const lvalue reference to type 'Point' cannot bind to a temporary of type 'long'
    Point &p=NULL;
           ^ ~~~~
/tmp/815282660/main.cpp:94:6: error: member reference type 'Circle' is not a pointer; maybe you meant to use '.'?
    c->set(1,2,3);
    ~^~
     .
/tmp/815282660/main.cpp:95:6: error: member reference type 'Circle' is not a pointer; maybe you meant to use '.'?
    c->display();
    ~^~
     .
/tmp/815282660/main.cpp:98:6: error: member reference type 'Cylinder' is not a pointer; maybe you meant to use '.'?
    d->set(1,2,3,4);
    ~^~
     .
/tmp/815282660/main.cpp:99:6: error: member reference type 'Cylinder' is not a pointer; maybe you meant to use '.'?
    d->display();
    ~^~
     .
5 errors generated.

exit status 1
搜索更多相关主题的帖子: Point int set display cout 
2018-06-01 15:02
快速回复:新人求助,Point &p=NULL;
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020229 second(s), 9 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved