0% found this document useful (0 votes)
47 views30 pages

11a Classes

The document discusses classes and objects in object-oriented programming. It defines what a class is - a user-defined data type that defines variables and functions as members. Objects are instances of a class. The document covers defining a class with access specifiers like private and public, modeling classes with UML class diagrams, and using get and set functions to access private data members.

Uploaded by

noonedin32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views30 pages

11a Classes

The document discusses classes and objects in object-oriented programming. It defines what a class is - a user-defined data type that defines variables and functions as members. Objects are instances of a class. The document covers defining a class with access specifiers like private and public, modeling classes with UML class diagrams, and using get and set functions to access private data members.

Uploaded by

noonedin32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Classes

Objects
Separating Interface from Implementation
Conclusions

Object Oriented Programming


(Classes-I)

The University of Lahore, Lahore.

Chapter 1
Classes
Objects
Separating Interface from Implementation
Conclusions

Table of contents

1 Classes

2 Objects

3 Separating Interface from Implementation

4 Conclusions

Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions

Introduction

Aclassis a user-defined data type.


The variables and functions declared within a class are called
members of the class.
The variables are calleddata membersand the functions are
calledmember functions. The functions are sometimes
referred to asmethods.
Once a class is defined, we can declare variables of the class
type. These variables are also calledobjectsof the class.
A class definition ends with a semicolon.

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

A class normally consists of one or more member functions that


manipulate the data members of the class.
Data members are declared inside a class definition but outside the bodies of
the class’s member function definitions. Data member declared private are
accessible only to member functions of the class.
Declaring data members with access specifier private is known as data
hiding. When a program creates an object, data members are
encapsulated(hidden) in the object and can be accessed only by member
functions of the object’s class.
A class’s data members cannot be initialized where they’re declared in the
class body (With the exception of staticconstint data members).
Variables declared in a function definition’s body are known as local
variables. A local variable cannot be accessed outside the function in
which it’s declared.

Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions

The set and get Functions

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

UML Class Diagram

UML is a standardized graphical language used by software developers to


represent their object-oriented systems.
In the UML, each class is modelled as a rectangle with three compartments.
1 The top compartment contains the class’s name centred
horizontally and in boldface type.
2 The middle compartment contains the class’s attributes, which
correspond to data members in C++.
3 The bottom compartment contains the class’s operations,
which correspond to member functions in C++.
The UML represents data members as attributes by listing the attribute
name, followed by a colon and the attribute type.
For a private data member,the class diagram lists a minus sign (-) in front of
the corresponding attribute’s name. The minus sign in the UML is equivalent
to the private access specifier in C++.

Chapter 1
Classes
Introduction
Objects
Defining a Class
Separating Interface from Implementation
Modelling a Class
Conclusions

UML Class Diagram

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

UML Class Diagram

Student
- age :Integer

« constructor » + Student(a ∶ Integer)


+ setAge(a ∶Integer)
+ getAge() ∶ Integer
+ displayAge()

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Constructors

A class constructor is a special kind of function in a class.


A constructor is called when an object of the class is created.
It initializes the object and ensures that data members contain valid values.
A class constructor always has the same name as the class in which it is defined.
A constructor does not return any value and therefore has no return type.
Even void is not allowed to specify its return type.
The general form of a constructor is :

className(argument −list){
// initialization of data members
}

Here argument −list initializes the data members of a class.

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

The Default Constructor

Every class has at least one constructor because an object of class is


always created using a constructor.
If we do not define a constructor for our class then the complier will
supply a default constructor that will be used to create objects of our
class. The default constructor has no parameter.
When we declare a class, the compiler supplies a default constructor
automatically. This automatic default constructor exists as long as we do not
define a constructor for the class our self.
The default constructor generated by complier does nothing. It does not
initialize the data members of the object that is created.
A programmer can define a default constructor of his class as well. Note
that a default constructor can be called with out specifying an
argument −list. It does not even need parentheses.

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Defining Objects

To define a class object, the general form is :


classNameobjectName (argument −list);
When an object of a class is created, the class constructor is
automatically called.
The values specified in argument −list are passed as arguments to the
constructor and are used to initialize the data members of the object.
The number of values specified in the argument-list must be the same as in
constructor’s argument −list.
Each object of a class maintains its own copy of data members in memory.
Classesencapsulate(i.e., wrap) attributes and member functions into
objects.

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Accessing Class Members

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.

Like operations, the UML models constructors in the third compartment of a


class in a class diagram. To distinguish a constructor from a class’s operations,
the UML places the word “constructor” between guillemets (« and ») before
the constructor’s name. By convention, you list the class’s constructor before
other operations in the third compartment.

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 1 : Accessing public Members

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 2 : Accessing public Members

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 3 : Accessing private Members

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 4 : set and get Functions

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 5 : Default Constructor

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 6 : Initialization using Constructor

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 7 : Initialization using Constructor

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 8 : Illegal use of Constructor

Chapter 1
Classes Constructors
Objects Defining Objects
Separating Interface from Implementation Accessing Class Members
Conclusions Examples

Example 9 : Multiple Objects

Chapter 1
Classes Introduction
Objects Member Functions Outside the Class
Separating Interface from Implementation Example
Conclusions

Separating Interface from Implementation

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

Member Functions Outside the Class

Member functions can be defined inside a class, but it is good practice to


define the functions outside the class definition.
Declaring member functions inside a class definition and then defining
these member functions outside the class definition separates the
interface of a class from its implementation.
Once a class is defined and its member functions are declared, the
member functions must be defined after the class body.
When a member function is defined after its corresponding class
definition, the function name is preceded by the class name and the
binary scope resolution operator (: :) i.e.,

returnType className : : funName(parameter −list){


//body
}

Chapter 1
Classes Introduction
Objects Member Functions Outside the Class
Separating Interface from Implementation Example
Conclusions

Member Functions Outside the Class

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 :

className : : constructor (parameter −list){


//body
}

Chapter 1
Classes Introduction
Objects Member Functions Outside the Class
Separating Interface from Implementation Example
Conclusions

Example : Member Functions Outside the Class

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

In this lecture we discussed about


-Classes
-Objects
-public and private member of the class
-Data members and member functions
-Constructors
In the next lecture we will discuss more about
-Classes

Chapter 1

You might also like