Читаем C++ Primer Plus полностью

The new feature to note in Listings 13.14 and 13.15 is how derived classes can make use of a friend to a base class. Consider, for example, the following friend to the hasDMA class:

friend std::ostream & operator<<(std::ostream & os,


                                 const hasDMA & rs);

Being a friend to the hasDMA class gives this function access to the style member. But there’s a problem: This function is not a friend to the baseDMA class, so how can it access the label and rating members? The solution is to use the operator<<() function that is a friend to the baseDMA class. The next problem is that because friends are not member functions, you can’t use the scope-resolution operator to indicate which function to use. The solution to this problem is to use a type cast so that prototype matching will select the correct function. Thus, the code type casts the type const hasDMA & parameter to a type const baseDMA & argument:

std::ostream & operator<<(std::ostream & os, const hasDMA & hs)


{


//  type cast to match operator<<(ostream & , const baseDMA &)


    os << (const baseDMA &) hs;


    os << "Style: " << hs.style << endl;


    return os;


}

Listing 13.16 tests the baseDMA, lacksDMA, and hasDMA classes in a short program.

Listing 13.16. usedma.cpp


// usedma.cpp -- inheritance, friends, and DMA


// compile with dma.cpp


#include


#include "dma.h"


int main()


{


    using std::cout;


    using std::endl;



    baseDMA shirt("Portabelly", 8);


    lacksDMA balloon("red", "Blimpo", 4);


    hasDMA map("Mercator", "Buffalo Keys", 5);


    cout << "Displaying baseDMA object:\n";


    cout << shirt << endl;


    cout << "Displaying lacksDMA object:\n";


    cout << balloon << endl;


    cout << "Displaying hasDMA object:\n";


    cout << map << endl;


    lacksDMA balloon2(balloon);


    cout << "Result of lacksDMA copy:\n";


    cout << balloon2 << endl;


    hasDMA map2;


    map2 = map;


    cout << "Result of hasDMA assignment:\n";


    cout << map2 << endl;


    return 0;


}


Here’s the output of the program in Listings 13.14, 13.15, and 13.16:

Displaying baseDMA object:


Label: Portabelly


Rating: 8



Displaying lacksDMA object:


Label: Blimpo


Rating: 4


Color: red



Displaying hasDMA object:


Label: Buffalo Keys


Rating: 5


Style: Mercator



Result of lacksDMA copy:


Label: Blimpo


Rating: 4


Color: red



Result of hasDMA assignment:


Label: Buffalo Keys


Rating: 5


Style: Mercator

Class Design Review

C++ can be applied to a wide variety of programming problems, and you can’t reduce class design to some paint-by-numbers routine. However, there are some guidelines that often apply, and this is as good a time as any to go over them by reviewing and amplifying earlier discussions.

Member Functions That the Compiler Generates for You

As first discussed in Chapter 12, the compiler automatically generates certain public member functions, termed special member functions. The fact that it does so suggests that these special member functions are particularly important. Let’s look again at some of them now.

Default Constructors

A default constructor is one that has no arguments, or else one for which all the arguments have default arguments. If you don’t define any constructors, the compiler defines a default constructor for you. Its existence allows you to create objects. For example, suppose Star is a class. You need a default constructor to use the following:

Star rigel;        // create an object without explicit initialization


Star pleiades[6];  // create an array of objects

One more thing an automatic default constructor does is call the default constructors for any base classes and for any members that are objects of another class.

Also if you write a derived-class constructor without explicitly invoking a base-class constructor in the member initializer list, the compiler uses the base class default constructor to construct the base class portion of the new object. If there is no base-class default constructor, you get a compile-time error in this situation.

If you define a constructor of any kind, the compiler does not define a default constructor for you. In that case, it’s up to you to provide a default constructor if one is needed.

Note that one of the motivations for having constructors is to ensure that objects are always properly initialized. Also if a class has any pointer members, they certainly should be initialized. Thus, it’s a good idea to supply an explicit default constructor that initializes all class data members to reasonable values.

Copy Constructors

A copy constructor for a class is a constructor that takes an object of the class type as its argument. Typically, the declared parameter is a constant reference to the class type. For example, the copy constructor for a Star class would have this prototype:

Star(const Star &);

A class copy constructor is used in the following situations:

• When a new object is initialized to an object of the same class

• When an object is passed to a function by value

• When a function returns an object by value

Перейти на страницу:

Все книги серии Developer's Library

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных

Похожие книги

1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных