essential c++ book note 3 chapter 4

本文深入讲解面向对象编程的核心概念,包括类定义、构造与析构、成员初始化、常量与可变成员、this指针、静态成员、友元及运算符重载等内容,通过实例演示如何构建迭代器类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/ggppwx/archive/2011/01/10/1931842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值