11a Classes
11a Classes
Objects
Separating Interface from Implementation
Conclusions
Chapter 1
Classes
Objects
Separating Interface from Implementation
Conclusions
Table of contents
1 Classes
2 Objects
4 Conclusions
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Introduction
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Defining a Class
To define a class, the keyword class is used. The general form to declare a class
is :
classclassName
{
// data members
// member functions
};
When we create a class, all of its members are hidden by default and are not
accessible outside the class. They are said to be the private members of the class
and private members can only be accessed within the same class.
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Defining a Class
We can also manually declare the data members of a class as private
using the following statement :
private :
All the members following this label will be private.
For the members of a class to be accessible outside the class, we must
declare them to be public members of the class by using the following
statement :
public :
All class members that follow this statement in the class definition will be
public up to the point where another specifier appears.
The keywordsprivateandpublicare access specifiers. An access specifier is
always followed by a colon.
All the class members that follow an access specifier in the class definition
will obey the specifier until another access specifier is defined.
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Defining a Class
Generally, we declare data members of a classprivateand member
functions aspublic.
classclassName {
private :
// data members
public :
// member functions
};
This is equivalent to
classclassName {
// data members
public :
// member functions
};
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Defining a Class
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Classes often provide public member functions to allow clients of the class to
set (i.e., assign values to) or get (i.e., obtain the values of) private data
members. These member function names need not begin with set or get, but
this naming convention is common.
Set functions are sometimes calledmutators(because they mutate, or
change, values), and get functions are also calledaccessors(because they
access values).
The set and get functions allow a client to interact with an object, but the
object’s private data remains safely encapsulated (i.e., hidden) in the object
itself.
The set and get functions of a class also should be used by other member
functions within the class to manipulate the class’s private data, although
these member functions can access the private data directly.
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
The plus sign (+) in front of the operation name indicates a public
operation in the UML (i.e., a public member function in C++).
The UML models a parameter by listing the parameter name, followed by a
colon and the parameter type in the parentheses following the operation
name. The UML has its own data types similar to those of C++.
The UML indicates the return type of an operation by placing a colon and
the return type after the parentheses following the operation name.
For operations that do not return values (i.e., they return void in C++), the
UML class diagram does not specify a return type after the parentheses of
these operations.
The UML is language independent so its terminology does not exactly
match that of C++.
Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions
Student
- age :Integer
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Constructors
className(argument −list){
// initialization of data members
}
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Defining Objects
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
We can not access class members without the object of that class. To
access member of a class object, member access operator is used. The
general form to access a particular member is : objectName.member;
Heremembercan be a data member as well as a member function. Member
functions defined inside the class definition are inline functions by default.
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples
Chapter 1
Classes Introduction
Objects Member Functions Outside the Class
Separating Interface from Implementation Example
Conclusions
Interfaces define and standardize the ways in which people and systems
interact with one another. For example, an ATM’s controls serve as an
interface between the ATM’s users and its internal components.
The controls allow users to perform a limited set of operations (such as
money withdraw, check balance, and money transfer). Various ATMs may
implement these operations differently. Some provide push buttons, some
provide touch screen and some support voice commands.
The interface specifies what operations a ATM permits users to perform but
does not specify how the operations are implemented inside the ATM.
Similarly, the interface of a class describes what services a class’s clients can
use and how to request those services, but not how the class carries out the
services.
Separating the interface from the implementation details is called
abstraction.
Chapter 1
Classes Introduction
Objects Member Functions Outside the Class
Separating Interface from Implementation Example
Conclusions
Chapter 1
Classes Introduction
Objects Member Functions Outside the Class
Separating Interface from Implementation Example
Conclusions
The member function defined outside the class definition still has a class
scope i.e., its name is known only to the other members of the class unless
referred to via an object of the class.
Note that if a member function is defined in a class definition, the
member function is automatically inlined. Member functions defined
outside a class may be made inline explicitly using the keyword inline.
Just like member function, the constructor definition can also be placed
outside the class definition. The constructor is declared in class definition and
defined after its corresponding class definition.
To define a constructor outside the class, the general form is :
Chapter 1
Classes Introduction
Objects Member Functions Outside the Class
Separating Interface from Implementation Example
Conclusions
Chapter 1
Classes
Objects Exercise
Separating Interface from Implementation Conclusions
Conclusions
Exercise
Create a class called Box that uses three variables of type double to calculate
the volume of a box. Initialize an object of type Box to specific dimensions,
then calculate the volume it represents, and print out the result
Create a class Rectangle with attributes length and width. Provide member
functions that calculate the perimeter and the area of the rectangle. Also,
provide set and get functions for the length and width attributes. The set
functions should verify that length and width are each double numbers larger
than 0.0 and less than 20.0.
Model the class diagrams for the above two exercises.
Chapter 1
Classes
Objects Exercise
Separating Interface from Implementation Conclusions
Conclusions
Conclusions
Chapter 1