Classes struct Public and Private Parts of a
Classes • • struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this 1
C++ 159. 234 Classes: C++ implements objects by extending the idea of structures. We can specify functions and operations associated with a struct as well as data. The name of a struct is automatically a new type: struct point { int x, y; }; . . . point w; We no longer have to use typedef to define a new type when it is a struct. 2
C++ 159. 234 Structure A structure not only groups data, it also groups operations that can be performed on the data. struct point { int x, y; void print(void) { cout << "(" << x << ", " << y << ")"; } }; int main() { point w; w. x = 2; w. y = 5; w. print(); } -> (2, 5) w. print() invokes the print 'member function' of the point structure. 3
C++ 159. 234 Structure C++ limits the visibility of data and functions by allowing public and private parts to a structure. By default all elements of a structure are public. Programs that use variables of this type are allowed to access the data and functions of the structure. w. y = 5; w. print(); 4
C++ 159. 234 Structure Declarations within the private section of a structure are only visible to the structure itself. struct point { public: void print(void) { cout << "(" << x << ", " << y << ")"; } private: int x, y; }; We can no longer access x and y but we are allowed to print them! 5
C++ 159. 234 Public and Private members: struct point { public: void print(void) { cout << "(" << x << ", ” << y << ")”; } void init(int u, int v) { init x = u; y = v; } private: int x, y; }; Now the structure is very secure! - no one can alter the data of the structure without using the functions that are supplied by the structure itself: 6
C++ 159. 234 Public and Private members int main() { point w; w. init(2, 5); w. print(); } Stopping un-authorised access to data is 'good practice' practice and is one of the benefits of using C++. The keywords public and private can be used many times within a structure. It is usual to put all public members first and private members last. 7
C++ 159. 234 Class C++ introduces a new keyword: class A class is exactly the same as a struct - except that all members are private unless specified otherwise The exact opposite of a struct Most people use class rather than struct Always specify private and public -do not use the defaults Functions written within a class or struct are inline by default inline functions should be small, and those that are defined within a class should be one or two lines at most. 8
Classes class Point { int x, y; //private void print(); //private public: void init(int, int); private: int distance; }; struct Point { int x, y; //public void print(); //public: void init(int, int); private: int distance; }; 9
Scope We can define class member functions outside the class definition. They are then no longer inline by default. Only the function prototype needs to be included within the class point { public: void print(void); private: int x, y; }; 159. 234 Class scope: The class scope operator : : is used to define functions outside the class declaration. void point: : print(void) { cout << "(" << x << ", " << y << ")"; } 10
C++ 159. 234 Member Functions Member functions can also be overloaded. class point { public: void init(int u, int v) {x = u; y = v}; void print(void); print(void) void print(char *s); *s) private: int x, y; }; void point: : print(void) { cout << "(" << x << ", ” << y << ")”; } void point: : print(char *s) { cout << s; print(); } 11
C++ 159. 234 Overloading Member Functions. . . point w; w. init(4, 7); w. print(); cout << endl; w. print("point = "); -> (4, 7) point = (4, 7) 12
C++ 159. 234 Overloading Member Functions Within the second form of the print function, there is a call to the other function print (it has different arguments) void point: : print(char *s) { print cout << s; print(); } No scope operator is required. If we want to call a global function print, print and print is also a class member function, function then we use the scope operator on its own external scope void print(void) { cout << " The global print function”; } void point: : print(char *s) { cout << s; : : print(); 13 }
C++ 159. 234 Class in a Classes can contain other classes. char c; class Y { public: char c; }; class X { public: char c; Y y; }; int main() { X x; c = 'A'; x. c = 'B'; x. y. c = 'C'; } 14
C++ 159. 234 Static Members of Classes Static members of classes: If a member variable is declared static, there is only one instance of that in the program. A static variable is common to all class variables. class P { public: static char c; }; char P: : c = 'W'; int main() { P x, y; cout << x. c; x. c = 'A'; cout << y. c; y. c } 15
C++ 159. 234 Static Members of Classes Correct - but mis-leading. x. c and y. c are the same thing. Better to refer to the static member as P: : c int main() { P x; P: : c = 'A'; cout << P: : c; P: : c } 16
C++ this Each class variable has a 'selfreferential' referential pointer associated with it. Functions within a class may use the variable this 159. 234 class X { public: X* my_address(void); }; X* X: : my_address(void) { return this; } int main() { X x; cout << x. my_address(); } 17
C++ 159. 234 Objects C programming is actionoriented we concentrate on using functions. C++ programming is objectoriented we concentrate on using classes. An instance of a built-in type is called a variable int j; //j is a variable C++ classes are user-defined types we call an instance an object. of a class 18
- Slides: 18