[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.

发布了51 篇原创文章 · 获赞 5 · 访问量 4209

猜你喜欢

转载自blog.csdn.net/qq_43519498/article/details/91461919