02.OBJECT ORIENTED DESIGN & C++

SYNTAX

class className: memberAccessSpecifier baseClassName
{
  memberList
};
  • the memberAccessSpecifier is either public, protected, or private (default)

    • private members of the base can't be directly accessed by members of the derived class

    • public members of a base class can be inherited either as public or private members by the derived class

      • the derived class can include data/functions

DEFINITIONS

INHERITANCE

  • an object oriented design concept which relates many classes together to allow specific classes using members of general classes

  • a kind of hierarchy relation between different classes of different levels

OVERRIDING

  • occurs when public functions of the base class are redefined in a derived class (subclass)

  • the redefined function in the subclass must have the same name, number, and types of parameters (formal parameter list)

OVERLOADING

  • occurs when both functions in the base class & in the derived class have the same name, but different formal parameter list (overloading is allowed in C++)

COMPOSITION

  • another way to relate two or more classes - similar to inheritance

  • used for objects that have a "has-a" relationship to each other

  • C++ allows object composition by using classes as member variables in other classes

POLYMORPHISM

  • this is an OOD principle

  • can be discussed via overloading or via templates

  • allows the programmer defining operators/functions w/ same name, but different formal parameter list

    • see slide 14 of chapter 2

TEMPLATES

  • enables the programmer to write generic codes for related functions & classes

  • enable the user writing a single code segment for a set of related functions (function template) or related classes (class template)

INHERITANCE IMPLEMENTATION: CIRCLE

output of a C++ program that uses inheritance

UML

SOURCE CODE

HEADER FILE: circle.h

HEADER FILE: circleImplementation.h

SOURCE FILE: inheritance.cpp

COMPOSITION IMPLEMENTATION: PERSON

output of a C++ program that uses classes & composition

SOURCE CODE

HEADER FILE: date.h

HEADER FILE: person.h

HEADER FILE: personInfo.h

SOURCE FILE: implementation.cpp

TEMPLATE IMPLEMENTATION

output of a C++ program that uses templates & polymorphism

SOURCE CODE

HEADER FILE: template.h

SOURCE FILE: implementation.cpp

Last updated