adts and c++ classes

26
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading

Upload: olive

Post on 19-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

ADTs and C++ Classes. Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading. Classes and Members. In C++ the mechanism to create objects is called the class Member Functions are the way we manipulate objects - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ADTs and C++ Classes

ADTs and C++ Classes

• Classes and Members

• Constructors

• The header file and the implementation file

• Classes and Parameters

• Operator Overloading

Page 2: ADTs and C++ Classes

Classes and Members

• In C++ the mechanism to create objects is called the class

• Member Functions are the way we manipulate objects

• Classes can support information hiding

• Programs use classes without knowing how they are implemented

Page 3: ADTs and C++ Classes

Class Members

• C++ classes have Member Functions and Member variables

• Classes can have a public section and a private section

• Members in the public session can be seen from outside the class

• Members in the private session can not be seen from outside the class

Page 4: ADTs and C++ Classes

C++ class syntax (definition)

• The class head• The member list

– The public section• Member functions (Typically)• Member variables (Occasionally)

– The private section• Member variables (Typically)• Member functions (Occasionally)

Page 5: ADTs and C++ Classes

A common pattern for new classes

• Public member functions permit programmers to modify and examine objects of the new class

• Constant public members let a member function examine data (but not modify it)

• Private member variables store information about the status of an object of the new class (not available outside the class itself)

Page 6: ADTs and C++ Classes

Using a Class

• To use a class, we create an instance of the object

• Each instance has its own private data and its own member functions

• We activate the member functions to tell the object to perform the behavior [This is sometimes referred to as sending a message to the object]

Page 7: ADTs and C++ Classes

Implementing a Member Function

• Member functions are attached to their respective classes with the

scope resolution operator (::)

• The operator is placed between the class name and the function name

• A function definition looks like this:

<return type> <class name>::<function name()>

void myclass::myFunction(…) {…};

Page 8: ADTs and C++ Classes

The Key to Member Variables

• Each object keeps its own copies of all member variables

• When a member function’s implementation refers to a member variable, then the actual member variable always comes from the object that activated the member function

Page 9: ADTs and C++ Classes

Constructors

• Provides an initialization function that is guaranteed to be called.

• It is called every time an instance of the class is created.

• It can have several different number of and type of parameters.

• Since it operates inside a class, it does not have any return value (no return type allowed)

Page 10: ADTs and C++ Classes

Constructor (cont)

• A constructor with no parameters is called the default constructor

• When a class has no constructor, the compiler automatically creates the automatic default constructor that simply calls the default constructor for member variables that are objects of other classes

• You should never depend on the automatic default constructor

Page 11: ADTs and C++ Classes

Parameters

• Value parameters

• Reference parameters

• Constant reference parameters

Page 12: ADTs and C++ Classes

Inline Member Functions

• Placing a function definition inside a class definition is a called an inline member function

• You don’t have to write the implementation later

• Inline functions can cause some inefficiency, result in messier class definitions and should only be used when the function consists of a single short statement

Page 13: ADTs and C++ Classes

Header and Implementation Files

• A header file provides all the information that a programmer needs in order to use the class

• An implementation file that provides the actual implementations of the class’s member functions

Page 14: ADTs and C++ Classes

Compiler Directive

• A compiler directive in the header file, called a macro guard prevents duplicate class definitions

• #ifndef <class identifier> or (!defined) #define <class identifier>

<class definition> #endif

Page 15: ADTs and C++ Classes

Value Semantics of a Class• The assignment operator copies the values

for each member variable. Called the automatic assignment operator. Do not work under certain situations (discussed later on )

• A copy constructor is a constructor with exactly one argument that is the same type as the constructors class that creates a new object by coping an object of the same type default is automatic copy constructor

Page 16: ADTs and C++ Classes

The Constant Declaration

• A (usually) global definition of a constant that can not change while the program is running

• A more meaningful way to express a value used throughout a program

• Localizes the change of the constant to a single point in the program

Page 17: ADTs and C++ Classes

Classes and Parameters

• When functions communicate, they do so by using parameters. Understanding the use of parameters is essential for writing correct programs.

• Default arguments can be listed in the function prototypes of any function.

• A default constructor may be implemented with default arguments for all of its arguments.

Page 18: ADTs and C++ Classes

Parameters

• The function parameter is referred to as the formal parameter.

• The passed value is the argument, also called the actual parameter or the actual argument.

Page 19: ADTs and C++ Classes

Value Parameters

• A value parameter is declared by writing the type name followed by the parameter name. A local copy is made for use by the function. Data passed into a value returning function are always value parameters. Some values passed into a void function may be value parameters

Page 20: ADTs and C++ Classes

Reference Parameters

• A reference parameter is declared by writing the type name followed by the character & and the parameter name. Changes made to the formal parameter will change value of the actual parameter in the calling function. Use to pass data out of a function. [data may originate in the function or be changed from data passed into the function]

Page 21: ADTs and C++ Classes

Constant Reference Parameters

• A constant reference parameter is a reference parameters declared with the keyword const before the reference parameter. The compiler will not allow code to be written that would attempt to alter the value of the argument.

Page 22: ADTs and C++ Classes

Operator Overloading

• C++ lets you define the meaning of many operators for a new class

• This new definition is called overloading the operator

• The overloaded function has the name operator <operator symbol>e.g. operator == overloads the equivalence operator

Page 23: ADTs and C++ Classes

Overloading Input and Output Operators

• The return type is called a reference return type which permits “chaining” of output statements

• The function returns the same stream it has just written (read)

Page 24: ADTs and C++ Classes

Friend Functions

• A friend function is a function that is not a member function, but still has access to the private member variables

• The functions prototype is proceeded by the keyword friend

• Friendships should be limited to functions that are written by the programmer who implemented the class

Page 25: ADTs and C++ Classes

Summary

• Object-oriented programming supports information hiding by placing data in packages called objects.

• Objects are manipulated by member functions defined along with their classes.

Page 26: ADTs and C++ Classes

Summary

• Abstract Data Types (ADTs) are programmer defined data types. The term abstract emphasizes that the behavior of the ADT is what the user is concerned with.

• We create objects that exhibit the behavior of the problem we are trying to solve.