面向过程是一种按照步骤和顺序执行的编程方式,适合简单问题的解决。
面向对象是一种对象与对象之间的交互关系,更加灵活,适合大型项目和复杂问题的解决。
class className
{
//类体:由成员函数和成员变量组成
}; //一定要注意后面的分号
类是面向对象编程中的一个重要概念,它是一种抽象的数据类型,用于描述具有相同属性和行为的对象的集合。类由属性和方法组成,属性表示对象的特征,而方法表示对象的行为。
class为定义类的关键字,className为类的名字,{}中为类的主体,注意类定义结束后面分号不能省略
类体中内容称为类的成员:类中的变量称为类的属性或成员变量;类中的函数称为类的方法胡或者成员函数
类将数据和操作封装在一起,通过访问控制来隐藏内部细节,只暴露必要的接口给外部使用
类可以通过继承机制派生出新的类,子类可以继承父类的属性和方法,并可以在此基础上添加新的属性和方法
多态是指同一操作在不同对象上具有不同的行为。在面向对象编程中,多态可以通过方法的重写和重载实现
C++实现封装的方式:用类将对象的属性与方法结合在一起,让对象更加完善,通过访问权限选择性的将其接口提供给外部的用户使用
注意:访问限定符只在编译时有用,当数据映射到内存后,没有任何访问限定符上的区别
将数据和操作数据的方法进行有机结合,隐藏对象的属性和实现细节,仅对外公开接口来和对象进行交互
类定义了一个新的作用域,类的所有成员在类的作用域中。在类体外定义车成员时,需要使用::作用域操作符指明成员属于哪个类域
class Person
{
public:
void PrintPersonInfo();
private:
char _name[20];
char _gender[3];
int _age;
};
// 这里需要指定PrintPersonInfo是属于Person这个类域
void Person::PrintPersonInfo()
{
cout << _name << " "<< _gender << " " << _age << endl;
}
用类类型创建对象的过程,称为类的实例化。
定义出一个类不会分配实际的内存空间
一个类可以实例化多个对象,实例化出的对象占用实际的物理空间,存储类成员变量
#include
using namespace std;
//类的定义
class Person
{
public:
const char* _name;
const char* _sex;
int _age;
};
//类的实例化+输出
int main()
{
Person man;
man._name = "jack";
man._age = 10;
man._sex = "男";
cout << man._name << endl;
cout << man._age << endl;
cout << man._sex << endl;
}
输出结果:
jack
10
男
一个类的大小,实际就是该类中的成员变量之和,当然要注意内存对齐
注意空类的大小,空类比较特殊,编译器给了空类一个字节来唯一标识这个类的对象
#include
using namespace std;
// 类中既有成员变量,又有成员函数
class A1 {
public:
void f1() {}
private:
int _a;
};
// 类中仅有成员函数
class A2 {
public:
void f2() {}
};
// 类中什么都没有---空类
class A3
{};
int main()
{
cout << sizeof(A1) << endl;
cout << sizeof(A2) << endl;
cout << sizeof(A3) << endl;
return 0;
}
输出结果:
4
1
1
结构体内存对齐规则
- 第一个成员在结构体偏移量为0的地址处
- 其他成员变量要对齐到某处数字(对齐数)的整数倍的地址处(注意:对其数=编译器默认的一个对齐数与该成员大小的较小值,VS默认对齐数为8)
- 结构体总大小为:最大对其数(所有变量类型最大者与默认对其数取最小)的整数倍
- 如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍
C++编译器给每个非静态的成员函数
增加了一个隐藏的指针参数,让该指针指向当前对象(函数运行时调用该函数的对象),在函数体中所有成员变量
的操作,都是通过该指针去访问,只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成。
类类型*const
,即成员函数中,不能给this指针赋值this指针本质上是成员函数的形参
,当对象调用成员函数时,将对象地址作为实参传递给this形参,所以对象中不存储this指针
this指针是成员函数第一个隐含的指针形参
,一般情况由编译器通过ecx寄存器自动传递,不需要用户传递构造函数
是一种特殊的成员函数
,名字与类名相同,创建类类型对象时由编译器自动调用
,以保证每个数据成员都有一个合适的初始化,并且在对象整个生命周期只调用一次
#include
using namespace std;
class Date
{
public:
//无参数的构造函数
Date()
{
}
//带参数的构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
//注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
Date d1;//调用无参的构造函数
Date d2(2019, 1, 1);//调用带参的构造函数
}
7.C++11中对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值
#include
using namespace std;
class Time
{
public:
Time()
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
void Printf()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
private:
// 基本类型(内置类型)
int _year = 1970;
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
int main()
{
Date d;
d.Printf();
return 0;
}
运行结果:
Time()
1970年1月1日
8.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意无参的构造函数,全缺省的构造函数,我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。
#include
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year <<"年" << _month <<"月" << _day <<"月" << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
d1.Print();
Date d2(2000, 2, 2);
d2.Print();
}
初始化列表:以一个冒号开始,接着是一个以逗号分隔的数据成员列表,每个成员变量后面跟一个放在括号中的初始值或表达式
#include
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
:_year(year)
,_month(month)
,_day(day)
{}
void Print()
{
cout << _year <<"年" << _month <<"月" << _day <<"日" << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(1000,1,1);
d1.Print();
}
每个成员变量在初始化列表中只能出现一次(初始化只能初始化一次)
类中包含以下成员,必须先放在初始化列表位置进行初始化:引用成员变量、const成员函数、自定义类型成员(且该类没有默认构造函数时)
class A
{
public:
A(int a)
:_a(a)
{}
private:
int _a;
};
class B
{
public:
B(int a, int ref)
:_aobj(a)
, _ref(ref)
, _n(10)
{}
private:
A _aobj; // 没有默认构造函数
int& _ref; // 引用
const int _n; // const
};
尽量使用初始化列表初始化,因为不管你是否使用初始化列表,对于自定义类型成员变量,一定会先使用初始化列表初始化
成员变量在类中声明次序就是其在初始化列表中的初始化顺序,与其在初始化列表中的先后次序无关
构造函数不仅可以构造与初始化对象,对于单个参数或者除第一个参数无默值其余均有默认的构造函数,还具有类型转换的作用
与构造函数功能相反,析构函数
不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作
用已经存在的类类型对象创建新对象时由编译器自动调用
拷贝构造函数是构造函数的一个重载形式
拷贝构造函数的参数只是一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用
#include
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1);
d1.Print();
d2.Print();
return 0;
}
//如果在拷贝构造函数中将对象按值进行传递,会导致无穷递归的问题。这是因为拷贝构造函数的参数是一个对象,如果将对象按值传递给拷贝构造函数,那么在调用拷贝构造函数时又会创建一个新的对象,这个新对象又会调用拷贝构造函数,如此无限递归下去。
//为了避免无穷递归的问题,应该将对象按引用传递给拷贝构造函数,即使用常量引用作为参数类型。这样可以避免创建新的对象,只是使用已有的对象进行拷贝。
**若未显式定义,编译器会生成默认的拷贝构造函数。**默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫浅拷贝,或者值拷贝
#include
using namespace std;
class Time
{
public:
//默认构造函数
Time()
{
_hour = 1;
_minute = 1;
_second = 1;
}
//默认拷贝构造
Time(const Time& t)
{
_hour = t._hour;
_minute = t._minute;
_second = t._second;
//cout << "Time::Time(const Time&)" << endl;
}
void Print()
{
cout << _hour <<":" << _minute <<":" << _second <<":" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
void Print()
{
cout << _year << ":" << _month << ":" << _day << ":" << ":";
_t.Print();
}
private:
// 基本类型(内置类型)
int _year = 1970;
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
int main()
{
Date d1;
// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
Date d2(d1);
d2.Print();
return 0;
}
在编译器生成的默认拷贝构造函数中,内置类型按照字节方式直接拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的。
类中如果没有涉及资源申请时,拷贝构造函数是否写都可以,一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就算是值拷贝。
拷贝构造函数典型调用场景:
使用已经存在对象创建新对象
函数参数类型为类类型对象
函数返回值类型为类类型对象
#include
using namespace std;
class Date
{
public:
Date(int year, int minute, int day)
{
cout << "Date(int,int,int):" << this << endl;
}
Date(const Date& d)//使用已经存在对象创建新对象
{
cout << "Date(const Date& d):" << this << endl;
}
~Date()
{
cout << "~Date():" << this << endl;
}
private:
int _year;
int _month;
int _day;
};
Date Test(Date d)//函数参数类型为类类型对象\函数返回值类型为类类型对象
{
Date temp(d);
return temp;
}
int main()
{
Date d1(2022, 1, 13);
Test(d1);
return 0;
}
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似
函数名为:关键字operator后面接需要重载的运算符符号
函数原型:返回值类型 operator操作数(参数列表)
【注意事项】
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作数必须有一个类类型参数
- 用于内置类型的运算时,其含义不能改变,例如:内置的整型+,不能改变其含义
- 作为类成员函数重载时,其形参看起来比操作数目少1,因为成员函数的第一个参数为隐藏的this
.* :: sizeof ?: .
注意以上5个运算符不能重载。
#include
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//bool operator==(Date* this,const Date& d2)
bool operator==(const Date& d2)
{
return _year == d2._year && _month == d2._month && _day == d2._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2000, 1, 2);
Date d2(2000, 1, 2);
cout << (d1 == d2) << endl;
}
1.赋值运算符重载格式
#include
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2000, 2, 2);
Date d2;
Date d3;
d2 = d1;
d3 = d2 = d1;
d1.Print();
d2.Print();
d3.Print();
return 0;
}
2.赋值运算符只能重载成类的成员函数不能重载成全局函数(赋值运算符如果不显示实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重符只能是类的成员函数)
3.用户没有显示实现时,编译器会生成一个默认重载赋值运算符函数,以值的方法逐字节拷贝。(注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值)
#include
using namespace std;
class Time
{
public:
Time()
{
_hour = 1;
_minute = 1;
_second = 1;
}
Time& operator=(const Time& t)
{
if (this != &t)
{
_hour = t._hour;
_minute = t._minute;
_second = t._second;
}
return *this;
}
void Print()
{
cout << _hour << "小时" << _minute << "分钟" << _second << "秒" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日" <<" ";
_t.Print();
}
private:
//内置类型
int _year=1900;
int _month=1;
int _day=1;
//自定义类型
Time _t;
};
int main()
{
Date d1;
Date d2;
d1 = d2;
d1.Print();
d2.Print();
return 0;
}
#include
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//前置++
Date& operator++()
{
_day += 1;
return *this;
}
//后置++
Date operator++(int)//temp是临时对象,因此只能以值的方式返回,不能返回引用
{
Date temp(*this);
_day += 1;
return temp;
}
void Print()
{
cout << _year <<" " << _month <<" " << _day <<" " << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d;
Date d1(2022, 2, 2);
d = d1++;
d.Print(); //2022 2 2
d1.Print(); //2022 2 3
d = ++d1;
d.Print(); //2022 2 4
d1.Print(); //2022 2 4
return 0;
}
这两个默认成员函数一般不用重新定义,编译器默认会生成
#include
using namespace std;
class Date
{
public:
Date* operator&()
{
return this;
}
const Date* operator&() const
{
return this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date* p1 = &d1;
const Date* p2 = &d1;
cout <<"p1:" << p1 << endl;
cout <<"p2:" << p2 << endl;
return 0;
}
声明为static的类成员
称为类的静态成员
,用static
修饰的成员变量
,称之为静态成员变量
,用static
修饰的成员函数
,称之为静态成员函数
,静态成员变量一定要在类外进行初始化
静态成员
为所有类对象所共享,不属于某具体的对象,存放在静态区静态成员变量
必须在类外定义,定义时不添加static关键字,类中只是声明类名::静态成员
或者 对象.静态成员
来访问静态成员函数
没有隐藏的this指针,不能访问任何非静态成员友元提供了一种突破封装的方法,有时提供了便利,但是友元会增加耦合度,破坏了封装,所以友元不宜多用(友元分为友元函数和友元类)
友元函数可以直接访问类的私有成员,它是定义在类外部的普通函数,不属于任何类,但需要在类的内部声明,声明时需要加friend关键字
#include
using namespace std;
class Date
{
friend ostream& operator<<(ostream& _const, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year=1900, int month=1, int day=1)
:_year(year)
, _month(month)
, _day(day)
{
}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin >> d._year;
_cin >> d._month;
_cin >> d._day;
return _cin;
}
int main()
{
Date d;
cin >> d;
cout << d << endl;
return 0;
}
说明:
- 友元函数可以访问类的私有和保护成员,但不是类的成员函数
- 友元函数不能用const修饰
- 友元函数可以在类定义的任何地方声明,不受访问限定符限制
- 一个函数可以是多个类的友元函数
- 友元函数的调用与普通函数的调用原理相同
友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非共有成员
#include
using namespace std;
class Time
{
//声明日期类为时间类的友元类,则在日期类中就直接访问Time类中的私有成员变量
friend class Date;
public:
Time(int hour = 1, int minute = 1, int second = 1)
:_hour(hour)
,_minute(minute)
,_second(second)
{}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{}
void SetTimeOfDate(int hour, int minute, int second)
{
//直接访问时间类私有的成员变量
cout<<(_t._hour = hour)<<" ";
cout << (_t._minute = minute) << " ";
cout << (_t._second = second) <<" ";
}
private:
int _year;
int _month;
int _day;
Time _t;
};
int main()
{
Date d1;
d1.SetTimeOfDate(1000,1,1);
}
如果一个类定义在另一个类的内部,这个内部类就叫做内部类。内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员,外部类对内部类没有任何优越的访问权限.
内部类是外部类的友元类,内部类可以通过外部类的对象参数来访问外部类中的所有成员。但是外部类不是内部类的友元
#include
using namespace std;
class A
{
private:
static int k;
int h=11;
public:
class B //B天生就是A的友元
{
public:
void foo(const A& a)
{
cout << k << endl;
cout << a.h << endl;
}
};
};
int A::k = 1;
int main()
{
A::B b;
b.foo(A());
return 0;
}
如果一个类定义在另一个类的内部,这个内部类就叫做内部类。内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员,外部类对内部类没有任何优越的访问权限.
内部类是外部类的友元类,内部类可以通过外部类的对象参数来访问外部类中的所有成员。但是外部类不是内部类的友元
#include
using namespace std;
class A
{
private:
static int k;
int h=11;
public:
class B //B天生就是A的友元
{
public:
void foo(const A& a)
{
cout << k << endl;
cout << a.h << endl;
}
};
};
int A::k = 1;
int main()
{
A::B b;
b.foo(A());
return 0;
}