150 likes | 325 Views
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; };.
E N D
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; };
Structure variables • They are declared like variables of any other type. rectangle rc; rectangle &rcRef = rc; rectangle *rcPtr = &rc;
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.
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 };
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; }
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);
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).
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 • };
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.
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 };
What is an object? • An instance or an object is a variable of type class.
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.