More Class Details

Const Object and Member Functions
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

Scope

Member Functions and Data Members

Inline Functions

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