COMP 206 Unit 13: Inheritance and Composition

Description

University-level C++ COMP 206 Flashcards on COMP 206 Unit 13: Inheritance and Composition, created by Adriana Vincelli-Joma on 18/10/2021.
Adriana Vincelli-Joma
Flashcards by Adriana Vincelli-Joma, updated more than 1 year ago
Adriana Vincelli-Joma
Created by Adriana Vincelli-Joma over 2 years ago
3
0

Resource summary

Question Answer
syntax to create class using composition class X {}; class Y { public: X x; }; int main() { Y y; y.f(47); y.x.set(37); }
syntax to create class using inheritance class X {}; class Y : public X {}; int main() { Y D; D.change(); // X interface comes through }
constructor initializer list calls constructor for sub-object when object is created
syntax for constructor initializer list - call to sub-object constructors after constructor argument list and colon, but before opening brace of function body - MyType::MyType (int i) : Bar(i) {
syntax for member object initializer list - if you have more than one constructor call in initializer list, separate calls with commas - MyType2::MyType2 (int i) : Bar(i), m(i + 1) {
syntax for initializing built-in type in initializer list - treat built-in types as if they have single constructor which takes single argument class X { int i; float f; char c; X() : i(7), f(1.4), c('x') {} };
unusual feature in calling constructors/destructors automatically - every constructor/destructor in hierarchy is called - with normal member function only that function is called, but not any of base-class versions
name hiding explicit declaration of name of class in derived class or nested declarative region
virtual function member function in base class that you redefine in derived class
redefining provide exact signature and return type in derived class definition as in base class definition for ordinary member functions
overriding - same function in child class which is already present in parent class - base class member function is virtual function
polymorphism call to member function will cause different function to be executed depending on type of object that invokes function
when function is not automatically inherited from base class - constructor/destructor don't inherit and must be created specifically for each derived class - operator= doesn't inherit because it performs constructor-like activity
3 ways static member functions act same as non-static member functions 1. inherit into derived class 2. if you redefine static member, all other overloaded functions in base class are hidden 3. if you change signature of function in base class, all base class versions with that function name are hidden
composition vs inheritance composition - want features of existing class inside new class, but not interface - has-a relationship inheritance - include all methods and attributes from parent class and expose to child class - is-a relationship
subtyping - making new type from existing type - new type has exactly name interface as existing type
private inheritance - makes public and protected members of base class private in derived class - create new class that has all data and functionality of base class, but functionality is hidden
publicizing privately inherited members - inherit privately = all public member of base class become private - to make visible, say names along with using keyword in public section of derived class
protected private as far as class user is concerned but available to anyone who inherits from class
protected inheritance when deriving from protected base class, public and protected members of base class become protected members of derived class
multiple inheritance add more classes, in base-class list during inheritance, separated by commas
subobject copy of all data members of that base class
cautions against using multiple inheritance - duplicate subobjects take up extra space and introduce ambiguity - ambiguous upcasting: clash between function definitions and subobjects
incremental development method that develops system in manner where various portions of system are developed at different times/speed of development
composition and inheritance support incremental development by inheriting from/composing with an existing functional class and adding data members and member functions, existing code is untouched and unbugged
upcasting converting derived-class reference/pointer to base class
using upcasting with copy-constructor - synthesize copy-constructor for derived class - properly call base-class copy-constructor whenever you write own copy-constructor - base-class copy-constructor calls upcasts a reference to child to a reference to parent and uses it to perform copy-construction
upcasting pointers and references - upcasting occurs during function call - child object outside function has reference taken and becomes parent reference inside function - Wind w; - Instrument* ip = &w; - Instrument& ir = w;
Show full summary Hide full summary

Similar

COMP 206 Unit 11: Operator Overloading
Adriana Vincelli-Joma
COMP 206 Unit 9: Name Control
Adriana Vincelli-Joma
COMP 206 Unit 6: Initialization and Cleanup
Adriana Vincelli-Joma
COMP 206 Unit 5: Hiding the Implementation
Adriana Vincelli-Joma
COMP 206 Unit 12: Dynamic Object Creation
Adriana Vincelli-Joma
COMP 206 Unit 7: Function Overloading and Default Arguments
Adriana Vincelli-Joma
COMP 206 Unit 8: Constants
Adriana Vincelli-Joma
COMP 206 Unit 3: The C in C++
Adriana Vincelli-Joma
GOVN 377 Unit 8: The Brave New World of Data Management and Manipulation
Adriana Vincelli-Joma
HIST 404: Unit 4 Science in the Renaissance
Adriana Vincelli-Joma
HIST 404: Unit 9 Discovering DNA
Adriana Vincelli-Joma