Suggestions
Use up and down arrows to review and enter to select.Please wait while we process your payment
If you don't see it, please check your spam folder. Sometimes it can end up there.
If you don't see it, please check your spam folder. Sometimes it can end up there.
Please wait while we process your payment
By signing up you agree to our terms and privacy policy.
Don’t have an account? Subscribe now
Create Your Account
Sign up for your FREE 7-day trial
Already have an account? Log in
Your Email
Choose Your Plan
Individual
Group Discount
Save over 50% with a SparkNotes PLUS Annual Plan!
Purchasing SparkNotes PLUS for a group?
Get Annual Plans at a discount when you buy 2 or more!
Price
$24.99 $18.74 /subscription + tax
Subtotal $37.48 + tax
Save 25% on 2-49 accounts
Save 30% on 50-99 accounts
Want 100 or more? Contact us for a customized plan.
Your Plan
Payment Details
Payment Summary
SparkNotes Plus
You'll be billed after your free trial ends.
7-Day Free Trial
Not Applicable
Renews December 7, 2023 November 30, 2023
Discounts (applied to next billing)
DUE NOW
US $0.00
SNPLUSROCKS20 | 20% Discount
This is not a valid promo code.
Discount Code (one code per order)
SparkNotes PLUS Annual Plan - Group Discount
Qty: 00
SparkNotes Plus subscription is $4.99/month or $24.99/year as selected above. The free trial period is the first 7 days of your subscription. TO CANCEL YOUR SUBSCRIPTION AND AVOID BEING CHARGED, YOU MUST CANCEL BEFORE THE END OF THE FREE TRIAL PERIOD. You may cancel your subscription on your Subscription and Billing page or contact Customer Support at custserv@bn.com. Your subscription will continue automatically once the free trial period is over. Free trial is available to new customers only.
Choose Your Plan
For the next 7 days, you'll have access to awesome PLUS stuff like AP English test prep, No Fear Shakespeare translations and audio, a note-taking tool, personalized dashboard, & much more!
You’ve successfully purchased a group discount. Your group members can use the joining link below to redeem their group membership. You'll also receive an email with the link.
Members will be prompted to log in or create an account to redeem their group membership.
Thanks for creating a SparkNotes account! Continue to start your free trial.
Please wait while we process your payment
Your PLUS subscription has expired
Please wait while we process your payment
Please wait while we process your payment
An important feature of object-oriented programming languages is inheritance: the ability to create derived classes. Consider the following class definition:
You might like to create a new class called Quad to represent a 4-sided figure; but since this is a type of polygon, it shares some of the same properties. In this simple example, num_sides might be a convenient property for the Quad class to keep track of, so you might choose to derive class Quad from class Polygon:
Quad is called a subclass of its superclass Polygon. Creating a derived class is similar to creating a new class, except you use the colon operator to indicate the superclass in the declaration, in the constructor, and in the destructor. Notice that in the notation for the constructor, the superclass's constructor is called with argument 4.
Any instance of a subclass has all the same member functions and data members as its superclass. Therefore Quad has a private data member num_sides and member function get_num_sides() (as well as the constructor Polygon() ). However, Quad cannot directly access its num_sides data member, even within the class definition, because the variable was declared to be private in the definition of Polygon. A superclass's private data member can only be accessed through the superclass's public member functions. If I wanted num_sides to be accessible to the Quad subclass, but still have the other properties of a private data member within Quad, I would have to declare num_sides as a protected variable in its superclass, Polygon. The protected keyword is used for any variable (or function) that should be directly accessible to subclasses, but should otherwise behave as a private data member.
Multiple inheritance is also allowed in C++. In the class definition, separate the superclasses by commas:
Be sure to use the scope operator generously; if two classes have the same variable name or function name, you need to be specific. Consider the following:
Class Kid attempts to access variable age in its get_age() function; but it is unclear whether this means Dad::age or Mom::age, so it is necessary to specify. A similar problem occurs when two superclasses share the same base class. Suppose there were a base class Grandparent with protected data member height, and Mom and Dad were subclasses of Grandparent:
The compiler will not like the ambiguity in class Kid. To fix this, include the word virtual in deriving Mom and Dad:
Then the compiler will have class Kid inherit only one subclass Grandparent. In addition, virtual classes have some other functionalities beyond the scope of this introduction. Having stated the problems and solutions of multiple inheritance, it's worth noting that it can generally be avoided by using composition; that is, by using would-be superclasses as data member objects:
This changes the structure of class Kid, but it may simplify programs by avoiding confusion.
The last fundamental topic about classes essential for C++ programming is that of class templates. Similar to function templates, class templates are useful if the data type of some component is not yet known, or if many data types will be used for the same kind of object. Consider the following class template that contains an array of some unknown type:
Different types of Array objects can be created:
To define member functions outside the class specification, use the syntax:
etc.
If you need class templates for "containers" (e.g. arrays, stacks, linked lists, or "vectors"), then before programming them yourself, look into the C++ Standard Template Library (STL); it contains class templates that can be very useful.
Please wait while we process your payment