英文翻译样品
数学学习方法原文
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 ba class). Public inheritance models an applicableis-a relationship, meaning a derived-class object also should be a kind of ba-class object. As part of the aabcis-a model, a derived class inherits the data members and most methods of the ba class. However, a derived class doesn't inherit the ba class constructors, destructors, and assignment operators. A derived class can access the public and protected members of the ba class directly and the private ba class members via the public and protected ba-class methods. You then can add new data members and methods to the class, and you can u the derived class as a ba class for further develop
ment. Each derived class requires its own constructors. When a program creates a derived class object, it first calls a ba class constructor and then the derived class constructor. When a program deletes an object, it first calls the derived class destructor and then the ba class destructor.
If a class is meant to be a ba class, you may choo to u protected members instead of private members so that derived class can access tho 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 ba class method, make it a virtual function by declaring it with the keyword virtual. This enables objects accesd 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 ba class normally should be virtual.
You might want to define an ABC (abstract ba class) that defines an interface without getting into implementation matters. For example, you could define an abstract Shape cla
小学一年级英语磁带ss from which particular shape class, such as Circle and workout造句Square, will be derived. An abstract ba class must include at least one pure virtual method. You can declare a pure virtual method by placing = 0 before the closing micolon 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 rve to define a common interface to be ud by derived class
译文
节选自Stephen Prata克里斯布朗的《C++ Primer Plus》第四版第十三章。
本章总结
继承通过用已有的类(基类)来定义新类(派生类),使你能够根据需要修改程序代码。公有继承建立的是is-a关系,即派生类对象也是某种基类的对象。作为一种is-a关系,派生
类将继承基类的所有数据及大部分方法而不继承其构造函数,析构函数和赋值操作符。派生类可以直接访问基类的公有及保护成员,而对基类的私有成员则需通过基类的公有或保护方法来访问。你可以在派生类中添加新的数据成员或方法,还可以其为基类开发新的派生类。
每个派生类都要有自己的构造函数。程序创建派生类对象时将先调用基类的构造函数,再调用派生类的构造函数;而删除对象时将先调用派生类的析构函数,再调用基类的析构函数。
健身教练考试 若要将某类用做基类,可将其某些成员设定为保护的而不是私有的,使得派生类可以直接访问它们;不过,设定私有成员大体上可以避免一些编程问题的出现。若想在派生类中重新定义基类中的方法,则需要在基类中使用去职virtual关键字将此方法设定为虚方法。 从而使得对通过指针或引用来访问的对象的处理将由其引用的对象的类型而不是由指针或引用的自身的类型来决定。特别地,基类的析构函数常设为虚函数。
你可以定义一个ABC(抽象基类),它将只定义接口而没有提供实现。如:可以定义一个名为Shape(形状)的抽象基类用以派生出具体的如Circle(圆形),Square(正方形)
等图形类。作为ABC,它必须有至少一个纯虚方法;可以在类中虚方法声明语句的结尾分号之前加=0,是之成为纯虚方法声明:
cross over
virtual double area() const = 0;
不过纯虚方法并不一定得需要定义,而且包含纯虚成员的类是不能直接用于创建对象的,因为纯虚函数仅仅提供了用于派生类定义的通用接口而已。
苏凡
2009.01.05