标题:公有继承
只看楼主
大剑
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
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-355790-1-1.html




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

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