Skip to Content
Practical C++ Programming, 2nd Edition
book

Practical C++ Programming, 2nd Edition

by Steve Oualline
December 2002
Beginner to intermediate
576 pages
14h 6m
English
O'Reilly Media, Inc.
Content preview from Practical C++ Programming, 2nd Edition

Chapter 21. Advanced Classes

The ruling ideas of each age have ever been the ideas of its ruling class.

Karl Marx, Manifesto of the Communist Party

This chapter discusses derived classes, virtual functions, and virtual classes.

Derived Classes

Suppose we want a stack that allows us to push on three items at a time in addition to performing all usual operations of a stack.[1] If we parse this statement in C++ terms, we discover something significant. We want a stack that:

  1. Does all the operations of a typical stack. (In C++ this is called a base class.)

  2. Expands on this by allowing us to do something more: specifically, push things on in groups of threes. (C++ calls this a derived class.)

Our basic stack is defined in Example 13-2.

We need to define a new expanded stack, which allows us to push multiple items. We call this an m_stack. This new stack does everything a simple stack does but also lets you push three items on at once. C++ allows you to build new classes on old ones. In this case we will be building our multiple-push stack (m_stack) on the existing simple stack (stack). Technically we will be using the class stack as a base class to create a new derived class, the multiple-push stack.

We start by telling C++ that we are creating m_stack out of stack:

class m_stack: public stack {

The keyword public tells C++ to make all the public members of stack accessible to the outside world. If we declared stack as private, the public and protected members of stack would be accessible only ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Modern C++ Programming Cookbook - Second Edition

Modern C++ Programming Cookbook - Second Edition

Marius Bancila
C++ Today

C++ Today

Jon Kalb, Gašper Ažman

Publisher Resources

ISBN: 0596004192Errata Page