Unit_6: OOCF Flashcards

OOCF, LSP

1
Q

What is OCCF? what 4 things every C++ class should have?

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why reference parameters? how to safely use it

A
  1. Efficiency: Passing large objects or data structures (like arrays or vectors) by reference avoids the overhead of copying the entire object.
  2. Modification: Functions can change the original data
  3. 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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Principle of substitution
(aka the Liskov Substitution Principle - LSP)

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A* aVar = new B; What is your undestanding?

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

n C++, destructors are called in reverse order of construction, so:

👉 Child → Parent → Grandparent so the most derived class cleans up first.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

predict output :
void byConstReference(const int &x) {
cout &laquo_space;“byConstReference recv: “ &laquo_space;x &laquo_space;endl;
}
int main() {
int x = 10;
byConstReference(x);
cout &laquo_space;“After byConstReference: “ &laquo_space;x &laquo_space;endl;
}

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If we have an Item class, what is the header for a
copy constructor?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is the assignment operator different
from the copy constructor?

A
  1. 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);
  2. 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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly