1.C++中类的数据成员
- 1st way: 在类中进行初始化
// 它们是常量,所以一旦初始化就不能更改
#include <iostream>
using namespace std;
class Circle
{
const float PI = 3.1415926; // 1st way: 在类中进行初始化
float r;
public:
Circle(float a){ r = a;}
float getArea() { return r * r * PI;}
};
int main()
{
Circle c1(5.2);
cout << c1.getArea() << endl;
return 0;
}
- 2st way:从外部使用初始化列表初始化它们
#include <iostream>
#include <string>
using namespace std;
class Phone
{
private:
const string pname;
public:
Phone(string str):pname{str}{} // 从外部使用初始化列表初始化它们
string getPhoneName() {return pname;}
};
int main()
{
Phone p1("MOTO G4"), p2("Iphone X");
cout << p1.getPhoneName()<<endl;
cout << p2.getPhoneName()<<endl;
return 0;
}
2.C++ 中的构造函数和成员初始化列表
- 使用 {} ,这是统一初始化,应该是首选
- 使用()
//用来初始化类的数据成员
#include <iostream>
using namespace std;
class Base
{
private:
int x;
public:
Base(int a):x{a}{} // or Base(int a):x(a){}
void print(){ cout << x << endl;}
};
int main()
{
Base b(10);
b.print();
return 0;
}
但是初始化列表不会隐式转化,例如
#include <iostream>
using namespace std;
class Base
{
private:
char x;
public:
// Base(int a):x{a}{} // error
Base(int a):x(a){} // OK
void print(){ cout << x << endl;}
};
int main()
{
Base b(10);
b.print();
return 0;
}
3.C++中必须使用初始化列表的地方
#include <iostream>
using namespace std;
class Base
{
private:
const int _x; //1. 必须使用初始化列表进行初始化
int& _y; //2. 必须使用初始化列表进行初始化
public:
Base(int y, int x=0):_y{y}, _x{x}{}
void print(){cout << _x << "," << _y << endl;}
};
int main()
{
Base b(10);
b.print();
Base b1(12);
b1.print();
return 0;
}
#include <iostream>
using namespace std;
class One
{
int _x;
public:
One(int x):_x{x}{}
};
class Two
{
One a;
public:
Two(One x): a{x}{} //3. 必须使用初始化列表进行初始化
// Two(One x): {a=x;} // error
};
int main()
{
One one(10);
Two two(one);
return 0;
}
#include <iostream>
using namespace std;
class Base
{
int _x;
public:
Base(int x):_x{x}{}
};
class Child:public Base
{
int _y;
public:
Child(int x, int y): Base{x}, _y{y}{} //4. Base必须使用初始化列表进行初始化
};
int main()
{
Child c(1, 2);
return 0;
}
下面两种情况都可以,但是调用不同的构造函数
#include <iostream>
using namespace std;
class Base
{
int _x;
public:
Base(){cout<<"base default"<<endl;}
Base(int x):_x{x}{cout<<"base parameter constructor"<<endl;}
Base(const Base& obj){this->_x=obj._x; cout<<"base copy constructor"<<endl;}
Base& operator=(const Base& obj){cout<<"base assignment operator"<<endl;return *this;}
};
class MyClass
{
Base _b;
public:
MyClass(){cout<<"myclass default"<<endl;}
MyClass(Base b): _b{b}{cout<<"myclass parameter"<<endl;} //4. Base必须使用初始化列表进行初始化
};
int main()
{
Base b(10);
MyClass mc(b);
return 0;
}
/*
base parameter constructor
base copy constructor
base copy constructor
myclass parameter
*/
#include <iostream>
using namespace std;
class Base
{
int _x;
public:
Base(){cout<<"base default"<<endl;}
Base(int x):_x{x}{cout<<"base parameter constructor"<<endl;}
Base(const Base& obj){this->_x=obj._x; cout<<"base copy constructor"<<endl;}
Base& operator=(const Base& obj){cout<<"base assignment operator"<<endl;return *this;}
};
class MyClass
{
Base _b;
public:
MyClass(){cout<<"myclass default"<<endl;}
MyClass(Base b): {_b=b;cout<<"myclass parameter"<<endl;} //4. Base必须使用初始化列表进行初始化
};
int main()
{
Base b(10);
MyClass mc(b);
return 0;
}
/*
base parameter constructor
base copy constructor
base default
base assignment operator
myclass parameter
*/
4.在 C 和 C++ 中编译是如何在内部工作的
------------------------------------------Diagram--------------------------------------------
1.cpp ----> Compiler ---> 1.o --*
[Source Code] ----> Compiler ---> [Object Code] --*
|
[Source Code] ----> Compiler ---> [Object Code] --*-->Linker --> [Exexutable] --> Loader
| |
[Source Code] ----> Compiler ---> [Object Code] --* |
| V
[Library file]--* [Running Executable in Memory]
预处理preprocessing----->编译和汇编compilation & assembly------>链接linking
完成链接工作之后,链接器根据编译目的不同,把链接的结果生成为一个动态链接库,或是一个可执行文件。
---------------------------------------------------------------------------------------------