| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 751 人关注过本帖
标题:自学C++中的类时候出现的问题,请帮下忙了。。
只看楼主 加入收藏
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
结帖率:96.23%
收藏
 问题点数:0 回复次数:6 
自学C++中的类时候出现的问题,请帮下忙了。。
#include<iostream>   //题目是这样的,先建立一个抽象的基类就是这个shape,然后派生出point,circle,volum等派生类。最后在主函数里定义shape类的指针pt,使它
#include<string>       可以指向基类和派生类的数据及函数,但是我这个主函数没写2句就错误了。。我定义的shape 指针pt无法指向shape的派生类。。
using namespace std;    特别请教错误啊。。
class shape
{
public:
     virtual float area() {return 0;}
     virtual float volume() {return 0;}
    virtual void shapename() =0;
};
class point :public shape
{
public:
    point(float a=0,float b=0):x(a),y(b){}
    void setnumber()
    {
        cout<<"please input the x"<<endl;
        cin>>x;
        cout<<"please input the y"<<endl;
        cin>>y;
    }
    virtual void shapename()
    {
        cout<<"this porject is a point"<<endl;
    }
    friend ostream &operator <<(ostream &,point &);
protected:
    float x;
    float y;
};
class circle :public point
{
public:
    circle(float a=0,float b=0,float r=0):point(a,b),radius(r){}
    void setradius()
    {cout<<"please input the radius"<<endl;
    cin>>radius;
    }
    virtual float area()
    {
        return(3.14*radius*radius);
    }
    virtual void shapename()
    {
        cout<<"this project is circle"<<endl;
    }
    friend ostream &operator <<(ostream &,circle &);
protected:
    float radius;
};
ostream &operator <<(ostream &output,circle &c1)
{output<<"("<<c1.x<<","<<c1.y<<")"<<c1.radius<<c1.area()<<endl;
return output;
}

class cylinder :public circle
{
public:
    cylinder(float a=0,float b=0,float r=0,float h=0):circle(a,b,r),height(h){}
    void setheight()
    {cout<<"please input the height"<<endl;
    cin>>height;
    }
    virtual float volume()
    {
        return(3.14*radius*radius*height);
    }
    virtual void shapename()
    {
        cout<<"this project is cyliner"<<endl;
    }
    friend ostream &operator <<(ostream &,cylinder &);
protected:
    float height;
};
ostream &operator <<(ostream &output,cylinder &c1)
{output<<"("<<c1.x<<","<<c1.y<<")"<<c1.radius<<c1.area()<<" "<<c1.height<<" "<<c1.volume()<<endl;
return output;
}
int main()
{
 shape *pt;
 pt=&point;//!这就是错误的地方,pt指针无法变为指向point类的。
搜索更多相关主题的帖子: namespace include public return 
2013-03-12 23:30
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
shape* pt = &point;
相当于
int* p = &int;
能正确吗?
2013-03-13 08:33
路由zzz
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-3-11
收藏
得分:0 
要先将类实例化成对象。。

point只是数据类型。。

楼上好精辟啊!!
2013-03-13 13:09
明天更好山鹰
Rank: 2
等 级:论坛游民
威 望:1
帖 子:23
专家分:30
注 册:2013-3-11
收藏
得分:0 
回复 楼主 fl8962
point a;
shape *pt;
pt=&a;
这样就能使指针指向不同对象时执行不同的操作,体现多态性。
2013-03-14 15:13
贝斯007
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:3
专家分:2
注 册:2013-3-7
收藏
得分:0 
我也遇到了一个问题   就是不知道怎么在这个上面发帖     好郁闷
2013-03-14 15:57
现在是个学生
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2013-3-16
收藏
得分:0 
回复 5楼 贝斯007
点击用户控制面板,“我的博客”,就可以写了。
2013-03-17 13:24
现在是个学生
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2013-3-16
收藏
得分:0 
学好C程序,是一项大工程。
2013-03-17 13:25
快速回复:自学C++中的类时候出现的问题,请帮下忙了。。
数据加载中...
 
   



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

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020907 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved