Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Inheritance in Object-Oriented Programming: Saving Time with Shared Properties and Methods, Lecture notes of Object Oriented Programming

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

  • How can we add a CashTransaction class using inheritance?
  • Where should we put a 'sendToLedger' function that applies to all kinds of transactions?
  • Where is the best place to write a function to verify a credit card number?

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

arwen
arwen 🇬🇧

4.3

(10)

249 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf3
pf4
pf5

Partial preview of the text

Download Inheritance in Object-Oriented Programming: Saving Time with Shared Properties and Methods and more Lecture notes Object Oriented Programming in PDF only on Docsity!

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 #include using namespace std; class Transaction { public: // Constructors Transaction(); // Default constructor Transaction(string newName, double newAmount); string getName(); double getAmount(); private: string name; double amount; }; File: Transaction.cpp #include #include #include "Transaction.h" using namespace std; Transaction::Transaction() { name = "Unknown"; amount = 0; } Transaction::Transaction(string newName, double newAmount) : name(newName), amount(newAmount) { }

string Transaction::getName() { return name; } double Transaction::getAmount() { return amount; } File: CreditCardTransaction.h #pragma once #include #include using namespace std; class CreditCardTransaction : public Transaction { public: // Constructors CreditCardTransaction(); // Default constructor CreditCardTransaction(string newName, string newNumber, double newAmount); string getNumber(); private: string number; }; File: CreditCardTransaction.cpp #include "Transaction.h" #include "CreditCardTransaction.h" using namespace std; CreditCardTransaction::CreditCardTransaction() : Transaction(), number("") { } CreditCardTransaction::CreditCardTransaction(string newName, string newNumber, double newAmount) : Transaction(newName, newAmount), number(newNumber) { } string CreditCardTransaction::getNumber() { return number; }

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

  1. A derived class inherits public (or protected) methods from the base class
  2. Derived methods override methods of the same name in the base class
  3. Use the scope resolution operator to invoke methods from the base class
  4. Private variables are inherited by the derived class, but are not directly accessible by name. Public or protected variables are directly accessible by name. Multi-Level Classes In our example we only had one parent and one child class, but we could have a chain of multiple parents. In fact, in C++ it is possible for one class to have two separate parents, but this is very uncommon (and not allowed in most programming languages).