(2,5) w.print() invokes the print 'member function' of the point structure."> (2,5) w.print() invokes the print 'member function' of the point structure.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 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.

Similar presentations


Presentation on theme: "1 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."— Presentation transcript:

1 1 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

2 2 C++ Classes: 159.234 C++ implements objects by extending the idea of structures. We can specify functions and operations associated with a struct as well as data. typedef We no longer have to use typedef to define a new type when it is a struct. The name of a struct is automatically a new type: point struct point { int x,y; };... point w;

3 3 C++ Structure 159.234 A structure not only groups data, it also groups operations that can be performed on the data. struct point { int x,y; print(void) 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.

4 4 C++ Structure 159.234 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(); w.print();

5 5 C++ Structure 159.234 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!

6 6 C++ Public and Private members: 159.234 struct point { public: void print(void) { cout << "(" << x << ",” << y << ")”; } init void init(int u, int v) { x x = u; y 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:

7 7 C++ Public and Private members 159.234 int main() { point w; w.init(2,5); w.print(); } 'good practice Stopping un-authorised access to data is 'good 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.

8 8 C++ Class 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 inline by default 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.

9 9 Classes Point struct Point { int x,y; //public void print();//public public: void init(int, int); private: int distance; }; Point class Point { int x,y; //private void print();//private public: void init(int, int); private: int distance; };

10 10 Scope 159.234 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 class point { public: print(void); void print(void); private: int x,y; }; Class scope: class scope operator :: The class scope operator :: is used to define functions outside the class declaration. point::print(void) void point::print(void) { cout << "(" << x << "," << y << ")"; }

11 11 C++ Member Functions 159.234 Member functions can also be overloaded. class point { public: void init(int u, int v) {x = u; y = v}; print(void) void print(void); print(char *s) void print(char *s); private: int x,y; }; point::print(void) void point::print(void) { cout << "(" << x << ",” << y << ")”; } point::print(char *s) void point::print(char *s) { cout << s; print(); }

12 12 C++ Overloading Member Functions 159.234... point w; w.init(4,7); w.print(); cout << endl; w.print("point = "); ->(4,7) point = (4,7)

13 13 C++ Overloading Member Functions 159.234 Within the second form of the print function, there is a call to the other function print (it has different arguments) print void point::print(char *s) { cout << s; print(); } No scope operator is required. global function printprint is also a class member function If we want to call a global function print, and print is also a class member function, then we use the scope operator on its own - external scope print(void) void print(void) { cout << " The global print function”; } void point::print(char *s) { cout << s; ::print(); }

14 14 C++ Class in a Class 159.234 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'; }

15 15 C++ Static Members of Classes 159.234 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 P x,y; x.c cout << x.c; x.c x.c = 'A'; y.c cout << y.c; }

16 16 C++ Static Members of Classes 159.234 same Correct - but mis-leading. x.c and y.c are the same thing. P::c Better to refer to the static member as P::c int main() { P x; P::c P::c = 'A'; P::c cout << P::c; }

17 17 C++ this 159.234 self- referential Each class variable has a 'self- referential' pointer associated with it. Functions within a class may use the variable this class X { public: X* my_address(void); }; X* X::my_address(void) { return this; } int main() { X x; cout << x.my_address(); }

18 18 C++ Objects 159.234 action- oriented - C programming is action- oriented - we concentrate on using functions. object- oriented - C++ programming is object- oriented - we concentrate on using classes. instance of a built-in type An instance of a built-in type is called a variable int j; //j is a variable C++ classes are user-defined types an instance of a class we call an instance of a class an object.


Download ppt "1 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."

Similar presentations


Ads by Google