0% found this document useful (0 votes)
24 views32 pages

It111 Lecture08

It111 is programming language It teach well it is a descriptive language It is the great language every one can learn easy It is more perfect it is enough

Uploaded by

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

It111 Lecture08

It111 is programming language It teach well it is a descriptive language It is the great language every one can learn easy It is more perfect it is enough

Uploaded by

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

Center for Information and Communication Technology

LECTURE 08

CLASS AND OBJECTS

CONSTRUCTORS AND DESTRUCTORS

UNIFIED MODELING LANGUAGE (UML)


1
Center for Information and Communication Technology

CLASS AND OBJECTS


In Object-Oriented Design (OOD), the first step is to
identify the components called objects.
An object combines data and the operations on that data
in a single unit.
In C++, the mechanism that allows you to combine data
and the operations on that data in a single unit is called a
class

2
Center for Information and Communication Technology

CLASS AND OBJECTS


A class is a template, blueprint or contract that defines
what an object’s data fields and functions will be.
A class is a collection of a fixed number of components
The components of a class are called the members of the
class.
Syntax for defining a class class Class_Identifier
{
classMembersList
};
3
Center for Information and Communication Technology

CLASS AND OBJECTS


Class Members List consists of variable declarations
and/or functions.
A member of a class can be either a variable (to store
data) or a function (to manipulate data)
Object-Oriented Programming (OOP) involves
programming using objects.

4
Center for Information and Communication Technology

CLASS AND OBJECTS


An object represents an entity in the real world that can
be distinctly identified
For example, a student, a desk, a circle, a button, and
even a loan can all be viewed as objects
An object is an instance of a class.
Many instances of a class can be created.
Creating an instance is referred to as instantiation.
The terms object and instance are often
interchangeable.
5
Center for Information and Communication Technology

CLASS AND OBJECTS


An object has a unique :-
Identity
State, and
Behavior
State of an object (also known as properties or attributes) is
represented by data fields with their current values.
A circle object, for example, has a data field, radius, which is
the property that characterizes a circle.
A rectangle object, for example, has data fields, width and
height, which are the properties that characterize a rectangle
6
Center for Information and Communication Technology

Behavior of an object (also known as actions) is defined


by functions.
To invoke a function on an object is to ask the object to
perform an action.
For example, you may define a function named
getArea() for circle objects.
A circle object may invoke getArea() to return its area.
Identity of an object are the names used to differentiate
class members created within a Class.
For example circle(), getArea() etc
7
Center for Information and Communication Technology

CLASS AND OBJECTS


A C++ class uses variables to define data fields and
functions to define behaviors
Objects of the same type are defined using a common
class.

8
Center for Information and Communication Technology

Constructors
A C++ class uses variables to define data fields and
functions to define behaviors.
Additionally, a class provides functions of a special type,
known as constructors, which are invoked when a new
object is created.
A constructor is a special kind of function. Constructors
can perform any action, but they are designed to perform
initializing actions, such as initializing the data fields of
objects.

9
Center for Information and Communication Technology

Constructors
This figure shows an
example of the class
for Circle objects

A class is a blueprint
that defines objects of
the same type

10
Center for Information and Communication Technology

Constructors
A constructor is invoked to create an object.
Constructors are a special kind of function, with three
peculiarities:
Constructors must have the same name as the class
itself.
Constructors do not have a return type—not even void.
Constructors are invoked when an object is created.
Constructors play the role of initializing objects.
The constructor has exactly the same name as the
defining class.
11
Center for Information and Communication Technology

Constructors
The constructor has exactly the same name as the defining
class. Like regular functions, constructors can be overloaded
(i.e., multiple constructors with the same name but different
signatures), making it easy to construct objects with different
sets of data values.
It is a common mistake to put the void keyword in front of a
constructor.
For example,
void Circle()
{
}
12
Center for Information and Communication Technology

Constructors
Most C++ compilers will report an error, but some will
treat this as a regular function, not as a constructor.
Constructors are for initializing data fields. The data field
radius does not have an initial value, so it must be
initialized in the constructor
Variable (local or global) can be declared and initialized
in one statement, but as a class member, a data field
cannot be initialized when it is declared

13
Center for Information and Communication Technology

Public, Private and Protected

14
Center for Information and Communication Technology

Local Variable
A variable defined inside a function (defined inside
function body between braces) is called a local variable or
automatic variable.
Its scope is only limited to the function where it is
defined.
Local variable exists and can be accessed only inside a
function.
The life of a local variable ends (It is destroyed) when the
function exits.
15
Center for Information and Communication Technology

 The variable var cannot be


used inside test() and var1
cannot be used inside
main() function.

 Keyword auto was also


used for defining local
variables before as: auto
int var;

 Latest C++ version, auto


has a different meaning
and should not be used for
defining local variables.

16
Center for Information and Communication Technology

Global Variable
If a variable is defined outside all functions, then it is
called a global variable.
The scope of a global variable is the whole program.
It can be used and changed at any part of the program
after its declaration.
Its life ends only when the program ends.

17
Center for Information and Communication Technology

 In this program, c is a
global variable.

 This variable is visible


to both functions
main() and test() in
the program.

18
Center for Information and Communication Technology

Static Local variable


Keyword static is used for specifying a static variable.
A static local variable exists only inside a function where
it is declared (similar to a local variable) but its lifetime
starts when the function is called and ends only when the
program ends.
The main difference between local variable and static
variable is that, the value of static variable persists the
end of the program.

19
Center for Information and Communication Technology

Output of a program
if var was specified
as static variable

Output

1
1

Output of a program
if var was not
specified as static
variable
Output

1
2

20
Center for Information and Communication Technology

In the above program, test() function is invoked 2 times.


During the first call, variable var is declared as static
variable and initialized to 0. Then 1 is added to var which
is displayed in the screen.
When the function test() returns, variable var still exists
because it is a static variable.
During second function call, no new variable var is
created. The same var is increased by 1 and then
displayed to the screen.
21
Center for Information and Communication Technology

Class clockType
The following statements define the class clockType:
class clockType
{
public:
void setTime(int, int, int);
void getTime(int&, int&, int&) const;
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType&) const;
private:
int hr;
int min;
int sec;
}; 22
Center for Information and Communication Technology

Unified Modeling Language


Unified Modeling Language Class Diagrams
A class and its members can be described graphically
using a notation known as the Unified Modeling Language
(UML) notation
For example, UML class diagram of the class clockType.

23
Center for Information and Communication Technology

Unified Modeling Language


clockType Class name
-hr: int
-min: int
-sec: int Data fields
+setTime(int, int, int): void
+getTime(int&, int&, int&) const: void
+printTime() const: void
+incrementSeconds(): int
+incrementMinutes(): int Constructors and
+incrementHours(): int
+equalTime(const clockType&) const: bool functions

24
Center for Information and Communication Technology

Unified Modeling Language


In UML class diagram or simply class diagram:
 The data field is denoted as
dataFieldName: dataFieldType
The constructor is denoted as
ClassName(parameterName: parameterType)
The function is denoted as
functionName(parameterName: parameterType): returnType

25
Center for Information and Communication Technology

Unified Modeling Language


In UML Class Diagram;
The top box contains the name of the class.
The middle box contains the member variables and their data
types.
The last box contains the member function name, parameter list,
and the return type of the function.
A + (plus) sign before a member name indicates that this is a
public member;
A - (minus) sign before a member name indicates that this is a
private member.
The symbol # before the member name indicates that the
member is a protected member
26
Center for Information and Communication Technology

Variable (Object) Declaration


Once a class is defined, you can declare variables of that
type.
In C++ terminology, a class variable is called a class object
or class instance.
we will use the term class object, or simply object, for a
class variable
The syntax for declaring a class object is the same as that
for declaring any other variable

27
Center for Information and Communication Technology

Variable (Object) Declaration


The following statements declare two objects of type clockType:
clockType myClock;
clockType yourClock;
Each object has 10 members:
seven member functions and three member variables.
Each object has separate memory allocated for hr, min, and sec.
memory is allocated only for the member variables of each class
object.
The C++ compiler generates only one physical copy of a member
function of a class, and each class object executes the same copy of
the member function
28
Center for Information and Communication Technology

Accessing Class Members


Once an object of a class is declared, it can access the members of the
class.
The general syntax for an object to access a member of a class is:
classObjectName.memberName
The class members that a class object can access depend on where
the object is declared.
If the object is declared in the definition of a member function of
the class, then the object can access both the public and private
members.
If the object is declared elsewhere (for example, in a user’s
program) then the object can access only the public members of
the class.
29
Center for Information and Communication Technology

Accessing Class Members


In C++, the dot, . (period), is an operator called the
member access operator

30
Center for Information and Communication Technology

Built-in Operations on Classes


Most of C++’s built-in operations do not apply to classes.
You cannot use arithmetic operators to perform arithmetic
operations on class objects (unless they are overloaded).
For example, you cannot use the operator + to add two class
objects of, say, type clockType.
Also, you cannot use relational operators to compare two
class objects for equality (unless they are overloaded).
The two built-in operations that are valid for class objects are:-
member access (.) and
assignment (=)
31
Center for Information and Communication Technology

Built-in Operations on Classes


To access an individual member of a class; use name of
the class object, then a dot, and then the member name.
For example, if myClock is a clockType object, in the
statement
myClock.incrementSeconds();,
myClock accesses the member incrementSeconds

32

You might also like