| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 687 人关注过本帖
标题:公有继承
只看楼主 加入收藏
大剑
Rank: 2
等 级:论坛游民
帖 子:30
专家分:25
注 册:2011-11-16
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:2 
公有继承
问两个问题  小弟新手 问的幼稚请莫见怪
程序如下
#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;
class CRect
{  
    float length;
protected:
    float width;
    float get_length() { return length;}
public:
    float area(){return length * width;}
    CRect(float l,float w)
    { length=l;width=w;}
};
class CParal:public CRect
{
    float angle;
public:
    float area() {return (float)(length * width * sin(angle));}//erro
    CParal(float l,float w,float f):CRect(l,w)
    {
        angle=f;
    }
};
float PI=3.14;
void main()
{
    CParal paral(10,12,PI/6);
    cout<<"The area of paral is: "<<paral.area()<<endl;
}
 错误提示是这样的 error C2248: 'CRect::length' : cannot access private member declared in class 'CRect'
意思是否是length 是基类的的私有成员 不能直接访问  但我已经加了这句 float get_length() { return length;}
了啊   我开头这样写对不对#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;
搜索更多相关主题的帖子: include public return angle 
2011-11-20 21:14
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
class CRect
{
public:
    CRect( float length, float width ) : length_(length), width_(width)
    {
    }
    virtual ~CRect()
    {
    }
    virtual float area() const
    {
        return length_ * width_;
    }
    float get_length() const
    {
        return length_;
    }
    float get_width() const
    {
        return width_;
    }
private:
    float length_, width_;
};

#include <cmath>

class CParal : public CRect
{
public:
    CParal(float length, float width, float angle) : CRect(length,width), angle_(angle)
    {
    }
    virtual ~CParal()
    {
    }
    virtual float area() const
    {
        return (float)( get_length() * get_width() * sin(angle_) );
    }
    float get_angle() const
    {
        return angle_;
    }
private:
    float angle_;
};

#include <iostream>
using namespace std;

const float PI = 3.14f;

int main()
{
    CParal paral(10,12,PI/6);
    cout << "The area of paral is: " << paral.area() << endl;

    return 0;
}
2011-11-21 08:26
kscooh1
Rank: 2
等 级:论坛游民
帖 子:53
专家分:25
注 册:2011-8-8
收藏
得分:10 
私有的不能直接使用 只能调用函数
我改的可以运行了:
程序代码:
//#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;
class CRect
{ 
    float length;
protected:
    float width;
    float get_length() { return length;}
public:
    float area(){return length * width;}
    CRect(float l,float w)
    { length=l;width=w;}
};
class CParal:public CRect
{
    float angle;
public:
    float area() {return (float)(get_length() * width * sin(angle));}//erro
    CParal(float l,float w,float f):CRect(l,w)
    {
        angle=f;
    }
};
const float PI=3.14;
void main()
{
    CParal paral(10,12,PI/6);
    cout<<"The area of paral is: "<<paral.area()<<endl;
}

2011-11-21 09:30
快速回复:公有继承
数据加载中...
 
   



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

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