书上写的这个抽像类,能被继成吗?

pdaliu 2005-05-15 11:07:04
class Employee
{
public:
Employee(const char*,const char *);
~Employee();
const char *getFirstName() const;
const char *getLastName() const;
//
virtual double earnings() const=0;
virtual void print() const;
private:
char *firstName;
char *lastName;
}
书上说有纯虚函数的类,是不能实例化对像的,只能用来被别的类继成,那上面有两个private数据成员,这两个能被继成吗? 如果不能继成的话,在它的子类Boss类中调用它的构造函数是什么意思?如下:
Boss::Boss(const char *first,const char *last,double s)
:Employee(first,last)
(setWeeklySalary(s);}
...全文
71 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
useresu 2005-05-15
  • 打赏
  • 举报
回复
你既然在point中有getX(),和getY(),
他们都是public
circle可以通过他们访问x和y啊
pdaliu 2005-05-15
  • 打赏
  • 举报
回复
这个是我写的一个类~继成时就会出错~
//point.h
class point
{
friend ostream &operator<<(ostream &,const point &);
friend int operator+(const point &,const int &);
public:
point &operator=(const int &);
point &operator+=(const point&);
point(int=0,int=0);
~point();
void setPoint(int =0,int =0);
int getX() const{return x;}
int getY() const{return y;}
private:
int x,y;
};

//circle.h
class circle : public point
{
friend ostream &operator<<(ostream &,const circle &);
public:
circle(double=0,int=0,int=0);
~circle();

void setR(double);
double getR() const;
double getArea() const;
protected:
double r;
};
//circle.cpp
circle::circle(double r,int x,int y)
:point(x,y)
{
setR(r);
cout<<"circle 构建函数:"
<<r<<"["<<getX()<<","<<getY()<<"]"<<endl;
}//
circle::~circle()
{
cout<<"circle 析构函数"<<r<<"["<<x<<","<<y<<"]"<<endl;
}
double circle::getR() const
{
return r;
}//
void circle::setR(double r)
{this->r=r;}//
double circle::getArea() const
{
return 3.14159*r*r;
}//
ostream &operator<<(ostream &output,const circle &tem)
{
output<<tem.r<<'\n';
return output;
}//
在VC7下会有如下错误:
circle.cpp
f:\My document\study\main\main\circle.cpp(16) : error C2248: “point::x” : 无法访问 private 成员(在“point”类中声明)
f:\My document\study\main\main\point.h(19) : 参见“point::x”的声明
f:\My document\study\main\main\point.h(7) : 参见“point”的声明
f:\My document\study\main\main\circle.cpp(16) : error C2248: “point::y” : 无法访问 private 成员(在“point”类中声明)
f:\My document\study\main\main\point.h(19) : 参见“point::y”的声明
f:\My document\study\main\main\point.h(7) : 参见“point”的声明
qhfu 2005-05-15
  • 打赏
  • 举报
回复
virtual double earnings() const=0;
----------------
应该只是表示earnings这个方法的实现父类没有提供而已,要子类自己去具体实现。
------------------------------------------------------------------------
这不代表这个方法的实现父类没有提供,只是表示父类是虚拟父类,不能被实例话, 这个方法父类同样能实现。。
zhousqy 2005-05-15
  • 打赏
  • 举报
回复
virtual double earnings() const=0;
----------------
应该只是表示earnings这个方法的实现父类没有提供而已,要子类自己去具体实现。
useresu 2005-05-15
  • 打赏
  • 举报
回复
这里调用父类的构造函数是因为父类没有默认构造函数,

只能通过显示调用来调用父类的构造函数.

而两个private成员可能就是通过父类的构造函数来初始化的

useresu 2005-05-15
  • 打赏
  • 举报
回复
当然可以继承,

但是一般在父类中是private的成员都用父类的函数来初始化或赋值,

即对他们的控制和改变都是通过父类的函数来实现的,

在子类中是不能对他们进行访问控制的.
llf_hust 2005-05-15
  • 打赏
  • 举报
回复
那上面有两个private数据成员,这两个能被继成吗? //可以被派生类继承

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧