Object oriented programming (OOP)
It is based on the data and the functions that operate on it. Objects are instances
of Abstract Data Type that represent the data and its functions. Data that are
contained in an object is known as the object’s attributes. The procedures
(functions) that an object performs are called member functions. Essentially, an
object is, a self-contained unit consisting of attributes (data) and procedures
(functions).
class: like a struct (allows bundling of related variables), but variables and
functions in the class can have different properties than in a struct.
object: an instance of a class, in the same way that a variable can be an
instance of a struct
A Class specifies the attributes and member functions that a particular type of
object may have. It is like a blueprint and objects are like houses built from the
blueprint. So, a class provides a description of an object. Blueprint is NOT a house,
but a detailed description of a house. When we use the blueprint to build an
actual house, then we are building an instance of the house described by the
blueprint.
More on Objects
data hiding: restricting access to certain members of an object
public interface: members of an object that are available outside of
the object. This allows the object to provide access to some data and
functions without sharing its internal details and design, and provides
some protection from data corruption
Objects are created from a class
Syntax: class ClassName
{ declaration;
declaration;
};
1
Access Specifiers
Used to control access to members of the class
public: can be accessed by functions outside of the class
private: can ONLY be called by or accessed by functions that are members
of the class
Example: class name is written uppercase
class Rectangle
{
private:
double width; private members
double length;
public:
void setWidth(double); Mutator functions
void setLength(double);
public members double getWidth() const; Accessor functions
double getlength() const; const specifies that the
double getArea() const; function will not
change any data in the
calling object
} ; //end class declaration
Note: public and private members can be listed in any order. If not specified, the
default is private.
Accessors and Mutators
Mutator: a member function that stores a value in a private member
variable, or changes its value in some way.
Accessor: function that retrieves a value from a private member variable.
Accessors do not change an object's data, so they should be marked const.
2
Why Have Private Members?
Making data members private provides data protection- encapsulation
Data can be accessed ONLY through public functions
Public functions define the class’s public interface.
Code outside the class must use the class's public member functions to
interact with the object.
Separating Specification from Implementation
Place class declaration in a header file that serves as the class
specification file. Name the file ClassName.h, for example,
Rectangle.h
Place member function definitions in ClassName.cpp, for example,
Rectangle.cpp File should #include the class specification file
3
Programs that use the class must #include the class specification file,
and be compiled and linked with the member function definitions
Inline Member Functions
Inline appropriate for short function bodies:
int getWidth( ) const
{ return width; }
Regular functions – when called, compiler stores return address of call, allocates
memory for local variables, etc.
Code for an inline function is copied into program in place of call – larger
executable program, but no function call overhead, hence faster execution
Constructors
Member function that is automatically called when an object is created
Purpose is to construct an object by initializing private data variables
Constructor function name is class name
Has NO return type
Default Constructors
A default constructor is a constructor that takes NO arguments.
If you write a class with no constructor at all, C++ will write a default
constructor for you, one that does nothing.
A simple instantiation of a class (with no arguments) calls the default
constructor:
Rectangle r; //automatically default constructor called
If all of a constructor's parameters have default arguments, then it is a
default constructor. For example:
Rectangle(double = 0, double = 0);
4
Creating an object and passing no arguments will cause this constructor to
execute:
Rectangle r;
Classes with No Default Constructor
When all of a class's constructors require arguments, then the class has NO
default constructor.
When this is the case, you MUST pass the required arguments to the
constructor when creating an object.
Passing Arguments to Constructors
To create a constructor that takes arguments:
indicate parameters in prototype:
Rectangle(double, double);
Use parameters in the definition:
Rectangle::Rectangle(double w, double len)
{
width = w; private data members, width and length
length = len; are initialized to values through arguments
} w and len
You can pass arguments to the constructor when you create an object:
Rectangle r (10, 5); //Rectangle object r is created with width of 10
//and length of 5
5
Overloading Constructors
A class can have MORE than one constructor
Overloaded constructors in a class MUST have DIFFERENT parameter
lists:
Rectangle();
Rectangle(double);
Rectangle(double, double);
Destructors
Member function automatically called when an object is destroyed
Destructor name is ~classname, e.g., ~Rectangle
Has NO return type; takes NO arguments
Only ONE destructor per class, i.e., it cannot be overloaded
If constructor allocates dynamic memory, destructor should release it
Only One Default Constructor and One Destructor
Do NOT provide more than one default constructor for a class: one that takes no
arguments and one that has default arguments for all parameters
Square();
Square(int = 0); // will not compile
Since a destructor takes NO arguments, there can only be one destructor for a
class
Defining an Instance of a Class
• An object is an instance of a class
• Defined like structure variables:
Rectangle r;
6
• Access members using dot operator:
• r.setWidth(5.2); //setting the value of width to 5.2
• cout << r.getWidth(); //Accessing the current width
• Compile time error if attempt to access private member using dot
operator