1 / 15

Structures/Classes

Structures/Classes. CS 308 – Data Structures. 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; };.

constantine
Download Presentation

Structures/Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 (->): rcPtr -> 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_name::function_name(arguments);   ( :: is the "scope resolution operator") void rectangle::draw() { cout << "position is " << xpos << ypos << endl; } void rectangle::posn(int x, int y) { xpos = x; ypos = y; } void rectangle::move(int dx, int dy) { xpos += dx; ypos += dy; }

  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.

More Related