| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 687 人关注过本帖
标题:一个典型的例子--谭浩强《C++程序设计》求助!
只看楼主 加入收藏
hmsabc
Rank: 2
来 自:贵州省兴义市
等 级:论坛游民
帖 子:97
专家分:19
注 册:2010-8-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
一个典型的例子--谭浩强《C++程序设计》求助!
程序代码:
//第一版例12.1 ( P396 )
#include <iostream>
using namespace std;
//声明类 Point
class Point
{
public:
    Point( float x = 0,float y = 0);                                 //有默认参数的构造函数
    void setPoint( float,float);                                     //设置坐标值
    float getX( ) const { return x;}                                 //读 x 坐标
    float getY( ) const { return y;}                                 //读 y 坐标
    friend ostream & operator << ( ostream &,const Point &);         //重载运算符
protected:                                                           //受保护成员
    float x,y;
};
//下面定义 Point 类的成员函数
Point::Point( float a,float b)                                       //Point 的构造函数
{ x = a; y = b;}                                                     //对 x,y 初始化
void Point::setPoint(float a,float b)                                //设置 x 和 y 的坐标
{ x = a; y = b;}                                                     //为 x,y 赋新值
ostream & operator << ( ostream &output,const Point &p)              //重载运算符“ << ”,使之能输出点的坐标
{ output << "[" << p.x << "," << p.y << "]" << endl;
return output;
}
//第一次测试主函数
//int main( )
//{
//    Point p(3.5,6.4);                                                //建立 Point 类对象 p
//    cout << "x = " << p.getX( ) << ", y = " << p.getY( ) <<endl;     //输出 p 的坐标
//    p.setPoint( 8.5,6.8);                                            //重新设置 p 的坐标值
//    cout << "p(new): " << p << endl;
//    system("pause");
//    return 0;
//}


//声明派生类 Circle
class Circle:public Point                                             // circle 是 Point 类的公用派生类
{
public:
    Circle( float x = 0,float y = 0,float r = 0);                   //构造函数
    void setRadius( float);                                         //设置半径值
    float getRadius( ) const;                                       //读取半径值
    float area( ) const;                                            //计算圆面积
    friend ostream &operator << ( ostream &,const Circle &);        //重载运算符 “ << ”
private:
    float radius;
};

Circle::Circle( float a, float b, float r):Point( a,b),radius( r){ }    //定义构造函数,对圆心和半径初始化
void Circle::setRadius( float r)                                        //设置半径值
{ radius = r;}                                                         
float Circle::getRadius( ) const { return radius;}                      //计算圆面积
float Circle::area( ) const
{ return 3.14159 * radius * radius;}
ostream &operator << ( ostream &output,const Circle &c)               //重载运算符“<<”,使之按规定的形式输出圆的信息
{ output << "Center = ["<< c.x <<","<<c.y<<"],r = ";
  output <<c.radius<<",area = "<<c.area( )<<endl;
return output;
}

//第二次测试主函数
//int main( )
//{ Circle c( 3.5,6.4,5.2);                              //建立 Circle 类对象 c,并给定圆心坐标和半径
//cout<<"original circle:\nx = "<<c.getX( )<<",y =";
//cout<<c.getY( )<<",r ="<<c.getRadius( );
//cout<<",area ="<<c.area( )<< endl;                     //输出圆心坐标、半径和面积                         
//c.setRadius(7.5);                                      //设置半径值
//c.setPoint( 5,5);                                      //设置圆心坐标值 x,y
//cout<<"new circle:\n"<<c;                              //用重载运算符“<< ”输出圆对象的信息
//Point &pRef = c;                                       //pRef 是 Point 类的引用变量,被 c 初始化
//cout<<"pRef:"<<pRef;                                   //输出 pRef 的信息
//system("pause");
//return 0;
//}


//声明 Circle 的派生类 Cylinder
class Cylinder:public Circle                            // Cylinder 是 Circle 的公用派生类
{ public:
Cylinder(float x = 0,float y = 0,float r = 0,float h = 0);               //构造函数
void setHeight(float);                                                   //设置圆柱高
float getHeight( ) const;                                                //读取圆柱高
float area( ) const;                                                     //计算圆表面积
float volume( ) const;                                                   //计算圆柱体积
friend ostream& operator<<( ostream&,const Cylinder&);                   //重载运算符“<<”
protected:
    float height;                                                        //圆柱高
};

Cylinder::Cylinder( float a, float b,float r, float h):Circle(a,b,r),height(h){ }     //定义构造函数
void Cylinder::setHeight( float h){ height = h;}                                     //设置圆柱高
float Cylinder::getHeight( ) const { return height;}                                 //读取圆柱高
float Cylinder::area( ) const                                                        //计算圆柱表面积
{ return 2 * Circle::area( ) + 2 * 3.14159 * radius * height;}                       
float Cylinder::volume( ) const                                                      //计算圆柱体积
{ return Circle::area( ) * height;}
ostream &operator<<(ostream &output,const Cylinder& cy)
{ output<<"Center=["<<cy.x<<","<<cy.y<<"],r = "<<cy.radius<<",h = "<<cy.height
<<"\narea = "<<cy.area( )<<",volume = "<<cy.volume( ) <<endl;
return output;
}

//第三次测试主函数
int main( )
{
    Cylinder cy1( 3.5,6.4,5.2,10);                                                 //定义 Cylinder 类对象 cy1
    cout<<"\noriginal cylinder:\nx="<<cy1.getX( )<<",y = "<<cy1.getY( ) <<",r = "
        <<cy1.getRadius( ) <<", h = " <<cy1.getHeight( ) << "\narea = "
        << cy1.area( )<<",volume = "<<cy1.volume() << endl;               //用系统定义的运算符“<<”输出 cy1 的数据
    cy1.setHeight( 15);                                                   //设置圆柱高
    cy1.setRadius(7.5);                                                   //设置圆半径
    cy1.setPoint( 5,5);                                                   //设置圆心坐标值
    cout<<"\nnew cylinder:\n"<<cy1;                                       //用重载运算符“<<”输出 cy1 的数据
    Point &pRef = cy1;                                                    //pRef 是 Point 类对象的引用变量
    cout<<"\npref as a Point:"<<pRef;                                     //pRef 作为一个“点”输出
    Circle &cRef = cy1;                                                   //cRef 是 Circle 类对象的引用变量
    cout<<"\ncRef as a Circle:"<<cRef;                                    //cRef 作为一个“圆”输出
    return 0;
}
前两部分已经通过测试,最后这部分不知道为什么出错了,提示不能访问私有成员函数,可我这是公有继承呀!请高师帮帮忙,先谢了!
搜索更多相关主题的帖子: 谭浩强 例子 典型 
2010-08-10 10:28
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
收藏
得分:10 
公有继承中,子类对象也不能直接访问父类的私有函数啊
2010-08-10 12:47
hmsabc
Rank: 2
来 自:贵州省兴义市
等 级:论坛游民
帖 子:97
专家分:19
注 册:2010-8-2
收藏
得分:0 
回复 2楼 jjg
知道了,谢谢!
2010-08-10 15:36
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:10 
嗯,公有继承是属性不变。public -> public, private -> private。
2010-08-11 02:15
快速回复:一个典型的例子--谭浩强《C++程序设计》求助!
数据加载中...
 
   



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

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