英文翻译样品
object to
                            原文
Selected from Chapter 13 of Stephen Prata’s<C++ Primer Plus>,Fourth Edition.
Summary
Inheritance enables you to adapt programming code to your particular needs by defining a new class (a derived class) from an existing class (the base class). Public inheritance models an is-a relationship, meaning a derived-class object also should be a kind of base-class object. As part of the is-a model, a derived class inherits the data members and most methods of the base class. However, a derived class doesn't inherit the base class constructors, destructors, and assignment operators. A derived class can access the public and protected members of the base class directly and the private base class members via the public and protected base-class methods. You then can add new data members and methods to the class, and you can use the derived class as a base class for further develop
ment. Each derived class requires its own constructors. When a program creates a derived class object, it first calls a base class constructor and then the derived class constructor. When a program deletes an object, it first calls the derived class destructor and then the base class destructor.
If a class is meant to be a base class, you may choose to use protected members instead of private members so that derived classes can access those members directly. However, using private members will, in general, reduce the scope for programming bugs. If you intend that a derived class can redefine a base class method, make it a virtual function by declaring it with the keyword virtual. This enables objects accessed by pointers or references to be handled on the basis of the object type rather than on the basis of the reference type or pointer type. In particular, the destructor for a base class normally should be virtual.
You might want to define an ABC (abstract base class) that defines an interface without getting into implementation matters. For example, you could define an abstract Shape cla
ss from which particular shape classes, such as Circle and Square, will be derived. An abstract base class must include at least one pure virtual method. You can declare a pure virtual method by placing = 0 before the closing semicolon of the declaration.
virtual double area() const = 0;
You don't have to define pure virtual methods, and you can't create an object of a class containing pure virtual members. Instead, they serve to define a common interface to be used by derived classes
                            译文
节选自Stephen Prata的《C++ Primer Plus》第四版第十三章。
本章总结
  继承通过用已有的类(基类)来定义新类(派生类),使你能够根据需要修改程序代码。公有继承建立的是is-a关系,即派生类对象也是某种基类的对象。作为一种is-a关系,派生
类将继承基类的所有数据及大部分方法而不继承其构造函数,析构函数和赋值操作符。派生类可以直接访问基类的公有及保护成员,而对基类的私有成员则需通过基类的公有或保护方法来访问。你可以在派生类中添加新的数据成员或方法,还可以其为基类开发新的派生类。
每个派生类都要有自己的构造函数。程序创建派生类对象时将先调用基类的构造函数,再调用派生类的构造函数;而删除对象时将先调用派生类的析构函数,再调用基类的析构函数。
  若要将某类用做基类,可将其某些成员设定为保护的而不是私有的,使得派生类可以直接访问它们;不过,设定私有成员大体上可以避免一些编程问题的出现。若想在派生类中重新定义基类中的方法,则需要在基类中使用virtual关键字将此方法设定为虚方法。 从而使得对通过指针或引用来访问的对象的处理将由其引用的对象的类型而不是由指针或引用的自身的类型来决定。特别地,基类的析构函数常设为虚函数。
  你可以定义一个ABC(抽象基类),它将只定义接口而没有提供实现。如:可以定义一个名为Shape(形状)的抽象基类用以派生出具体的如Circle(圆形),Square(正方形)等
图形类。作为ABC,它必须有至少一个纯虚方法;可以在类中虚方法声明语句的结尾分号之前加=0,是之成为纯虚方法声明:
  virtual double area() const = 0;
不过纯虚方法并不一定得需要定义,而且包含纯虚成员的类是不能直接用于创建对象的,因为纯虚函数仅仅提供了用于派生类定义的通用接口而已。
                                                苏凡
                                                2009.01.05

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。