260 likes | 402 Vues
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
E N D
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 • Classes can support information hiding • Programs use classes without knowing how they are implemented
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
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)
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)
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]
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(…) {…};
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
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)
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
Parameters • Value parameters • Reference parameters • Constant reference parameters
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
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
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
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
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
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.
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.
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
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]
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.
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
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)
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
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.
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.