Object Oriented Programming Flashcards
Derived Classes/Child Classes
An EXTENSION of a base class Classes that inherit functions and variables from a base class
aka “child” class
Base class/Parent Class
Class that can be EXTENDED by a derived class.
Includes functions and variables that can be inherited by a derived class
Inheritance
Mechanism for EXTENDING a class
-If you extend the class rather than editing it, you will not have to “re-debug” the original code (because it was not changed since the last time you debugged)
-It allows you to reuse existing DEBUGGED code by allowing a class to acquire properties (data and operations) of another class
Multiple Inheritance
Inheritance of properties from multiple classes
-Generally not recommended, but some languages (like C++) will allow it
“Public”
Indicates that the data/operation can interface with the rest of the world
can be accessed from outside of the class
“Private”
Indicates that the data/operation can only interface with member functions of the class in which it was written
“Protected”
Indicates that the data/operation can ONLY interface with the data/operations of a derived class
DO NOT USE THIS UNLESS YOU KNOW THE CLASS IS GOING TO BE INHERITED (otherwise you can get yourself in trouble when it comes to debugging)
Practical applications of inheritance
- Principle of Least Privilege & Role-based access control
The most general base class should have the fewest permissions. As the roles become more specific, they are given more and more privileges.
2.
Reading a class block in a class diagram
Minus (-) inidicates private.
Plus (+) indicates public.
Pound sign (#) indicates “protected”.
The name of the class goes in the top box.
The data/variables of the class go in the middle box.
The operations/functions of the class go in the bottom box.
Inheriting “private” variables/function
Private members of the Base Class are present within objects of the Derived Class but are NOT directly accessible within the Derived Class code
Constructors
Constructors are not inherited.
Representing a derived class in a UML class diagram
Only show what is EXCLUSIVE to the derived class.
NEW variables
NEW functions
Anything that is overwritten
Memory handling of inheritance
When you create a child class, you are also “creating” a parent class inside of that child class. There is a Base class object nested within each Derived class object. The base class object literally comes directly before the derived class in memory. The Base class object “contains” all inherited members
Accessing private members of the base class
If the derived class inherits PUBLIC functions from the base class…. then those functions can still access the private variables of the base class via that public function it just inherited
Constructor inheritance
No such thing. Derived classes do not inherit the constructor from the base class.
You will have to make a new constructor for the derived class.
Order in which the destructors are called
Destructors are called in the upward inheritance order (less abstract constructors come first) Derived class destructors will be called before the base class destructor.
Parameters
Parameters are needed in the function header. It is a DECLARATION of variables that you can expect to find in that function. They require datatypes just like any declaration
Order in which the constructors are called
Constructors are called in the downward inheritance order (more abstract constructors come first) Base class constructors are called before the derived class constructors.
Arguments
Arguments are needed when the function is CALLED. They represent actual variable names so that you can pass actual values to the function. They do not require datatypes
Calling a function from the based class inside a function of the same name within the derived class
This is completely legal. But it you must specify the location of the function you are calling (in C++ this is done using:
base_name::function_name
What is the purpose/role of the constructor?
lol
What is the purpose/role of the destructor?
lol
What is the sequence of steps the machine goes through when it executes a program that includes both a base class AND a derived class that extends that base class?
Purpose of the #ifndef guard?
You NEED it to keep the code from redundant imports. You do not want to import the same file more than once and if you use multiple sourcecode files, it is extremely likely that you have importanted something more than once because some files (like iostream string.h) are included in almost every C++ program
Static Binding
Mean that something is decided how it behaves when you WRITE the code.
Compile-time determination of which function to call for a particular object
Dynamic Binding
Means that something is decided how it behaves when you EXECUTE the code.
Run-time determination of which function to call for a particular object
Polymorphism
The ability to determine which of several operations with the same name is appropriate
Polymorphic Operation
An Operation that has multiple meanings depending upon the data type of the object to which it is bound at run time
Abstract Data Type
Data type whose properties (domain and operations) are specified independently of any implementation
Class
–A structured type used to model abstract data types
–Encapsulate attributes (data) with the member functions that modify
the attribute values
Object
–An instance of a class
–Set of attribute values define the state of an object at a given time
–Member functions and attributes accessed using the member
selection operator (period .)
Client
–Software that declares and manipulates objects of a particular class
type
public members of a class
Can be accessed from outside of the class
private members of a class
Accessible only by the code within the implementation file and is not accessible by
code outside of the class
- Client cannot interact directly with private
variables or functions within object
Specification file
*The declaration of the class type
*Include guards (more on this later)
Implementation File
*Code that implements the member functions of the class
Unit Testing
- Performed with a
dedicated driver program
–Contains a simplified main function that creates instances
of the class (objects) and then tests the objects by using its
public interface
–Multiple source files must be compiled and linked to create
the executable
–Once tested, the class may be reused with the actual
application program
struct v. class
–A struct is a class whose members are public by default
–By default, all members of a C++ class are private
–Two built-in operations for structs and classes “.” and “=”
Five categories of member functions
- Constructors
- Transformers
- Observers
- Iterators
- Destructors
Constructor functions in a C++ class
Create and Initialize Object
Constructor methods have the same name as the class
Transformers functions in a C++ class
Alter the state of an object
Observer functions in a C++ class
Allow one to view the state of an object
Iterator functions in a C++ class
Allow us to process (one at a time) all components of an ADT
“const” for member functions
Member functions applied to an object may alter attributes stored within that object unless the reserved word const is used to prevent modification
“self” for member functions
The object to which a member function
is applied