Structures/Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Structures/Classes

Description:

It is an aggregate data type built using elements of other types. ... Information Hiding. Philosophy behind. information hiding ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 16
Provided by: Penelope69
Learn more at: https://www.cse.unr.edu
Category:

less

Transcript and Presenter's Notes

Title: Structures/Classes


1
Structures/Classes
  • CS 308 Data Structures

2
What is a structure?
  • It is an aggregate data type built using
    elements of other types.
  • Declaring a structure requires declaring its
    members and their data types.
  • struct rectangle
  • float height
  • float width
  • int xpos
  • int ypos

3
Structure variables
  • They are declared like variables of any other
    type.
  •  
  • rectangle rc
  • rectangle rcRef rc
  • rectangle rcPtr rc

4
Accessing members of structures
  • dot operator (.)
  • rc.height 15.89
  • rcRef.height 15.89
  • arrow operator (-gt)
  • rcPtr -gt height 15.89 or
  • (rcPtr).height 15.89
  • Important the parentheses around rcPtr are
    necessary since the member operator . takes
    precedence over the dereference operator.

5
Member functions (methods)
  • Functions which operate on the data of the
    structure.
  • The prototype of a member function appears within
    the structure definition.
  •  
  • struct rectangle
  • float height
  • float width
  • int xpos
  • int ypos
  • void draw() // draw member function
  • void posn(int, int) // position member
    function
  • void move(int, int) // move member function

6
Member function declaration
  • Usually, they are declared outside the structure.
  •  
  • data_type structure_namefunction_name(arguments)
  •   ( is the "scope resolution operator")
  •  
  • void rectangledraw()
  • cout ltlt "position is " ltlt xpos ltlt ypos ltlt endl
  •  
  • void rectangleposn(int x, int y)
  • xpos x
  • ypos y
  •  
  • void rectanglemove(int dx, int dy)
  • xpos dx

7
Referring to a member function
  • We refer to a member function just as any other
    variable of the structure.
  •  
  • rc.draw()
  • rc.posn(100, 100)
  • rc.move(50, 50)

8
Information Hiding
9
Philosophy behind information hiding
  • The actual data representation used within a
    structure is of no concern to the structure's
    clients.
  • Protects data members from receiving invalid
    values.
  • It promotes program modifiability (if the
    representation of data changes, only the member
    functions need to change).

10
Controlling access to members
  • Most common member access specifiers are public
    and private
  •  
  • struct rectangle
  • private
  • float height
  • float width
  • int xpos
  • int ypos
  •  
  • The private keyword specifies that the structure
    members following it are private to the structure
    and can only be accessed by member functions (and
    by friend functions)

public void draw() // draw member
function void posn(int, int) // position
member function void move(int, int) // move
member function
11
Controlling access to members
(cont.)
  • The public keyword specifies that the structure
    members following it are public to the structure
    and may be accessed from outside the structure.
  •  
  • void main()
  • rectangle rc
  •  
  • rc.height 20 // Error not accessible
  •  
  • Another way to access private data members is by
    using get-set member functions which are public
    to the structure.

12
Classes
13
What is a class?
  • Practically, there are no differences between
    structures and classes
  • (i) A class is a structure which has all of its
    members private by default.
  • (ii) Structures have all of their members
    public by default.
  •  
  • class rectangle
  • private // not needed but
    included for clarity
  • float height
  • float width
  • int xpos
  • int ypos
  • public
  • void draw() // draw member
    function
  • void posn(int, int) // position member
    function
  • void move(int, int) // move member function

14
What is an object?
  • An instance or an object is a variable of type
    class.

15
Class-based programming
  • Data and functions co-exist inside a class.
  • Member functions are called without passing the
    data members of the class to them.
  • There is far less chance of misusing functions by
    passing them the wrong data.
Write a Comment
User Comments (0)
About PowerShow.com