Tuesday
Agenda
- Imperative Programming vs Object Oriented Programming
- What is an Object?
- Syntax in C++
- Data Encapsulation
Our C++ programs so far consist of a sequence of statements that contain:
- variables -- bucket that contains data (it "knows stuff")
- functions -- sequence of instructions that accomplishes a task (it "does stuff")
Object Oriented Programming
Objects are "blobs" that contain a bit of data and a bit of code
- Since they contain data (attributes or member variables), they "know stuff".
- Since they contain code (methods), they can "do stuff".
A Class is the blueprint of an object. It describes the attributes and implementation of the behaviors.
An Object is an instance of a class which (usually) holds explicit values for each attribute.
C++ Class Syntax
Example of a point class.
Topics covered:
- Basic class syntax -- don't forget the semi-colon at the end
- private vs public attributes and methods
- private: implementation details not part of user accessible interface
- public: can be seen/called by user of object.
Having private attributes and methods enables the programmer to hide the implementation details from the user (abstraction) and helps to ensure that the object is used in a controlled way (encapsulation)
- Constructors and Destructors
- The constructor has the same name as the class and initializes the member attributes
- The destructor is mainly used to free the memory that was dynamically allocated over the lifetime of the object. More on this later.
- Accessing attributes and running methods from objects
Thursday
Agenda
Inheritance
We can create a hierarchy of types with base classes (or parent classes or super classes) that enforce a common interface and derived classes (or child classes or subclasses) that implement the interface and can contain additional features. In this course:
- Base classes only need a .h file, contain only public, pure virtual functions. This is essentially just an interface that must be implemented by all the derived classes.
- Derived classes have both a .h and a .cpp file and implement all the methods.
See examples with the Dog class and the Shape class.
Polymorphism
Polymorphism is the ability of a variable of type pointer to a base class to contain the memory address of an object of a derived class. This enables you to "forget" which precise type of object you are dealing with, and you can still call any of the methods declared in the base class. Virtual methods will still use the most recent definition of the method from the derived class.
However, calling a method that is unique to the derived class from a variable of type pointer to the base class will result in a compile error.
See examples using the Dog class and the Shape class.