91.1 OOP

Other than stated, all the content are taken from C++ Tutorial by Sololearn.

Composition is used for objects that share a has-a relationship, as in "A Person has a Birthday".

In general, composition serves to keep each individual class relatively simple, straightforward, and focused on performing one task. It also enables each sub-object to be self-contained, allowing for reusability.

  • Friend Functions

    Typical use cases of friend functions are operations that are conducted between two different classes accessing private members of both.

  • This keyword:

    store the address of the current object You may be wondering why it's necessary to use the this keyword, when you have the option of directly specifying the variable. The this keyword has an important role in operator overloading, which will be covered in the following lesson.

  • Operator Overloading

    With overloaded operators, you can use any custom logic needed. However, it's not possible to alter the operators' precedence, grouping, or number of operands.

Inheritance and Polymorphism

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal, hence dog IS-A animal as well.

inheritance A derived class inherits all base class methods with the following exceptions:

  • Constructors, destructors
  • Overloaded operators
  • The friend functions

A class can be derived from multiple classes by specifying the base classes in a comma-separated list. For example: class Daughter: public Mother, public Father

Protected member: A protected member variable or function is very similar to a private member, with one difference - it can be accessed in the derived classes.

Type of Inheritance

Public Inheritance: public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

Protected Inheritance: public and protected members of the base class become protected members of the derived class.

Private Inheritance: public and protected members of a private base class become private members of the derived class.

Polymorphism

C++ polymorphism means that a call to a member function will cause a different implementation to be executed depending on the type of object that invokes the function.

Simply, polymorphism means that a single function can have a number of different implementations

Virtual Functions

A virtual function is a base class function that is declared using the keyword virtual. Its behavior can be overridden withing an inheriting class by a function with same signature

Abstract Classes

  • Pure virtual function

A pure virtual function basically defines, that the derived classes will have that function defined on their own. Every derived class inheriting from a class with a pure virtual function must override that function.

  • Abstract Classes:

You cannot create objects of the base class with a pure virtual function. These classes are called abstract.

results matching ""

    No results matching ""