Unit_6: OOCF Flashcards
OOCF, LSP
What is OCCF? what 4 things every C++ class should have?
- Orthodox Canonical Class Form: having standard to ensure low level operations work the same for all objects
*a null constructor
* a destructor: perform tasks associated with the death of an object (JAVA manage it, C++ do manually ~ClassName())
* a copy constructor
* an assignment operator
Why reference parameters? how to safely use it
- Efficiency: Passing large objects or data structures (like arrays or vectors) by reference avoids the overhead of copying the entire object.
- Modification: Functions can change the original data
- Memory: Saves memory by not duplicating data.
Use const : void dostuff(const int& x) {….}
//with const, it is illegal to try to change x - USE IT!
What is Principle of substitution
(aka the Liskov Substitution Principle - LSP)
- Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
=» Subclasses should extend the behavior of the superclass without changing its fundamental behavior.
eg: B is subclass of A: Possible to substitute instances of class B for instances of class A (i.e. putting
instances of B in variables of type A) in any
situation with no observable effect.
A* aVar = new B.
A* aVar = new B; What is your undestanding?
- A* pointer aVar to an object of type A
- because B is a subclass of A, aVar can also point to objects of type B (since B is a more specific type, it is still compatible with A due to inheritance).
- new B creates new object type B on the heap (dynamic memory allocation) and returns a pointer to that object.
-> This allows for polymorphism. you might call methods that are defined in class A but are overridden by class B. At runtime, the method of class B will be called, even though the pointer is of type A*.
=»When you call aVar->show(), since show() is a virtual function and B overrides it, the B’s implementation is called, not A’s.
If we have a hierarchy Child extend Parent,
and Parent extends Grandparent, and each
of the three classes have a destructor, in
what order are the three destructors called?
n C++, destructors are called in reverse order of construction, so:
👉 Child → Parent → Grandparent so the most derived class cleans up first.
predict output :
void byConstReference(const int &x) {
cout «_space;“byConstReference recv: “ «_space;x «_space;endl;
}
int main() {
int x = 10;
byConstReference(x);
cout «_space;“After byConstReference: “ «_space;x «_space;endl;
}
byConstReference recv: 10
After byConstReference: 10
x is passed by constant reference to the function byConstReference, which means:
x is not copied The function can read x but cannot modify it
If we have an Item class, what is the header for a
copy constructor?
Item(const Item &source)
Name: Same as class (Item)
Parameter: A reference to a const Item object
Purpose: To create a new object as a copy of an existing one
Coding Practice
*Items have weights & prices.
*Book is a subclass of Item. Books additionally have
authors.
*Write the Book and Item classes, with copy
constructors and print methods.
See copyConstructor.cpp
class Item {
private:
int weight;
float price;
public: Item(int weight, float price){ this->weight = weight; this -> price = price; } Item(const Item &source){ weight = source.weight; price = source.price; cout << "Item copy constructor called" << endl; } virtual void print(){ //virtual to allow polymorphism cout << "Price: " << price << " Weight: " << weight << endl; } };
class Book : public Item {
private:
string author;
public: Book(int w, float p, string a): Item(w, p){ //make Item handle its data members this->author = a; } Book(const Book &source): Item(source){ author = source.author; cout << "Book copy constructor called" << endl; } void print(){ cout << "Author: " << author << endl; Item::print(); //make Item print its data members } void setAuthor(string author){ this-> author = author; } };
How is the assignment operator different
from the copy constructor?
- Copy Constructor
- Purpose: Used when a new object is created from an existing one.
- When called:
On declaration like Item a = b;
When passing by value - Signature: Item(const Item& other);
- Assignment Operator
- Purpose: Used when an already existing object is assigned a new value from another object.
- When called:
After both objects already exist: a = b; - Signature: Item& operator=(const Item& other);