0% found this document useful (0 votes)
24 views

Lecture 4 - Classes and Objects

The document discusses classes and objects in C++. It covers defining classes, creating objects, member functions, access control using public and private, and protocols for splitting code across multiple files. The key topics are defining classes and objects, using member functions, controlling access with public and private, and guidelines for file organization.

Uploaded by

HuayiLI1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 4 - Classes and Objects

The document discusses classes and objects in C++. It covers defining classes, creating objects, member functions, access control using public and private, and protocols for splitting code across multiple files. The key topics are defining classes and objects, using member functions, controlling access with public and private, and guidelines for file organization.

Uploaded by

HuayiLI1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Computer Aided Engineering

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;

Access members using dot operator:


r.setWidth(5.2);
cout << r.getWidth();
Classes Example
Classes Example
Topics
Classes
Member Functions
Public vs. Private
File Protocol
Class Member Functions
Consider an object to represent complex numbers.
Class Member Functions
We will also define a function that evaluates the
magnitude of complex_numbers.
Class Member Functions
This is a bad design because:
The function

float magnitude(complex_number &c);

Depends too heavily on how the class


complex_number is defined and so should be closely
tied into the class itself.

We can do this by introducing class member


functions.
Class Member Functions
We now make the “magnitude” function part of the
actual class itself
float magnitude(); is now a function that can only be
called in association with a particular instance of
complex_number in the following manner:

z.magnitude(); means call the function


magnitude() of the class.
Class Member Functions
The key point here is that the person who designed
the class complex_number is also providing its
function magnitude().
Therefore, it is much more likely that the function
will work reliably and also:
It is not possible for someone to change the details
of the complex_number class without considering
its impact on the function magnitude().
Overall this approach is far more robust and less
prone to human error.
ROBUST DESIGN!!
Class Member Functions
To define the member function (i.e. actually provide
the code to do something)
Summary: Defining a Member Function
When defining a member function:
– Put prototype in class declaration
– Define function using class name and scope resolution
operator “ :: ”

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.

So far our classes are of the form,

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!

The class designer is assuming (!) that


number_of_elements is properly set
by a user of the class to be the size of
the array, values.
Access Control in Classes
To avoid these problems we carefully define certain member
variables and functions of classes to be private i.e.
inaccessible.

It is not permitted (enforced by the compiler) to even read, let


alone change aprivate member variable or to call a private
member function.
Access Control in Classes
This seems stupid as now the private member variables can
never be used?
Actually, the one place from which private members can be
accessed is from within other member functions.

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:

int getWidth() const


{ return width; }
Rectangle Class with Inline Member Functions
Tradeoffs – Inline vs. Regular Member
Functions
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
Topics
Classes
Member Functions
Public vs. Private
File Protocol
File Protocol
Large scale projects will not be written in just one file, rather
split over many.
This raises a number of issues when compiling and
distributing different sections of code.
Here we will just make some brief observations on good
practice.
Example:
We are responsible for designing a class called motorbike in a
large project.
Other people will be using motorbikes but we will actually
code its functions.
What do the other people actually need from us to develop
their parts of the code?
They only need to see the declarations of the class and its
member functions - not the actual implementation code.
File Protocol
File Protocol
It is not that we are being secretive, rather that as we know we
ought to clearly define interfaces between different sections of
the project clearly and early on, we ourselves will produce this
first - well before getting into the detailed implementation.
That way they can work on their part knowing that however we
do our bit, the interface has been fixed. In particular, they can
actual compile, in order to check that their own part of the code
is correct.
What they can’t do is to link the code - actually produce an
executable program without our implementation details.
File Protocol - Summary
Complicated codes will be broken into many parts.
The interfaces will often be defined and distributed
in header files as this is all users actually need to
compile with.
The implementations will be distributed in separate
files, or as precompiled binaries.
This approach is very consistent with the philosophy
of code reuse and consequently quite simple classes
may have a file to themselves, so that they can be
cleanly extracted for reuse in another project.
Questions?

You might also like