目录
1.构造函数(初始化列表)
我们在创建对象时,编译器会通过调用构造函数,给对象中的每个成员变量一个合适的初始值。
class Date_2 {
Date_2(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
在上述类中,我们调用构造函数后,对象已经有了一个初始值,所以此操作不能称为对对象成员初始,构造函数体中的语句只能将其称之为赋初值, 而不能称作初始化,因为初始化只能初始一次,而构造函数体内可多次进行赋值操作。
我们想要初始化类成员对象的话,只能使用初始化列表进行操作。
//初始化列表语法: 以冒号(:)开始, 以逗号分隔数据成员列表
//每个“成员变量”后面跟一个放在括号中的初始值或者表达式
class Date_2 {
Date_2(int year, int month, int day)
:_year(year),
_month(month),
_day(day)
{
}
private:
int _year;
int _month;
int _day;
};
注:每个成员变量在初始化列表中只能出现一次(初始化只能初始化一次)
类中(引用成员变量,const成员变量,自定义类型成员(且该类没有默认构造函数时))
class A {
public:
A(int a)
:_a(a)
{
}
private:
int _a;
};
class Date_2 {
Date_2(int year, int month, int day, int a, int ref)
:_year(year),
_month(month),
_day(day),
_a(a),
_n(10),
_ref(ref)
{
}
private:
int _year;
int _month;
int _day;
A _a;//无默认构造
const int _n;//const修饰
int& _ref;//引用
};
注:尽量使用初始化列表,因为无论你是否使用初始化列表,对于自定义类型的成员变量,一定会先使用初始化列表初始化。
成员变量在类中声明次序就是其在初始化列表中的顺序,与初始化列表中的顺序无关
2.explicit
构造函数不仅可以构造和初始化对象,对于接收单个参数的构造函数(构造函数只接受一个参数,构造函数有多个参数,出第一个参数无默认值外,其他参数均有默认值, 全缺省),还具有类型转换的作用。
class Date_3
{
public:
/*
单参数构造函数,不加explicit修饰,则具有类型转换作用
explicit修饰构造函数,具有禁止类型转化的作用
*/
explicit Date_3(int year)
:_year(year)
{}
/*
虽然有多个参数,但是创建对象时后两个参数可以不传递,没有使用explicit修饰,具有类型转
换作用
explicit修饰构造函数,禁止类型转换
explicit Date(int year, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
*/
Date_3& operator=(const Date_3& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
void Test()
{
/*
用一个整数变量给日期类赋值时,
编译器会先构造出一个无名对象,最后用无名对象给d1对象进行赋值
*/
Date_3 d1(2022);
//d1 = 2023;//编译失败,在构造函数中用explicit修饰
}
用explicit修饰构造函数,将会禁止构造函数的隐式类型转换。
3.static成员
声明为static的类成员称为类的静态成员,用static修饰的成员变量,称之为静态成员变量,用static修饰的成员函数,称之为静态成员函数,静态成员变量一定要在类外进行初始化
#include <iostream>
using namespace std;
class Solution {
public:
Solution() {
++_socont;
}
Solution(const Solution& s) {
++_socont;
}
~Solution() {
--_socont;
}
int Sum_Solution(int n) {
// 这里只返回 _socont 的值
n = _socont;
return n;
}
private:
static int _socont;
};
int Solution::_socont = 0;
int main() {
Solution s1;
Solution s2(s1);
cout << s1.Sum_Solution(0) << endl; // 输出 2
{
Solution s3;
cout << s3.Sum_Solution(0) << endl; // 输出 3
}
cout << s1.Sum_Solution(0) << endl; // 输出 2
return 0;
}
注:1.静态成员为所有类对象所共享, 不属于某个具体的对象,存放在静态区。
2.静态成员变量必须是类外定义,定义时不添加static关键字,类中只是声明
3.类静态成员即可用 类名::静态成员 或者 对象.静态成员来访问
4.静态成员函数没有隐藏的this指针,不能访问任何非静态成员
5.静态成员也是类的成员,受private,public,protected访问限定符的限制
4.友元
友元提供了一种突破封装的方式,有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多用。友元分为:友元函数和友元类
class Date_4
{
public:
Date_4(int year = 2024, int month = 8, int day = 14)
:_year(year),
_month(month),
_day(day)
{}
Date_4(const Date_4& date)
{
_year = date._year;
_month = date._month;
_day = date._day;
}
~Date_4()
{
std::cout << "~Date_4()";
}
/*
重载流插入(<<) 和 流提取(>>)
如果这样实现,调用的时候就会出现问题
Date d1;
d1 << cont; => d1.operator<<(&d1, cout); (不复合常规调用)
这是因为成员函数的第一个参数一定是隐藏的this,所以d1必须放在<<的左侧
*/
std::ostream& operator<<(std::ostream& _cout)
{
_cout << _year << "-" << _month << "-" << _day << std::endl;
return _cout;
}
private:
int _year;
int _month;
int _day;
};
此时,我们就可以使用友元函数来突破类域,从而实现流插入(<<)和流提取(>>)
class Date_4
{
public:
Date_4(int year = 2024, int month = 8, int day = 14)
:_year(year),
_month(month),
_day(day)
{}
Date_4(const Date_4& date)
{
_year = date._year;
_month = date._month;
_day = date._day;
}
~Date_4()
{
std::cout << "~Date_4()";
}
/*
重载流插入(<<) 和 流提取(>>)
如果这样实现,调用的时候就会出现问题
Date d1;
d1 << cont; => d1.operator<<(&d1, cout); (不复合常规调用)
这是因为成员函数的第一个参数一定是隐藏的this,所以d1必须放在<<的左侧
*/
friend std::ostream& operator<<(std::ostream& _cout, const Date_4& date);
friend std::istream& operator>>(std::istream& _cin, const Date_4& date);
private:
int _year;
int _month;
int _day;
};
std::ostream& operator<<(std::ostream& _cout, const Date_4& date)
{
_cout << date._year << "-" << date._month << "-" << date._day << std::endl;
return _cout;
}
std::istream& operator>>(std::istream& _cin, const Date_4& date)
{
_cin >> date._year >> date._month >> date._day;
return _cin;
}
注:友元函数可访问类的私有和保护成员,但不能访问类的成员函数
友元函数不能用const修饰
友元函数可以在类定义的任何地方声明,不受类访问限定符限制
一个函数可以是多个类的友元函数
友元函数的调用与普通函数的调用原理相同
友元类的所有成员函数都可以是另一类的友元函数,都可以访问另一个类中的非公有成员。
注:友元关系是单向的,不具有交换性。
友元关系不能传递
友元关系不能继承
class Time
{
/* 声明日期类为时间类的友元类,则在日期类中就直接访问Time类中的私有成 */
friend class Date_4;
public:
Time(int hour = 0, int minute = 0, int second = 0)
: _hour(hour)
, _minute(minute)
, _second(second)
{}
private:
int _hour;
int _minute;
int _second;
};
class Date_4
{
public:
Date_4(int year = 2024, int month = 8, int day = 14)
:_year(year),
_month(month),
_day(day)
{}
Date_4(const Date_4& date)
{
_year = date._year;
_month = date._month;
_day = date._day;
}
~Date_4()
{
std::cout << "~Date_4()";
}
friend std::ostream& operator<<(std::ostream& _cout, const Date_4& date);
friend std::istream& operator>>(std::istream& _cin, const Date_4& date);
private:
int _year;
int _month;
int _day;
};
std::ostream& operator<<(std::ostream& _cout, const Date_4& date)
{
_cout << date._year << "-" << date._month << "-" << date._day << std::endl;
return _cout;
}
std::istream& operator>>(std::istream& _cin, const Date_4& date)
{
_cin >> date._year >> date._month >> date._day;
return _cin;
}
5,内部类
一个类定义在另一个类的内部, 这个内部类就叫做内部类,内部类是一个独立的类, 它不属于外部类,更不能通过外部类的对象去访问内部类的成员。外部类对内部类没有任何的优越访问权限。
注:内部类就是外部类的友元类(内部类可以定义在外部类的public, protected, private都是可以的, 内部类可以直接访问外部类的static成员,不需要外部类的对象/类名, 使用sizeof计算外部类时, 和内部类没有任何关系)
class A
{
public:
A()
:_a(0)
{
}
/* B为A的内部类 */
class B
{
public:
B()
:_b(0)
{}
void printB()
{
std::cout << "printB()" << _b << std::endl;
}
private:
int _b;
};
private:
int _a;
};
int main()
{
/* 定义B */
A::B b;
b.printB();
return 0;
}
总结:类是对某一类实体(对象)来进行描述的,描述该对象具有哪些属性,哪些方法,描述完成后形成了一个新的自定义类型,才用该自定义类型就可以实例化具体的对象。