Structures and Classes

Embed Size (px)

Citation preview

  • 8/3/2019 Structures and Classes

    1/15

    CSC 211 - Data Structures

  • 8/3/2019 Structures and Classes

    2/15

    It is an aggregate data type built usingelements of other types.

    Declaring a structure requires declaring its

    members and their data types.struct rectangle {

    float height;

    float width;

    int xpos;int ypos;

    };

  • 8/3/2019 Structures and Classes

    3/15

    They are declared like variables of any othertype.

    rectangle rc;

    rectangle &rcRef = rc;

    rectangle *rcPtr = &rc;

  • 8/3/2019 Structures and Classes

    4/15

    dot operator (.):rc.height = 15.89;

    rcRef.height = 15.89;

    arrow operator (->):rcPtr -> height = 15.89; or

    (*rcPtr).height = 15.89;

    Important: the parentheses around *rcPtrare

    necessary since the member operator . takesprecedence over the dereference operator.

  • 8/3/2019 Structures and Classes

    5/15

    Functions which operate on the data of thestructure.

    The prototype of a member function appearswithin the structure definition.

    struct rectangle {float height;float width;int xpos;int ypos;

    void draw(); // draw member functionvoid posn(int, int); // position member functionvoid move(int, int); // move member function

    };

  • 8/3/2019 Structures and Classes

    6/15

    Usually, they are declared outside the structure.data_type structure_name::function_name(arguments);

    ( :: is the "scope resolution operator")void rectangle::draw(){cout

  • 8/3/2019 Structures and Classes

    7/15

    We refer to a member function just as anyother variable of the structure.

    rc.draw();

    rc.posn(100, 100);

    rc.move(50, 50);

  • 8/3/2019 Structures and Classes

    8/15

  • 8/3/2019 Structures and Classes

    9/15

    The actual data representation used within astructure is of no concern to the structure'sclients.

    Protects data members from receiving invalidvalues.

    It promotes program modifiability (if therepresentation of data changes, only the

    member functions need to change).

  • 8/3/2019 Structures and Classes

    10/15

    Most common member access specifiers are:public and private

    struct rectangle {private:float height;float width;int xpos;int ypos;

    Theprivate keyword specifies that thestructure members following it are privateto the structure and can only be accessed bymember functions (and byfriend functions)

    public:

    void draw(); // draw member functionvoid posn(int, int); // position member functionvoid move(int, int); // move member function

    };

  • 8/3/2019 Structures and Classes

    11/15

    Thepublic keyword specifies that thestructure members following it are public tothe structure and may be accessed from

    outside the structure.void main(){rectangle rc;

    rc.height = 20; // Error: not accessible}

    Another way to access private datamembers is by usingget-set memberfunctions which are public to the structure.

    (cont.)

  • 8/3/2019 Structures and Classes

    12/15

  • 8/3/2019 Structures and Classes

    13/15

    Practically, there are no differences betweenstructures and classes:(i) A class is a structure which has all of itsmembers private by default.

    (ii) Structures have all of their memberspublic by default.

    class rectangle {private: // not needed but included for clarityfloat height;

    float width;int xpos;int ypos;

    public:void draw(); // draw member functionvoid posn(int, int); // position member functionvoid move(int, int); // move member function

    };

  • 8/3/2019 Structures and Classes

    14/15

    An instance or an object is a variable of typeclass.

  • 8/3/2019 Structures and Classes

    15/15

    Data and functions co-exist inside a class.

    Member functions are called without passingthe data members of the class to them.

    There is far less chance of misusing functionsby passing them the wrong data.