• No results found

Lec13- Introduction to classes.pdf

N/A
N/A
Protected

Academic year: 2020

Share "Lec13- Introduction to classes.pdf"

Copied!
16
0
0

Loading.... (view fulltext now)

Full text

(1)

Structured Programming

Using C++

Lecture 13 : Classes & Objects

Dr. Amal Khalifa

Lecture Contents:

2

Classes

 Defining, member functions  Public and private members  Accessor and mutator functions  Structures vs. classes

Classes, Pointers, Dynamic Arrays

 The this pointer

 Destructors, copy constructors

(2)

Classes

 Similar to structures

 Adds member FUNCTIONS

 Not just member data

 Integral to object-oriented programming

 Focus on objects

 Object: Contains data and operations

 In C++, variables of class type are objects

6-3

Encapsulation

Any data type includes

 Data (range of data)

 Operations (that can be performed on data)

Example:

int data type has:

Data: +-32,767

Operations: +,-,*,/,%, logical, etc.

Same with classes

 Collection of data values together with set of basic

operations to be allowed for the values

(3)

Encapsulation

• The bundling of data and

procedures(functions) into a single unit(called class).

• Class is an example of

encapsulation, where various data elements and member functions are wrapped up together.

• used in object oriented programming

• Closely related to

information hiding.

•Abstract Data Types

Dr. Amal Khalifa, 2012 5

Class data Types

Dr. Amal Khalifa, 2012

6

Type

(4)

Class Definitions

Defined similar to structures

Example:

class DayOfYear  name of new class type

{

public:

void output();  member function!

int month; int day; };

Notice only member function’s prototype

 Function’s implementation is elsewhere

6-7

Declaring Objects

Declared same as all variables

 Predefined types, structure types

Example:

DayOfYear today, birthday;

 Declares two objects of class type DayOfYear

Objects include:

 Data

 Members month, day

 Operations (member functions)  output()

(5)

Objects & Classes

• Class == blueprint

• Object == concrete

building

• One class type  many

objects

Dr. Amal Khalifa, 2012 9

Class Member Access

 Members accessed same as structures  Example:

today.month

today.day

 And to access member function:

today.output();  Invokes member function

(6)

Class Member Functions

Must define or "implement" class member functions

Like other function definitions

 Can be after main() definition

 Must specify class:

void DayOfYear::output() {…}

 :: is scope resolution operator

 Instructs compiler "what class" member is from

 Item before :: called type qualifier

6-11

Example: DayOfYear class

Dr. Amal Khalifa, 2012 12

Define a class that represents a date of a specific year as a combination of day, and month.

(7)

Example:

•Class type definition

• Data • functions

6-13

Example

• object declaration

•Member access

•Member function call

(8)

Example

•Member function’s

definition :

• Refers to

member data of class with no qualifiers

• used for all

objects of the class

• Will refer to

"that object’s" data when invoked

6-15

Dot and Scope Resolution Operators

 Used to specify "of what thing" they are members

 Dot operator (.):

 Specifies member of particular object

 Scope resolution operator (::) :

 Specifies what class the function

definition comes from

(9)

Public and Private Members

 Data in class almost always designated private in

definition!

 Upholds principles of OOP  Hide data from user

 Allow manipulation only via operations  Which are member functions

 Public items (usually member functions) are

"user-accessible"

6-17

Private & public

Dr. Amal Khalifa, 2012

(10)

Public and Private Example

• Data now private

• Objects have no

direct access

• Object today can

ONLY access public members 6-19 class DayOfYear { public: void input(); void output(); private: int month; int day; }; Void main() { DayOfYear today;

cin >> today.month; // NOT ALLOWED! cout << today.day; // NOT ALLOWED!

//public members  OK

today.input(); today.output(); }

Public and Private Style

Can mix & match public & private

More typically place public first

 Allows easy viewing of portions that can be USED by

programmers using the class

 Private data is "hidden", so irrelevant to users

Outside of class definition, cannot change (or even

access) private data

(11)

Accessor and Mutator Functions

•Object needs to "do

something" with its data

•Call accessor member

functions

• Allow object to read data

• Also called "get

member functions"

• Simple retrieval of member data

Mutator member

functions

• Allow object to change

data

• Manipulated based on application

6-21

Example: set/get functions

Dr. Amal Khalifa, 2012 22

Define more member functions on the DayOfYear class: setDay : replace the object’s day information with its argument value.

setMonth : replace the object’s month information with its argument

(12)

A Class’s Place

Class is full-fledged type!

 Just like data types int, double, etc.

Can have variables of a class type

 We simply call them "objects"

Can have parameters of a class type

 Pass-by-value

 Pass-by-reference

Can use class type like any other type!

6-23

Example: More member functions

Dr. Amal Khalifa, 2012 24

Define more member functions on the DayOfYear class: IncrementDay : update the object’s data by advancing the date one day ahead.

IncrementMonth : update the object’s data by advancing the date one month ahead.

AddDays: update the object’s data by advancing the date by a number of days.

CopyFrom: copy the data members of a given object to “this” object

(13)

and Implementation

User of class need not see details of how class is

implemented

 Principle of OOP  encapsulation

User only needs "rules"

 Called "interface" for the class  In C++  public member functions and

associated comments

Implementation of class hidden

 Member function definitions elsewhere  User need not see them

6-25

Example: set/get functions

Dr. Amal Khalifa, 2012 26

Change the definition of the DayOfYear class to include the following:

year data member

setYear : replace the object’s year information with its argument

(14)

Structures versus Classes

 Structures

 Typically all members public  No member functions

 Classes

 Typically all data members private  Interface member functions public

 Technically, same

 Perceptionally, very different mechanisms

6-27

Thinking Objects

Focus for programming changes

 Before  algorithms center stage

 OOP  data is focus

Algorithms still exist

 They simply focus on their data

 Are "made" to "fit" the data

Designing software solution

 Define variety of objects and how they interact

(15)

Back to pointers !!

The -> operator

 Shorthand notation

Combines dereference operator, *, and dot

operator

Specifies member of class "pointed to” by given

pointer

Example:

MyClass *p;

p = new MyClass;

p->grade = "A"; //Equivalent to: (*p).grade = "A";

10-29

The this Pointer

10-30

Member function definitions might need to refer to

calling object

Use predefined this pointer

 Automatically points to calling object:

Class Simple {

public:

void showStuff() const; private:

int stuff; };

Two ways for member functions to access:

cout << stuff;

(16)

Array of Objects

Dr. Amal Khalifa, 2012

31

 Example:

DayOfYear Days[10], *MyDays;

Days[0].input(); Days[0].output(); //dynamic..

MyDays = new DayOfYear[10]; MyDays[0].input();

MyDays[0].output();

Dr. Amal Khalifa, 2012 32

That’s all for today !!

data Class object oriented programming

References

Related documents

Classes and Objects Class Declaration Class Members Data Constructors Data Constructors Destructors Member Functions.. Class Member Visibility

Write a class specifier that creates a class called student with one private data member called rn, of type int, and two public member function : putdata ( ) and getdata ( )..

Graph DBMSs Use A Very Simple Object Model Tree Structures 1-to-Many Relationship Data Object Class A y Data Object Class A. Graph (Network) Structures

A good way to do this is to start with an abstract button class which contains a general Execute method, and then subclass that to create the actual buttons.. //an abstract

Definition of UI commands - M.Asai (SLAC) 20 Target class object Command directory. Target class

WAP TO DEFINE NESTED CLASS ‘STUDENT_INFO’ WHICH CONTAINS DATA MEMBERS SUCH AS NAME, ROLL NUMBER AND SEX AND ALSO CONSISTS OF ONE MORE CLASS ‘DATE’ WHOSE DATA MEMBERS ARE DAY,

16.10 When Constructors and Destructors Are Called 16.11 Using Data Members and Member Functions 16.12 A Subtle Trap: Returning a Reference to a private Data Member.. 16.13

The String class also defines a number of static methods named valueOf() that return string representations of primitive Java data types and objects.. The Object class defines