01.C++ OVERVIEW: CLASSES & DATA ABSTRACTION
SYNTAX
class classIdentifier
{
private | protected:
dataType selfVariableA, selfVariableB, ...;
public:
constructor();
constructor(dataType variableA, dataType variableB);
classMemberList
};EXAMPLE
clockType // this is the class name
{
private:
selfHours: int // private variable
selfMinutes: int // private variable
selfSeconds: int // private variable
public:
//constructors
clockType() // constructor w/o parameters - initializes the variables of the class
//function headers/prototypes
//setter functions uses pass by value
void setTime(int hour, int minute, int second); // mutator function (aka setter in Python)
//getter functions uses pass by reference
const void getTime(int& hour, int& minute, int& second); // accessor function (aka getter in Python)
const void printTime(); // keyword "const" doesn't change the value
int incrementSeconds();
int incrementMinutes();
int incrementHours();
const bool equalTime(const clockType&); // used to compare two class objects
};DEFINITIONS
CLASS
a reserved keyword used to define a collection of fixed number of components
a class defines a data type
class members are the components of a class
classes are used for complex behavior or encapsulation and private members are accessed through getter/setter functions.
CLASS MEMBERS
variables
variables inside a class can't be initialized during declaration
functions
functions are declared as prototype only (heading only) and doesn't have definitions
can directly access any data or function member of the class
constructors/destructors
CLASS MEMBER: ACCESS SPECIFIER

private
by default, all members of a class are private even when not specified
if a member of a class is private, it can't be accessed outside of the class
c++ doesn't have fixed order; however, private members are typically declared first followed by the public members
public
use the label public w/ a colon "public" to make class members publicly accessible
protected
CLASS VARIABLE
an instantiated (created) class object
aka class object or class instance
declaration is similar to declaring any other variables
ACCESS OPERATOR
once an object is declared, it can access the public members of the class using the access operator (.)
OPERATIONS
most of C++'s built-in operations don't apply to classes
arithmetic operators can't be used on class objects unless the operators are overloaded/redefined (1st code block)
relational operators used to compare two class objects for equality aren't allowed (2nd code block)
the two built-in operations that are valid for class objects are member access (.) and assignments (=) (3rd code block)
REFERENCE VARIABLES & PARAMETERS
pass by reference
an efficient way to pass a variable as parameter
the actual parameter changes when the formal parameter changes as well (1st code block)
the "const" keyword in the formal parameter can be used to prevent the function from changing its value (2nd code block)
SPECIAL CLASS FUNCTIONS
accessor functions
think of this as getters from Python
these are member functions that only accesses (doesn't modify) the value(s) of the data member(s)
mutator functions
think of this as setters from Python
these are member functions that modifies the value(s) of the data member(s)
constant functions
these are member functions that can't modify data members
the keyword "const" makes the function unchangeable
constructors
these are class members used to guarantee that data members are initialized
classes might have many constructors all having the same name as their classes
if a class has more than one constructor - they must have different sets of parameters
either in the number of parameters or types
constructors have no return type; so it's void
think of the C++ constructor as similar to Python constructor
CONSTRUCTOR TYPES
with parameters
without paramters
invoking constructors
CONSTRUCTOR RULES
a constructor is automatically executed when a class variable is declared
if a class has no constructor(s), then C++ automatically provides the default constructor; however, the object declared will be uninitialized
if a class includes constructor(s) with parameter(s) & doesn't include default constructor, then C++ doesn't provide default constructor
if a class has constructors & you declare an array of class objects, then the class should have the default constructor
DESTRUCTORS
these are member functions which is used to destroy the objects created by constructors
destructors are automatically executed when the class object goes out of scope
destructors have the same name as the class name but preceded by ~ tilde operator
clockType:~clockType();
destructors doesn't take any argument or return value
a class can only have one destructor
ABSTRACT DATA TYPES
a type (or class) for objects whose behavior is defined by a set of values & a set of operations
the definition of ADT only mentions what operations are to be performed, but not how these operations will be implemented

INFORMATION HIDING
this simply means hiding the operations on the data
interface (header) file
contains the specification details
implementation file
contains the implementation details
STATIC MEMBERS
when a member of a class is declared as static, it means no matter how many objects of the class are created, there is only one copy of the static member
used the "static" keyword
a static member is shared by all objects of the class
if no other initialization is present (outside the class), all static data is initialized to zero when the first object is created
static member function can only access static data member, other static member functions and any other functions from outside the class
CLASS IMPLEMENTATION: CLOCK

UML
The negative sign (-) signifies that the member variables are private
The positive sign (+) signifies that the member functions are public

SOURCE CODE
HEADER FILE: clock.h
HEADER FILE: implementation.h
SOURCE FILE: implementation.cpp
Last updated