essential c++ book note 3 chapter 4

chapter 4 object-based programming 

1,define a class 

   all members functions must be declared within the class definition. if defined within a body of the class, the member function is automatically treated as being inline

  the class definition and the inline member functions are typically placed in a header file. the none inline member functions are defined within a program text file.

 

2, constructor and destructor 

 

class Triangular{
public:
   //overload set
   Triangular();
   Triangular(int len = 1, int bp =1 );
   //destructor
   ~Triangular();
}

//the member initialization list
Triangular::Triangular(const Triangular &rhs)
   : _length(rhs._length)
{}  //yes it is empty

 

the destructor class cannot be overloaded ..

 

3, memberwise initialization 

provide deep copy. when we design a class, we must consider if the default memberwise behavior is adequate for the class 

 

4, mutable and const 

 

int sum(const a ){ // .......
}

which means the compiler should guarantee that the a is not modified with the sum

 

 

 

class Triangular{
public:
  int length() const {return _length; }
  int elem(int pos) const;
}
int Triangular::elem (int pos ) const 

a const member function defined outside the class body should specify the "const " in both its declaration and definition 

 

const member function: didn't modify the class object .

provide 2 versions of member function. 

each const reference class parameter will be unable to invoke the non-const portion of the class interface 

 

class val_class{
public: 
   const bigclass& val() const {return _val;}
   bigclass& val() {return _val;}
   
};

void example(const bigclass *pvc, bigclass &rbc)
{
    pbc -> val();   // invoke the const instance 
    rbc.val();  // invokes non-const instance
}

 

mutable: by identifying a mutable variable, we are saying that the change to it doesn't violate the contness of class object !!!

 

5, this pointer

this pointer provides an access to the class object through which the member function is invoked. 

 

6, static class members 

a static data member represents a single, shared instance of that member that is accessible to all the objects of that class. 

class Triangular{
public:
   static bool is_elem(int); 
private:
   static vector<int> _elems ;   //
   static const int _size = 1234;
}
//the definition looks like the global definition of an object except that its name is qualified with //the class scope operator:
vector<int> Triangular::_elems;
//when defined outside the class body, the static keyword is not repeated
bool Triangular:: is_elem(int val)
{ //.............
}

//if the is_elem(), doesn't access any nonstatic data members, its operation is independent of any particular class, it's convenient to invoke it
//free standing function
int main()
{
   if(Triangular::is_elem(8)){}
}

7, friendship 

when to use friend ship: friendship is generally required for performance reason(multiplication of pointer), for a simple read or write of data member, an inline public is adequate.

class Triangular{
//confers friendship on all the member of Triangular_iterator
   friend class Triangular_iterator;
}
//which means we can use
Triangular::Triangular_iterator it;

8, operator 

building an iterator class 

class Triangular_iterator
{
public:
   Triangular_iterator(int index) : _index(index){}
   bool operator==(const Triangular_iterator& ) const;
   bool operator!= (const Triangular_iterator& ) const;
   int operator*() const;
   int& operator++ ();   //prefix
   int operator++(int);  //postfix
private:
   _index;
}

inline int& Triangular_iterator::
operator++()
{
  ++_index;
  check()
  return Triangular::_elems[_index];
}

inline int Triangular_iterator::
operator++(int)
{
// ordinarily, the postfix instance would be also defined with an empty parameter list, however, 
//each overloaded operator must have an unique parameter list.
}

9. function object

posted @ 2011-01-10 12:33  love && peace  阅读(246)  评论(0编辑  收藏  举报