Basic principles of OOD

Encapsulation

Ability to combine data and operations

Inheritance

is-a relationship
example - every employee is a person
Allows new class creation from existing classes
Derived class created on the base of an existing class
inherits base class' properties
reduces software complexity
becomes base class for future derived class

Inheritance types:

Inheritance can be viewed as tree-like of hierarchical
Derived class general syntax
no {cpp}memberAccessSpecifier specified, assume {cpp}private inheritance
rM drawing 2025-10-21-22.07.21.png|300

Inheriting class members

{cpp}private base class members are available only to the base class
{cpp}public members are inherited, and can be {cpp}private or {cpp}public to the derived class

Derived class can include additional members or redefine public member base class functions

Function overloading & overrinding

Inherited class variables and functions have same name, type and parameters.
functions can be overloaded, changing the set of parameters

Function overloading is changing function's parameters, whereas function overriding is changing the function code.

class Clock {
protected:
	int hours;
	int minutes;
public:
	void increaseTime();
	void increaseTime(int h, int m);
}
class Clock {
protected:
	int hours;
	int minutes;
public:
	void increaseTime() virtual {hours++; minutes++;}
}

class accurateClock : public Clock {
protected:
	int seconds;
public:
	void increaseTime() override {hours++; minutes++; seconds++;}
	void increaseTime(int h, int m, int s); // function overloading
}
Constructors

Derived class with own private member variables must include its own constructor
Derived class constructors can only directly initialize inherited members (public data)

Header files

Required to define new classes
Base class should be defined in the header file
New class header files contain preprocessor commands for computer where to look for base classes' definitions
{cpp}include command
preprocessor processes the program before it is compiled
avoid multiple inclusions of a file - use proproc. commands

#ifndef H_TEST
#define H_TEST
const int ONE = 1;
const int TWO = 2;
#endif
Protected members of class

{cpp}private class members
private to the class
cannot be directly accessed outside the class
derived class cannot access {cpp}private members

Solution - make members {cpp}public
problem - anyone can access that member
Solution - declare member as protected
derived classes can access the member
prevents direct access outside the class

Inheritance keywords
{cpp}public MemberAccessSpecifier
{cpp}protected MemberAccessSpecifier
{cpp}private MemberAccessSpecifier
Composition

Another way to relate two classes
One or more class members
another class type object
has-a relationship
example - every person has a date of birth

Polymorphism

OOD third principle
Occurs through operator overloading and templates

Operator overloading

Why is it needed?
Programmer can extend most operation definitions to make them work 'in logical way' on the objects.
Rational, arithmetic, insertion for data output, extraction for data input operators applied to classes

Examples - stream insertion operator <<, stream extraction >>, +, -
C++ does not allow user to create now operators