Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.

Similar presentations


Presentation on theme: "Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance."— Presentation transcript:

1 Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance

2 Classes The C++ class is derived form the ‘C’ structure Classes have the same syntax as ‘C’ structures Classes have additional features to provide the OO features – The member of a class can be functions as well as data – The member can be public, private or protected (used in inherited classes) A class definition is a template for creating objects A class’s name becomes a data-type and is used in the same way as a data type.

3 Classes General format class myclass { public: Public members go here. These are normally functions, but can be data. private: Private member go here. These are normally data, but can be functions }; // End of class definition- note the semi-colon

4 Classes A class is normally split into two files – A header file (.h) that contains the definition of the class and the declaration of the member functions – A C++ source file (.cpp) that contains the definitions of the member functions. Alternatives – All function definitions embedded within the class definition. Now no need for separate header file. - S ometimes done for templates! – classes placed within another C++ source file. Not normally good practice.

5 Class definition - header file #ifndef myclass_h #define myclass_h class myclass { public: void set_height( float h); void set_age(int a); int get_age(); float get_height(); private: int age; float height; }; #endif The class definition is place in file myclass.h To prevent class re-definition the preprocessor directives #ifndef and #endif are used. The code for the functions (also called methods) can be placed directly within the class definition. It is normally preferred to only declare the functions in the class and to place the function definitions in a separate file.

6 Class member functions - cpp file The file myclass.cpp containing the function definitions. Note: The function names are preceded with the class name to which the function belongs and the scope resolution operator :: The class definition file is included. Note the header file name is in double quotes. This is the way C++ separates interface (the header file) from implementation (the cpp file). #include “myclass.h” void myclass::set_height (float h) { height = h; } float void myclass::get_height ( ) { return(height); } void myclass::set_age (int a) { age = a; } int myclass::get_age( ) { return age; };

7 Creating objects An object is an instance of a class.

8 Creating objects If there is a class called myclass. To create a myclass object in C++ :- myclass billy; // billy is the name of the myclass object to apply the class methods (i.e. member functions) to an object we use the name of the object, a period and the function name and any arguments that the function requires. billy.set_age(22); int x; x = billy.get_age( );

9 Alternatively using new and pointers myclass * p = new myclass; p->set_age(22); int x; x = p-> get_age( ); * indicates declaration of a pointer -> is the indirection operator which is used with a pointer to access the item that the pointer is referencing.


Download ppt "Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance."

Similar presentations


Ads by Google