欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

类和对象【超详细】

时间:2023-06-05
预学先知

①面向对象和面向过程的初步认识
C语言是面向过程,关注的是过程,分析步骤,利用函数调用解决问题
C++是基于面向对象的,关注的是对象,将一件事拆成不同的对象,靠对象的交互完成
例子:
设计一个快递系统
面向过程:关注实现下单,信息整合,送单的过程,体现到代码–方法/函数
面向对象:关注实现类对象及类对象之间的关系,快递员,商家,用户之间的关系 --类的设计及类之间的关系

②C++是基于面向对象:面向过程和面向对象混编。
原因:C++兼容C
然而JAVA是纯面向对象:只有面向对象

③C++对结构体进行了升级
一方面struct 在C++中升级成了类
C++类跟结构体不同的是除了可以定义变量,还可以定义方法/函数

#include#include#include#includeusing namespace std;//struct/class//C++兼容C中结构体的用法//同时Struct在c++中升级成了类//C++在类中还可以定义方法/函数struct Student {//成员变量char _name[10];int _age;int _id;//成员方法void Init(const char* name, int age, int id) {//避免冲突,将前面改成_strcpy(_name, name);_age = age;_id = id;}void Print() {cout << _name << " " << _age << " " << _id << endl;}};int main() {struct Student s1 = {"xpc",18,7};//兼容CStudent s2 = { }; //升级成为类,Student是类名,也是类型s1.Print();s2.Init("zkx", 20, 11);s2.Print();return 0;}

类和对象 1.类的访问限定符

了解class 和 struct 定义的区别,class 里面成员变量默认是私有的,而struct默认是公有(public)

最好不要使用默认限定,自己定义private和public(访问限定符)
我们来定义第一个类~
封装是一种更好的管理模式
①数据和方法都封装到类里面
②可以给你访问定义成共有,不想给你访问定义成私有或者保护
不封装是一种自由管理

class Student {private://成员变量char _name[10];int _age;int _id;public://成员方法void Init(const char* name, int age, int id) {//避免冲突,将前面改成_strcpy(_name, name);_age = age;_id = id;}void Print() {cout << _name << " " << _age << " " << _id << endl;}};int main() {struct Student s1; //兼容CStudent s2; //升级成为类,Student是类名,也是类型return 0;}

注意,如果加了inline或者直接在类内定义(10几行内),编译器可能将这个方法当成内联函数处理

2.类的作用域

类定义了一个域

3.类大小的计算

只保存成员变量,成员函数放在公共代码区

为什么成员函数不放进来计算?
原因:每个对象中都有独立的成员变量,不同对象调用成员函数,调的是同一个,没有必要每个对象都保存一份

结论:计算类和类对象的大小,只看成员变量,考虑内存对齐,C++内存对齐规则跟C语言一致

另外,空类会给1byte,不存储有效数据,只是为了占位表示对象的存在
sizeof 类 算出的是实例化对象的大小,而不是类是多大

补充C语言的内存对齐规则

4.this指针

既然成员方法不保存在类的实例对象内,那这些不同的对象调用同样的方法为什么会取得他们各自的值??
那是因为省略了隐藏的this指针!

①调用成员函数时,不能显示传实参给this
②定义成员函数时,也不能显示声明形参this
③在成员函数内部,我们可以显示使用this

问题1:this是存在哪的?
一般情况下是存在栈(形参),有些编译器会放到寄存器中,如VS2019,ecx
问题2:

这道题选C
如果有人觉得是空指针错了,那不行,空指针是运行时错误,编译是检查不出来的

问题3:

选B

5.默认成员函数


1.构造函数

不然既有全缺省又有无参,语法上可以同时存在,但是如果同时存在,就会产生二义性

class Date {public://推荐使用全缺省或者半缺省,比较好用Date(int year = 2022, int month = 2, int day = 8) {_year = year;_month = month;_day = day;}private:int _year;int _month;int _day;};int main() {Date d1;Date d2(2022);Date d3(2022, 1);Date d4(2022, 1, 15);return 0;}

C++把类型分成两类:内置类型(基本类型),自定义类型
内置类型:int/char/double/指针/内置类型数组等
自定义类型:struct/class定义的类型

我们不写编译器默认生成构造函数,对于内置类型不做初始化处理
对于自定义类型成员变量会去调用它的默认构造函数(不用参数就可以调的)初始化
如果没有默认构造函数就会报错
任何一个类的默认构造函数就是–不用参数就可以调用
有三个,全缺省,无参,我们不写编译器默认生成的

总结:C++我们不写编译器默认生成构造函数,设计得不好,没有对内置类型和自定义类型统一处理
不处理内置类型的成员变量,只处理自定义类型成员变量

#include#include#include#includeusing namespace std;class A {public:A(int a = 0){cout << "a()" << endl;_a= a;}private:int _a = 0;};class Date {public:private:int _year;int _month;int _day;A _aa;};int main() {Date d1;return 0;}

构造函数本质用来初始化

构造函数的初始化列表
用: 和 , 分割
弄懂声明和定义:声明是一种承诺,没有实现,没有开空间,类型+变量属于是声明,定义是要开空间,要给值


C++建议在初始化列表内初始化
内置类型的成员,在函数体和在初始化列表都可以的,
自定义类型的成员,建议在初始化列表初始化,这样更高效

一道选择题

声明顺序就是初始化顺序
建议:初始化的顺序就是上面写的顺序,这样就不会出错

2.析构函数

构造的顺序一定是s1再s2 , 析构的顺序是相反的

class Stack {public:Stack(int capacity = 4) {_a = (int*)malloc(sizeof(int) * capacity);if (_a == nullptr) {cout << "malloc failn" << endl;exit(-1);}_top = 0;_capacity = capacity;}void push() {}~Stack() {free(_a);_a = nullptr;_top = _capacity = 0;}private:int* _a;size_t _top;size_t _capacity;};int main() {Stack s1 ;Stack s2(20);return 0;}


一个选择题

构造顺序是C A B D
C是全局变量,构造在main函数之前
D是静态变量,定义在局部,范围在局部,但是生命范围是全局

析构顺序是 B A D C
出了作用域,局部的变量先析构
D是局部的静态的比c先析构
局部变量是相反的,因为在栈中
全局变量是最后销毁的

范围相同的情况下,先定义的后析构,后定义的先析构

3.拷贝构造函数

为什么要传引用
调用拷贝构造,需要先传参数,传值传参又是一个拷贝构造


默认构造的拷贝函数可以对简单的数据类型进行拷贝

总结:拷贝构造不写生成的默认拷贝构造函数,对于内置类型和自定义类型都会拷贝处理。但是处理细节是不一样的,这个跟构造和析构是不一样的


传值传参会有一次拷贝构造
传值返回也会有一次拷贝构造

按理这个过程应该是三次,答案却只有两次
一次调用里面,连续构造函数,会被编译器优化,合二为一
直接把零时变量省略了,直接给值

必须要连续,接下来这种情况就不能优化

如果是匿名对象,也会优化,合二为一

6.运算符重载


定义在类的外面,private权限要打开

#includeusing namespace std;class Date {public:Date(int year = 0, int month = 1, int day =1) {_year = year;_month = month;_day = day;}//private:int _year;int _month;int _day;};//传引用消耗小,传const可以保护等bool operator>(const Date& d1, const Date& d2) {if (d1._year > d2._year) {return true;}else if (d1._year == d2._year && d1._month > d2._month) {return true;}else if(d1._year == d2._year && d1._month == d2._month&&d1._day>d2._day){return true;}else{return false;}}int main() {Date d1(2022, 2, 12);Date d2(2022, 2, 9);cout<<(d1 > d2)<

在类的内部

#includeusing namespace std;class Date {public:Date(int year = 0, int month = 1, int day =1) {_year = year;_month = month;_day = day;}//bool operator>(Date* const this,const Date& d) {bool operator>(const Date& d) {if (_year > d._year) {return true;}else if (_year == d._year && _month > d._month) {return true;}else if (_year == d._year && _month == d._month && _day > d._day) {return true;}else{return false;}}private:int _year;int _month;int _day;};int main() {Date d1(2022, 2, 12);Date d2(2022, 2, 9);cout<(d2)<



总结一下:默认生成这四个默认成员函数
构造和析构处理机制是基本类似的
拷贝构造和赋值重载处理机制是基本类似的

区分拷贝构造和赋值重载

7.实现日期类

头文件Date.h

#pragma once#includeusing namespace std;class Date {public:Date(int year = 0, int month = 1, int day = 1);void Print();int GetMonthday(int year, int month);bool operator>(const Date&);bool operator<(const Date&);bool operator>=(const Date&);bool operator<=(const Date&);bool operator==(const Date&);bool operator!=(const Date&);//计算过n天以后是哪一天//+=改变原来 +不改变Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);//前置++ //++d1Date& operator++();//d1++:后置++为了跟前置区分,增加占位Date operator++(int);//计算两个日期之间的天数int operator-(const Date & d);//计算某一天是星期几void PrintWeekDay();private:int _year;int _month;int _day;};

源文件Date.cpp

#include "Date.h"void Date::PrintWeekDay() {const char* arr[] = {"星期一","星期二" ,"星期三" ,"星期四" ,"星期五" ,"星期六" ,"星期日"};Date d1(1900, 1, 1);//1900年一月一是星期一int day =*this-d1;//膜完范围0~6 对应周一到周日cout << arr[day % 7] << endl;}int Date::operator-(const Date & d) {Date max = *this;Date min = d;int count = 0;int flag = 1;if (max < min) {max = d;min = *this;flag = -1;}while (min != max) {++min;++count;}return count * flag;}//所有的类的比较都可以使用 复用 这种方法bool Date::operator>(const Date& d) {if (_year > d._year) {return true;}else if (_year == d._year && _month > d._month){return true;}else if (_year == d._year && _month == d._month && _day > d._day) {return true;}elsereturn false;}// < 就是 >= 取反bool Date::operator<(const Date& d) {return !(*this >= d);}//复用操作bool Date::operator>=(const Date& d) {return *this > d || *this == d;}bool Date::operator<=(const Date& d) {return !(*this > d);}bool Date::operator==(const Date& d) {return _year == d._year&& _month == d._month&& _day == d._day;}bool Date::operator!=(const Date& d) {return !(*this == d);}//建议使用前置++//这里是前置++Date& Date::operator++() {*this += 1;return *this;} //这里是后置++Date Date::operator++(int) {Date ret(*this);*this += 1;return ret;}Date& Date::operator-=(int day) {if (day < 0) {return *this += -day;}_day -= day;while (_day<=0) {_month -= 1;if (_month == 0) {_year -= 1;_month = 12;}_day += GetMonthday(_year, _month);}return *this;//返回后this还存在,就可以使用引用}Date Date::operator-(int day) {Date ret(*this);ret -= day;return ret;}Date& Date::operator+=(int day) {_day += day;while (_day >= GetMonthday(_year, _month)) {_day -= GetMonthday(_year, _month);_month =_month+ 1;if (_month == 13) {_month = 1;++_year;}}return *this;}Date Date::operator+(int day) {Date ret(*this);//ret._day += day;ret += day;return ret;}void Date::Print() {cout << _year << "-" << _month << "-" << _day << endl;}int Date::GetMonthday(int year, int month) {int Month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)&&month==2) {Month[2] += 1;}return Month[month];}Date::Date(int year, int month, int day) {_year = year;_month = month;_day = day;//判断当前日期是否合法if (!((year >= 0) && (month > 0 && month < 13) && (day > 0 && day <= GetMonthday(year, month)))) {cout << "该日期不合法,错误日期是->";Print();}}

源文件Test.cpp

#include "Date.h"//查询日期是否合法void Test1() {Date d1(2022, 2, 21);Date d2(2022, 2, 41);d1.Print();d2.Print();}//计算n天过后是哪一天void test2() {Date d1(2022, 2, 21);d1 += 100;d1.Print();Date d2(2022, 2, 22);d2 + 100;d2.Print();//前置++和后置++Date d3 = d1++;//d1.operator(&d1,0)Date d4 = ++d2;//d2.operator(&d2)d3.Print();d4.Print();}void test3() {Date d1(2022, 1, 1);d1 += 10;d1.Print();Date d2 = d1 - 20;d2.Print();d2 -= 10;d2.Print();Date d3;d3 =d2 - -100;d3.Print();d3++;d3.Print();}void test4() {Date d1(2022, 2, 2);Date d2(2022, 1, 2);int day1 = d1 - d2;cout << day1 << endl;int day2 = d2 - d1;cout << day2 << endl;d1.PrintWeekDay();}int main() {//test2();test4();return 0;}

8.const 修饰的成员


C++解决的方法,在后面加一个const


总结:成员函数加const是好的,建议能加const都加上,这样普通对象和const对象就都可以调用了。
但如果要修改成员变量的成员函数是不能加的,比如日期类中的 += ++等等实现

9.友元


就像黄牛,能不用就不用

案例引入
因为不能实现cout<<对象,于是我们要完成对cin,cout的重载
但普通的重载方式不可行
如果是双操作数的运算符重载,第一个参数是操作数,第二个参数是右操作数

于是我们引入friend有元,放入类中加入friend



友元类

#includeusing namespace std;//你是我的朋友,你就可以在你的类中访问我的私有属性//但你是我的朋友,我不一定是你的朋友,和关注的概念是一样的class T {friend class Date;public:T(int hour = 0,int minute = 0, int second = 0):_hour(hour),_minute(minute),_second(second){}private:int _hour;int _minute;int _second;};class Date {public :Date(int year =1,int month =1 ,int day =1):_year(year),_month(month),_day(day){}void SetTimeDate(int hour, int minute, int second) {_time._hour = hour;_time._minute = minute;_time._second = second;}private:int _year;int _month;int _day;T _time;};

10.explict关键字

用了explicit 编不过,不用可以使用,是C++默认的一种“潜规则”

#includeusing namespace std;class Date {public:Date(int year):_year(year){cout << "Date(int year)" << endl;}Date(const Date& d) {cout << "Date(const Date& d) " << endl;}private:int _year;int _day;int _month;};int main() {//虽然他们两个都是直接构造,但是过程是不一样的Date a(2022);Date b = 2022;//隐式类型转换//本来用2022构造一个临时对象Date(2022),再用这个对象拷贝构造b//但是C++编译器在连续的以恶搞过程中,多个构造会被优化,合二为一//所以这里被优化为直接是一个构造//隐式类型转换 -相近类型 --表示意义相似的类型double d = 1.1;int i = d;const int& i2 = d;//强制类型转换 - 无关类型int* p = &i;int j = (int)p;return 0;}}

11.static 成员

可以叫静态成员变量,静态成员函数

#includeusing namespace std;class A {public:A(int a = 0):_a(a){++_scount;}A(const A& aa):_a(aa._a){++_scount;}//静态可以保护,静态不能滴哦用_astatic int GetCount() {return _scount;}private:int _a;static int _scount;};int A::_scount = 0;void f(A a){}int main() {A a;f(a);cout << a.GetCount() << endl;return 0;}


12.C++11

#includeusing namespace std;class B{public:B(int b = 0):_b(b){}private:int _b;};//C++ -- 打补丁class A {public :A() {}private://这里并不是初始化(是给具体某个空间初始化),因为这里式声明int _a1 = 0 ;//这里给成员变量省略值B _bb1= 10;B _bb2 = B(20);//static int _scount = 0;不能这样写,不能给缺省,缺省是给构造函数用的,构造函数不处理静态的成员//静态的不能给缺省,要在类外面全局初始化};int main() {A aa;return 0;}

13.内部类


算一算sizeof A 是多大
答案是8
B和A完全是一样的,只不过收到A的约束
B中可以访问A的私有,但是A不能访问B的私有

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。