Lecture 4 - Classes and Objects
Lecture 4 - Classes and Objects
Lecture 4
Classes and Objects
Dr C.F. Kwong
[email protected]
Department of Electrical and Electronic Engineering
Faculty of Science and Engineering
Room: PMB 325
Topics
Classes
Member Functions
Public vs. Private
File Protocol
Object Oriented Programming Terminology
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
Classes and Objects
A Class is like a blueprint and objects are like houses
built from the blueprint.
Introduction to Classes
Objects are created from a class
Format:
class ClassName
{
declaration;
declaration;
};
Defining an Object
An object is an instance of a class
Defined like structure variables:
Rectangle r;
int Rectangle::setWidth(double w)
{
width = w;
}
Topics
Classes
Member Functions
Public vs. Private
File Protocol
Access Control in Classes
The use of the const specifier restricts the ability to change the value of
variables. This concept is clearly valuable for building in robustness. With
classes in particular we have even more control.
The public: grants read and write access to all the member variables
and allows anyone to call the function magnitude(); in association with
an instance of the class.
Access Control in Classes
The problem is that, if we have carefully designed and created instances of
a class where there is a natural relationship between different elements of
the data and then allow someone to randomly change the data, we are
asking for trouble!
Now the class designer has made sure that the array, values, and
the number of elements can only be “allocated” together and no
one can change either on their own.
Access Control in Classes
The class designer has made sure that the class data, values and
number_of_elements are always synchronised by restricting access to
them
Users of the class are forced to access the class data through an interface
that the class designer chose to provide i.e. void allocate_array(int
n);
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
Why Have Private Members
Making data members private provides data
protection.
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.
Class Example
Class Example
More on Access Specifiers
Can be listed in any order in a class
Can appear multiple times in a class
If not specified, the default is private
Using const With Member Functions
const appearing after the parentheses in a member
function declaration specifies that the function will
not change any data in the calling object.
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
Programs that use the class must #include the class
specification file, and be compiled and linked with
the member function definitions.
Inline Member Functions
Member functions can be defined
– inline: in class declaration
– after the class declaration
Inline appropriate for short function bodies: