[C++复习]03|classes and objects

[note 1]

We can define a function outside the class definition inline by using inline in the header line of function definition.
[example]

class A
{
	......
public:
	void getNum(int a, int b);
}
inline void A::getNum(int a,int b)
{
	numa=a;
	numb=b;
}

[note 2]

#include <iostream>
using namespace std;
class A
{
protected:
    int a;
    int b;
public:
    A():a(1),b(2){};
    int getNum(int c) const
    {
        cout<<"b: "<<c<<endl;
        //a++;   //illegal
        return a;
    }
};

int main(int argc, const char * argv[]) {
    A A_a;
    cout<<A_a.getNum(3)<<endl;
    return 0;
}

output:

b: 3
1

[tips]:

  1. Only the member function of class can be defined as const member function.
  2. The common function can’t be defined as const member function.
  3. const member function can’t alter any data.

[note 3]

Constructor can’t be inherited.
Constructor and destructor can be defined as inline function.

[note 4]

an object with a constructor or destructor can’t be used as a member of union.

[note 5]

copy constructor

#include <iostream>
using namespace std;

class code
{
    int id;
public:
    code(){}                                //constructor
    code(int a){id=a;}                        //constructor again
    code(code & x){id=x.id;}                //copy constructor
    void display(void){cout<<id;}
};

int main()
{
    code A(100);                            //object A is created and initialized
    code B(A);                                //copy constructor called
    code C=A;                                //copy constructor called again
    cout << "\n id of A: ";A.display();
    cout << "\n id of B: ";B.display();
    cout << "\n id of C: ";C.display();
    cout<<"\n";
    return 0;
}

[note 6]

When no copy constructor is defined, the compiler supplies its own copy constructor, taking the form X::X(const x&){}
When there is data members of pointer data type, the default copy constructor will generate dangling pointer problems.[Eg 04_02_3_8 ]

[note 7]

[example of the order of constructor and destructor]

#include <iostream>
using namespace std;
//test constructor and destructor
class test
{
    int a;
public:
    test(int m){a=m;cout<<"constructor..."<<a<<endl;}
    ~test(){cout<<"destructor..."<<a<<endl;}
};

int main()
{
    test A1(1),A2(2),A(3);
}

output:

constructor...1
constructor...2
constructor...3
destructor...3
destructor...2
destructor...1

[note 8]

The data members are initialized in the order of declaration, independent of the order in the initialization list.

The following class members must adopt initialization list to do the initialization:

  • const members
  • reference members
  • class object members
  • call of derivation constructor to its base constructor

[note 9]

When new and malloc operators are used to assign memory space by the constructor, delete or free should be used to free this memory space in the destructor.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值