Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes C++ representation of an object

Similar presentations


Presentation on theme: "Classes C++ representation of an object"— Presentation transcript:

1 Classes C++ representation of an object
Used to support object oriented programming A class is a user-defined container that provides: Encapsulation of both object data and supported operations An extension of the “struct” type to include operations as well as data Inheritance (ability to derive new classes from existing ones) Polymorphism “overloading” of class methods (functions) and/or built-in C++ operators

2 Class Definition Syntax
Defined with the keyword “class” “public” section Contains information visible to a user of the class “private” section Contains information only visible within the class itself (Note: a third section, called “protected” is used by inherited classes) class className { public: List all public data and functions private: List all private data and functions }; // Don’t forget the semicolon!

3 Classes: public vs. private
Classes store both data and methods (aka. functions) Methods are used to manipulate data Direct data manipulation is discouraged public vs. private sections Access to data/methods in the private section is limited to the class itself Only data/methods in the public section are visible externally Together they facilitate “encapsulation” User is shielded from internal implementation details of the object The object becomes a “black box”

4 Simple Class Example #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); // default constructor! void setValue(int v) ; int getValue(); private: int value; }; #endif #include “MyClass.h“ MyClass::MyClass() { value=0; } void MyClass::setValue(int v) { value= v; int MyClass::getValue() { return value; MyClass.h MyClass.cpp contains class definition and prototypes contains implementation details Classes are generally defined and implemented in two files

5 Class Creation Class is defined and implemented in two files
ex: MyClass.h and MyClass.cpp Class definition and function/method prototypes placed in header file MyClass.h Class function implementations placed in a “coding” file MyClass.cpp Header file is #include(‘ed) in the .cpp file at compilation

6 Class Creation (header file)
contains class definition public and private sections Data members Prototypes for methods #ifndef compiler directive Prevents multiple definitions of class if header is included more than once #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); // default constructor! void setValue(int v) ; int getValue(); private: int value; }; #endif

7 Class Implementation (“coding” file)
#include the header file Contains implementation details for prototyped methods (functions) Details are hidden from users Class member functions can access all class data members Scope Resolution operator Double colon “::” prefix for each member function Informs the compiler that the function is a member of the class #include “MyClass.h“ MyClass::MyClass() { value=0; } void MyClass::setValue(int v) { value= v; int MyClass::getValue() { return value; Note the use of scope resolution operator

8 Constructors Special member function for creating new instances of the class MyClass::MyClass() { … }; Important properties! Same name as the class No return value Newly created object instance is “returned” Automatically called upon creation of a new object instance “Default constructor” has no arguments Additional constructors can be created that take arguments (polymorphism)

9 Usage of Classes and Objects
Create a class instance E.g. MyClass c1; Creates an instance of the MyClass object named “c1” Default constructor automatically invoked Allocates space in memory for the new variable (c1) Similar to other data type declarations int value; Create and allocate memory for a variable named “value” of type int

10 Other Member Functions
May be either public or private Three main types: Mutators (modifiers) modify or change internal data c1.setvalue(x) Inspectors (accessors) Retrieve internal data c1.getvalue(x) Facilitators Helper functions (often private) Member functions are accessed using the dot (.) operator Similar to using a struct Data members are also accessible using (.) Member function use is preferred

11 Access to the Class Class definition/coding file is compiled separately Result is an object file (.o) that contains the complete class implementation Include the header file in any program that wishes to use the object because the class definition is contained in the header file #include “MyClass.h” (Note: use quote marks instead of brackets (< >) around class file) Link the user program together with the object file for the class Facilitates “encapsulation” Class definition is separate from the user program The class is, effectively a new, independent, data type

12 Compiler Directives Prevents multiple definitions of a class
#ifndef tests MYCLASS_H If MYCLASS_H is not defined #define defines it, then… Then, everything up to the #endif is included i.e. the class gets defined If MYCLASS_H is defined skip to the #endif skip over everything between #ifndef and #endif Don’t try to (re)define the class! #ifndef MYCLASS_H #define MYCLASS_H (define the class and prototypes) #endif


Download ppt "Classes C++ representation of an object"

Similar presentations


Ads by Google