Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.

Similar presentations


Presentation on theme: "Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism."— Presentation transcript:

1 Classes in C++ By: Mr. Jacobs

2 Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism for constructing them,  Learn that the construction of a data type is based both on the variables and functions.  C++ provides a unique way of combining variables and functions together in a self-contained, cohesive unit from which objects can be created.

3 Key Terms  Abstract data type  Class  Class members  Constructor  Data hiding  Data members  Data type  Declaration section  Default constructor  Destructor  Implementation section  Inline member function  Instance variables  Member functions  Method  Object  Private  Public

4 Class (Abstract Data Types)  In computer terminology, the combination of data and their associated operations is defined as data type.  Data type = allowable data values + operational capabilities  An Abstract Data Type is simply a user-defined type that fines both a type of data and the operation that can be preformed on it.  Such user defined data types are required when we wish to create objects that are more complex than simple integers and characters.

5 Class Construction  A class defines both data and functions.  A class is constructed in two parts  Declaration Section  Implementation Section

6 Class Construction  When a function is part of a class, it is formally referred to as a method to clearly denote class membership.  Variables and functions listed in the class declaration section are colectively referred to as class members.  Individually, they are referred to as  Variables  Data Members  Instance Variables  Functions  Member Methods

7

8 Declaration Section  Begins with the keyword Class  Followed by the Name of the class.  The body consists of variable and function declarations.  Keywords Private and Public define access rights  Private - May only be accessed by using the class functions (or friend functions)  Meant to enforce data security by requiring all access to private data members through the provided member functions.  Called data hiding

9 Declaration Section  Public – can be called by any objects and functions not in the class.  All class functions should be public  Constructor – can be used to initialize class data members with values.  Has the same name as the Class itself  Ex. Date::Date  Has no return type, requirement!  All other remain function types must have a return type.

10 Implementation Section  Where the member functions declared in the declaration section are written.  This format correct for all functions except the constructor

11 Objects  Notice that class declaration and class implementation sections do not create any variables of the class type.  This is true in all C++ types  Variables defined to be of user-declared classes are referred to as objects.  Declared in the main  Whenever a new object is defined, memory is allocated for the objects and its data members are automatically initialized.  objectName.attributeName  objectName = name of specific object  attributeName = name of data member defined in the object’s class

12 Objects  The syntax for referring to an object’s function  objectName.functionName(parameters)  objectName = name of the a specific object  functionName = is the name of one of the functions defined in the object’s class.  Objects are also referred to as instances of a class, and the process of creating a new object is frequently referred to as an instantiation.  Each time a new object is instantiated (created), a new set of data members belonging to the object is created.  The particular values contained in these data members for each object determine the object’s state.

13 Objects  In addition to the data types allowed for an object, a class also defines behavior, that is, the operations that are permitted to be performed on an object’s data members.  Users of the object need to know what these functions  Can Do  How to activate them  Unless run-time and space implications are relevant, user does not need to know how implementation is done.

14 Point of Information  An interface consists of a class’s public member function declarations and any supporting comments. Thus, the interface should be all that is required to tell a programmer how to use the class.  The implementation consists of both the class’s implementation section, which consists of both private and public member definitions, and the classes’ private data members, which are contained in a class’s declaration section.

15 Constructors  A constructor function is an function that has the same name as its class/  Multiple constructors can be defined for each class as long as they are distinguishable by the number and types of parameters.  Purpose is to initialize a new object’s data members.  Called each time an object is created  A Constructor must have:  Same name as the class it belongs to  No return type(not even void)

16 Constructors  If you do not include a constructor, the complier supplies a Default Constructor.  It is best to keep constructors simple and used only for initialization purposes  In C++, object members are initialized in the order in which they are declared in the class declaration section and not in the order in which they may appear.

17 Constructors  Constructors maybe overloaded in the same manner as any other user-written C++ function  Just as constructors may be overloaded, they may also be written as inline member functions. Doin so simply means defining the function in the class declaration section.  If a constructor is defined for a class, a user- defined default constructor also should be written because the complier does not supply it.

18 Destructor Function  A destructor function is called each time an object goes out of scope.  Destructors must have the same name as their class, but are preceded by a tilde (~).  Only one destructor per class.  A destructor function takes no arguments  Returns no value  Just like a constructor, if no destructor provided, complier provides a do-nothing destructor.

19 Common Errors 1.Failing to terminate the class declaration section with a semicolon. 2.Including a return type with the constructor’s prototype or failing to include a return type with the other function’s prototypes. 3.Using the same name for a data member as a member function 4.Defining more than one default constructor for a class 5.Forgetting to include the class name and scope, ::, in the header line of all member functions defined in the class implementation section. All of these errors result in a compiler error message.


Download ppt "Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism."

Similar presentations


Ads by Google