Object-Oriented Programming (Encapsulation)

Objects

  • Represents anything in the real world
  • Has a Unique Identity
  • Differentiable from one and other
  • Has State (properties or attributes)
  • Characterizations (color, size, etc…)
  • Represented by data fields or variables in C++
  • Has Behavior (actions)
  • What it does
  • Represented by methods in C++

Objects are like people

  • We all have unique identities, attributes, and complex behaviors. “If I’m fortunate enough to have a personal assistant, I can ask them to do complex things without worrying about how those things get done.” – Steve Jobs
  • Schedule my 6th month checkup with the dentist
  • Do my laundry

  • The interface is the same, I use personal communication, email, texts, all to interact with a person to perform specific tasks.

Class

  • A “class” defines a new type in the language
  • One class can create many objects
  • An object is an instance of a class
  • These two terms are often used interchangeably
  • Like a blueprint or a cookie cutter
  • Defines all the methods and state variables for each instance.
  • These methods and variables make up the interface for an object.

Principle 1: Encapsulation

  • Instead of manipulating data, one manipulates objects that have defined interfaces.
  • Implementation details are not important.
  • We don’t care about how things work.
  • We only care about what things do.
  • “black box” design
  • Also known as “data hiding”
  • Analogy: I don’t care how my car’s engine works, I just want it to power my car to get me places!
  • This principle allows for much more flexible and extendable designs with complex software.

Interface Access Control

  • Encapsulation is implemented by adding access control to each method or variable in a class
  • “public” access control means everyone has unrestricted access.
  • “private” access control is used for items that you only want accessible by other members of this class.
  • “protected” access control is used for items that you want accessible by this class and any derived class.

Object Anatomy

  • Inside a class declaration:
  • Constructors
  • Instance Methods
  • Static Methods (class methods, often “utility” functions)

  • Instance Variables

  • Constants
  • Static Variables (class variables)

Object-Oriented Programming (Inheritance)

Object Relationships

  • Review: Encapsulation is a technique that allows objects to exhibit a set of traits and behaviors without unnecessary clutter.

  • Some objects have relationships with other objects.

  • If there are common traits or behaviors, we’d like to exploit that when possible…

Common traits

  • When objects share several attributes or behaviors we might choose to categorize their similarities and differences.
  • These similarities might include both attributes and behaviors.

Circle Rectangle
color color
is filled is filled
radius length, width
Area ($$$\pi r^2$$$) Area ($$$length*width$$$)
Perimeter ($$$2\pi r$$$) Perimeter ($$$2*(length+width)$$$)

Relationships

  • Several common traits may suggest that objects are related.
  • Relationships imply that an object belongs to a more general category of objects.

Is-A Relationship

  • Objects with related traits get those traits from a general object or a shared ancestor.
    A circle is a shape. A rectangle is a shape. A student is a person. A car is a vehicle.

  • This relationship helps us define object hierarchies

Object Hierarchy

  • Principle 1: Encapsulation
  • Instead of manipulating data, one manipulates objects that have defined interfaces.
  • Implementation details are not important.
  • We don’t care about how things work.
  • We only care about what things do.
  • “black box” design
  • Also known as “data hiding”
  • Analogy: I don’t care how my car’s engine works, I just want it to power my car to get me places!
  • This principle allows for much more flexible and extendable designs with complex software.

Encapsulation Example

class Shape
{
public:
  Shape() {  };
  virtual double getArea() = 0; 
  virtual double getPerimeter() = 0;
  Point getCentroid() {  };
  

protected: 
  std::string _color;
  bool _filled;
  double _x, _y _z;
};

Principle 2: Inheritance

  • To avoid code duplication, common traits are factored out into a common or general superclass.
  • Subclasses may then inherit these attributes or behaviors and extend the superclass object to do more specialized things.
  • Our encapsulation principle is preserved through the use of private and protected visibility modifiers.

Inheritance Example

class Circle : public Shape
{
public:
  Circle() : Shape() {  };
  double getArea() {  }
  double getPerimeter() {  }

protected: 
  double radius;

  /* This class now contains *everything* from
   * it’s super class and this class.
   */
  
};

Instances of Shapes

Construction

  • Each subclass object must construct it’s parent first then itself. This is accomplished through the use of the parent’s named constructor either explicitly or implicitly.
Circle::Circle() :
  Shape(),
  // initialization list items  
{
}

Overriding

  • The designer of any subclass may choose to provide an alternative method of a superclass method. This is called overriding.
  • Method signatures must match exactly.
  • Note: Do not confuse the term overloading with overriding.
  • Overloading means that you are creating multiple methods with same name but different signature.
  • Overriding means that you are providing the exact same name and signature in related classes.

Object-Oriented Programming (Polymorphism)

Principle 1: Encapsulation

  • Instead of manipulating data, one manipulates objects that have defined interfaces.
  • Implementation details are not important.
  • We don’t care about how things work.
  • We only care about what things do.
  • “black box” design
  • Also known as “data hiding”
  • Analogy: I don’t care how my car’s engine works, I just want it to power my car to get me places!
  • This principle allows for much more flexible and extendable designs with complex software.

Principle 2: Inheritance

  • To avoid code duplication, common traits are factored out into a common or general superclass.
  • Subclasses may then inherit these attributes or behaviors and extend the superclass object to do more specialized things.
  • Our encapsulation principle is preserved through the use of private and protected visibility modifiers.

Principle 3: Polymorphism

  • It’s purpose is to enable generic programming.
  • While Encapsulation and Inheritance allow us to create “black box” objects, we need polymorphism to complete the object-oriented trilogy enabling programmers to create algorithms that can work with any related type.
  • This property comes from a superclass’s ability to behave as any subclass type!
  • e.g. I want to write an method that prints the area of any shape without the knowledge of what shape I’m dealing with.

Polymorphism Example

Circle circle();
Rectangle rectangle();
printArea(circle);
printArea(rectangle);

// Prints the area of *any* shape!
void printArea(const Shape & shape)
{
    std::cout << shape.getArea();
}

Which version is called?

  • The most specialized version of a virtual method will be called regardless of what kind of reference we hold to an object.
Circle circle();
Shape & shape = circle; // implicit upcast

Circle.getCentroid(); // Calls circle’s method
Shape.getCentroid();  // Calls shape’s method

circle.getArea();     // Calls circle’s method
shape.getArea();      // Calls circle’s method 

Upcasting and Downcasting

Circle circle();

// upcast
Shape & s_ref = circle;

// downcast (explicit)
Circle & c_ref = 
  static_cast<Circle &>(s_ref); 

Abstract Classes

  • Sometimes we want to declare an API without defining all of the methods in the base class.
  • If we declare any method “abstract” by using “=0;” we effectively create an abstract method without providing an implementation!
  • Implications:
  • Abstract classes cannot be instantiated.
  • Derived classes must override any and all abstract methods
  • Goal:
  • Declare “general” behavior of a object without defining specifics
  • Enable Polymorphism