CS 162 Lecture Notes - Lecture 15: Memory Management

48 views5 pages
CS 162 Lecture 15 Inheritance Details
Review: Inheritance
o The process by which a new class is created from another class
o Base (parent) class == more general class which derived class are created from
o Derived (child) class == new class
Has all of the member variables and functions as base class
o Class Derived:public Base {};
o In the derived class:
List only member variables you want to add, not what is inherited
Only redeclare inherited member functions if you want to redefine them
Redefining is when an inherited member function definition is
changed in the derived class
o Derived classes can be used anywhere the base class would be used but not the
other way around
Interface
o For child Instead of ending with }
Use private
Only say what is different than parent
o Declare the parent as normal
o The child:
Class Child:public parent {
Private:
o //any members which are unique to the child
public:
o Child(); //default constructor
o //other members including redefined functions from
parent
};
Implementation
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
o Parent class defined as a normal
o Takes everything that was inherited and initializes it, then looks at child
o Child:
Child::Child():parent() { //child makes call to parent constructor first
//initialize the Child
}
// define all other member functions as normal
//redefining of parent functions follows the normal way of defining
functions
Inheritance with the Big Three
o Recall: Big 3 are needed whenever there is dynamic memory or pointers, they
are not inherited from the parent. To use in child successfully, they must be
defined correctly in parent
o Destructor is different
Destructors get called in reverse when out of scope
o Child& Child::operator = (const Child& other){
Parent::operator = (other); //;invoke parent class AOO
// continue with things unique to child
o }
o Child:Child(const Child& copy):Parent(copy) {
// continue with things unique to child
o }
o Child::~Child(){
//define as normal, parent will be automatically called after the child
completes
// destructors go in the reverse of constructors calls
Public vs. Private vs. Protected Inheritance
o Anything public in the parent is public to the child
o Anything private in the parent is private to the child
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents