CS Week 8 - Inheritance and Polymorphism Flashcards
Derived class
a class that is derived from another class (base class or superclass)
Inheritance
the derived class inherits the properties of the base class
Declaring a derived class
place “:” after derived class name, followed by a member access specifier like “public” and base class name
class DerivedClass:public BaseClass {…};
member access
Members of a derived class have access to the public members of the base class, but not to the private members of the base class
protected
private
public
protected - accessible by self and derived classes
private - only accessible by self
public - accessible by anyone
specifiers in the class definition
public - “public->public, protected->protected” - public members of base are accessible as public members of Derived and protected members of base are accessible as protected members of derived
protected - “public->protected, protected->protected” - public and protected members of base are accessible as protected members of derived
private - “public->private, protected->private” - public and protected member of base are accessible as private members of derived, this is the default if none of these are typed (class derived :base{..};)
override
When a derived class defines a member function with same name and parameters as a base class function, member function overrides the base class function
override vs overloading
Overloading has different parameters, derived function does not hide base function
Overriding, all the same
hasa vs isa
Has-a - object has an object, no inheritance
Is-a - object is a kind of object
Polymorphism
determining which program behavior to execute depending on data types
Compile-time polymorphism
when the compiler determines which function to call at compile time
Runtime polymorphism
when the compiler is unable to determine which function to call at compile time, so the determination is made while the program is running
derived/base class pointer conversion
where a pointer to a derived class is converted to a pointer to the base class without explicit casting
Virtual function
member function that may be overridden in a derived class and is used for runtime polymorphism
Declared by prepending the keyword “virtual”, ex virtual string GetDescription() const
At runtime, when a virtual function is called using a pointer, the correct function to call is dynamically determined based on the actual object type to which the pointer or reference refers
override
keyword -optional - indicate that a virtual function is overridden in a derived class
virtual table
To implement virtual functions, the compiler creates a virtual table that allows the computer to quickly lookup which function to call at runtime
Table contains an entry for each virtual function with a function pointer that points to the most-derived function that is accessible to each class
Looking up which function to call makes runtime polymorphism slower than compile-time polymorphism
Pure virtual function
a virtual function that provides no definition in base class and all derived classes must override it
Declared like a virtual function but assigned with 0
virtual string GetHours() const = 0;
Abstract base class
class that has at least one pure virtual function
Cannot declare as an object
Classes
a class encapsulates data and behavior to create objects
Inheritance
allows one class (subclass) to be based on another class (a base class or superclass)
Abstract classes
a class that guides the design of subclasses but cannot itself be instantiated as an object
Pure virtual function
not implemented in base class, all derived classes must override the function
virtual type name() = 0;
Abstract class
cannot be instantiated as an object
Is the superclass for a sub class and specifies how subclasses must be implemented
When class has one or more pure virtual functions
Concrete class
not abstract, can be instantiated
Unified modeling language (UML)
a language for software design that uses different types of diagrams to visualize the structure and behavior of program
Structural diagram
visualizes static elements of software, such as attributes (variables) and operations (functions) used in the program
Behavioral diagram
visualizes dynamic behavior of software, such as the flow of an algorithm
A UML class diagram
a structural diagram that can be used to visually model the classes of a computer program, including member variables and functions
Project management tools
automate the compilation and linking process
Make
one project management tool that is commonly used on Unix and Linux computer systems
makefile
to recompile and link a program whenever changes are made to the source or header files
Makerules
used to specify dependencies between a target file (object files and executable) and a set of prerequisite files that are needed to generate the target file (source and header files)
make recipe
A make rule can include one or more commands - referred to as make recipe that will be executed in order to generate the target file
Enumeration type
enum - declared a name for a new type and possible values for that type