More Class Details
Const Object and Member Functions
To declare a constant object:
const cls_name obj_name (val1, val2);
Obj_name instantiated with val1 and val2
Const member functions used for functions that do not change the object
Ret_type cls_name :: fn_name() const { . . . }
Course Schedule
Const functions cannot call a non-const member function of the class
using the const instance
Const function can be overloaded with a non-const version
Const declaration not needed for constructors and deconstructors. C++
will use the standard constructor for instantiation of a const object.
A class can have objects of other classes as members
Invoke constructor from the header of the method.
example of holiday class that has a date object within it
Holiday::Holiday(): date (1,1), parkingEnforcement(false)
OR
Holiday::Holiday(int month, int day, bool theEnforcement):
date (month,day), parkingEnforcement(false)
See display 7.3 for complete code
class date{
private:
int month, day, year;
};
class Employee{
private:
char ssn[9];
char name[40];
date birthdate;
float pay_rate;
public:
Employee(); //constructor
int set_number(char *);
float calc_pay(float, float);
};
Separating Interface from Implementation
Put class definition in .h file
#ifndef rational.h
#define rational.h
class def
#endif
Put member definitions in rational.cpp
Main program in different .cpp
Scope
Data members and member functions belong to that class' scope.
Non member functions - file scope
Member functions have function scope IN a class
Scope Resolution Operator :: access a hidden variable
Member Functions and Data Members
Private data manipulated only by member functions
Public member functions for clients of the class to set or get private data members.
Interface to the User - public stuff
Inline Functions
Short functions placed inside of class definition
Usually just one line of code
MESSY
Static Members
- A "global" data member that you want to access from methods
- All methods can access and change ONE version of variable
- A Static method does not need a calling object
- Instead use class_name :: Static_method_name( )
Assignment Operator
- Assignment of objects permitted.
r = s; // assignment
- Copies members
- Need to write own to copy "other elements" -- lists
Copy Constructors
Looks similar to initialization
Rational r = s; // OR
Rational r(s);
Prototype
Rational (const Rational &);
Header
Rational :: Rational (const Rational &old)
Code simply consists of assignments
Initializers
Initializers can be used in addition to a constructor
Cls_name(param_list) : list_of_data_members(initial_cale) {}
Rational::Rational (int numer, int denomer) : NumertorValue(numer), DenominatorValue(denomer) { };
Pointers as Class Members
In some class
class Book;
private:
cahr *title;
public:
Book (char * = '0') ;
void showtitle(void);
Constructor
Book::Book(char *name){
title = new char[strlen(name)+1);
strcpy(title,name);
}
Assignment Operators & Copy Constructor
What if:
Book book1 ("DOS"), book2 ("history");
Book2 = book1?
Need own assignment operator
void Book::operator=(Book& old) {
if (old.title != NULL)
delete(title);
title=new char[strlen(old.title)+1];
strcpy(title, old.title); }
Deallocate delete [ ] arrayptr
Returning reference to private data member
Reference to object is an alias
C++ lets you return a reference to a private data member
Permits main to change a private member!!!!
NO NO NO
Additional Class Features
Static class members
Use keyword static
All objects share same memory
THIS
A pointer called this exists for every object
this is used to access data members and functions in a member operation
Use either *. notation or -> notation
For example, in class rational defined in class the
Add function can access the parameter's numerator and denominator
and also the "current" numerator and denominator
this-> NumeratorValue
This can also be used to cascade member function calls by returning *this
Call by obj.operation(param).operation(param)
See Rational Example
Destructors
Name of class preceded by a ~
~Rational
Called when an object is destroyed.
Receives no parameter and returns nothing
Used when memory dynamically allocated
When Constructors and Destructors Called
Called automatically
Deconstructor called when main terminates
Constructors called on only once for static variables
Deconstructors called for statics when exit called or main terminates