



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Learn about inheritance in object-oriented programming, a key feature that allows defining general classes and creating more specific classes that inherit their properties. Discover how it saves time and reduces duplicate code, and explore real-life examples using C++ and the concept of an 'IS-A' hierarchy. Understand the relationship between inheritance and polymorphism, and discover how to create derived classes and access private variables.
What you will learn
Typology: Lecture notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Inheritance The concept of inheritance is one of the key features of an object-oriented programming language. Inheritance allows a programmer to define a general class, and then later define more specific classes that share or inherit all of the properties of the more general class. This allows the programmer to save time and energy that might otherwise be spent writing duplicate code. Related to inheritance is the concept of polymorphism. Polymorphism allows us to invoke the “correct” method in an inheritance hierarchy. We’ll see how this is used later. The idea of inheritance is similar to the taxonomy of living things. You probably recall the following hierarchy from a biology class: For example, to zoom in on the deer family (cervid), we could make a tree like the following.
Note that this hierarchy is an “IS-A” hierarchy. A deer “IS-A” cervid. In turn, a cervid “IS-A” ruminant, and so forth. The term “parent” is the term used to describe concepts going up the hierarchy, and “child” is the term when going down the hierarchy (imagine a family tree). For example, a deer is a “child” of cervid, and cervid is the “parent” of deer. Note that any property that holds for a parent also holds for a child. For example, if we know that mammals are warm-blooded, then we know that everything listed below mammal is also warm-blooded. We’ll take advantage of this concept from a programming perspective to reduce duplicative code. For an example in C++, recall the CreditCardTransaction class that we wrote earlier: class CreditCardTransaction { public : void CreditCardTransaction(); void CreditCardTransaction(string name, string number, double amount); string getName(); string getNumber(); double getAmount(); private : string name; string number; double amount; }; There are other kinds of transactions we might want to support, such as CashTransaction or CheckTransaction or PaypalTransaction. Let’s just look at how we might write CheckTransaction:
methods defined in a parent class can’t be accessed directly from a derived class. There is a modifier, protected , that allows access in any derived classes but not any other class. To create a derived class, we add a colon after the class definition then the word “public” followed by the name of the base class. E.g.: class CreditCardTransaction : public Transaction (There is also a type of private inheritance, but we won’t cover it here.) Here is the first version of our new class for Transaction. We only put in code that is relevant to all sub-classes. Here is a simple version of our classes; this is where we need to organize them in separate header and implementation files: File: Transaction.h #pragma once #include
string Transaction::getName() { return name; } double Transaction::getAmount() { return amount; } File: CreditCardTransaction.h #pragma once #include
We have now redefined the print method if we invoke it from a CreditCardTransaction object: CreditCardTransaction t2("Chuck Jones", "1234 1234 1234 1234", 55.45); t2.print(); // prints out name, amount. number Note that if we create a Transaction object and call print, then it calls the Transaction print function. If for some reason we want to we can also force calling the Transaction print function for a CreditCardTransaction object: Transaction t3("Bob", 10.05); t3.print(); t2.Transaction::print(); Next time we will do something different than redefining a function – instead we’ll make a virtual function, which turns out to be one of the big ideas in object-oriented programming. Takeaways