公司内网站建设,商会小程序开发一个多少钱啊,seo网站自动推广,软件开发培训机构哪个好1.面向过程和面向对象初步认识
C语言是面向过程的#xff0c;关注的是过程#xff0c;分析出求解问题的步骤#xff0c;通过函数调用逐步解决问题。 C是基于面向对象的#xff0c;关注的是对象#xff0c;将一件事情拆分成不同的对象#xff0c;靠对象之间的交互完 成 …1.面向过程和面向对象初步认识
C语言是面向过程的关注的是过程分析出求解问题的步骤通过函数调用逐步解决问题。 C是基于面向对象的关注的是对象将一件事情拆分成不同的对象靠对象之间的交互完 成
2.类的引入
C语言结构体中只能定义变量在C中结构体内不仅可以定义变量也可以定义函数。比如 之前在数据结构初阶中用C语言方式实现的栈结构体中只能定义变量现在以C方式实现 会发现struct中也可以定义函数。
#include iostreamusing namespace std;struct stack
{void init(int capcity){_a (int*)malloc(sizeof(int) * capcity);if (_a nullptr){perror(malloc fail);return;}_capacity capcity;_top 0;}void Push(const intdata){// 扩容_a[_top] data;_top;}int Top(){return _a[_top- 1];}void Destroy(){if (_a){free(_a);_a nullptr;_capacity 0;_top 0;}}int* _a;int _top; // 栈顶int _capacity;};int main()
{stack st;st.init(4);st.Push(1);st.Push(2);st.Push(3);int ret st.Top();st.Destroy();}上面结构体的定义在C中更喜欢用class来代替。
3.类的声明与定义
1.上面的那种方法定义和声明都在类里面 2.定义和声明分开 stack.cpp
#include stdlib.h#includestack.hvoid stack:: init(int capcity){_a (int*)malloc(sizeof(int) * capcity);if (_a nullptr){perror(malloc fail);return;}_capacity capcity;_top 0;}void stack::Push(const int data){// 扩容_a[_top] data;_top;}int stack::Top(){return _a[_top - 1];}void stack::Destroy(){if (_a){free(_a);_a nullptr;_capacity 0;_top 0;}}
stack.h
#pragma once
struct stack
{void init(int capcity);void Push(const int data);int Top();void Destroy();int* _a;int _top; int _capacity;};源.cpp
#include iostream
#include stack.h
using namespace std;int main()
{stack st;st.init(4);st.Push(1);st.Push(2);st.Push(3);int ret st.Top();st.Destroy();
}注意类声明放在.h文件中成员函数定义放在.cpp文件中注意成员函数名前需要加类名:: 4.类的访问限定符及封装 【访问限定符说明】
public修饰的成员在类外可以直接被访问protected和private修饰的成员在类外不能直接被访问(此处protected和private是类似的)访问权限作用域从该访问限定符出现的位置开始直到下一个访问限定符出现时为止如果后面没有访问限定符作用域就到 } 即类结束。class的默认访问权限为privatestruct为public(因为struct要兼容C) 【面试题】 解答C需要兼容C语言所以C中struct可以当成结构体使用。另外C中struct还可以用来 定义类。和class定义类是一样的区别是struct定义的类默认访问权限是publicclass定义的类 默认访问权限是private。
5.类的作用域
类定义了一个新的作用域类的所有成员都在类的作用域中。在类体外定义成员时需要使用 :: 作用域操作符指明成员属于哪个类域。
struct stack
{void init(int capcity);void Push(const int data);int Top();void Destroy();int* _a;int _top;int _capacity;};void stack:: init(int capcity){_a (int*)malloc(sizeof(int) * capcity);if (_a nullptr){perror(malloc fail);return;}_capacity capcity;_top 0;}这里的init属于stack这个类域
6.类的实例化
1.用类类型创建对象的过程称为类的实例化 2. 一个类可以实例化出多个对象实例化出的对象 占用实际的物理空间存储类成员变量 3.做个比方。类实例化出对象就像现实中使用建筑设计图建造出房子类就像是设计图只设 计出需要什么东西但是并没有实体的建筑存在同样类也只是一个设计实例化出的对象 才能实际存储数据占用物理空间
struct stack
{void init(int capcity);void Push(const int data);int Top();void Destroy();int* _a;int _top; int _capacity;
};
int main()
{stack st;}st就是类的实体化。
7.如何计算类对象的大小
/ 类中既有成员变量又有成员函数
class A1 {
public:void f1(){}
private:int _a;
};
// 类中仅有成员函数
class A2 {
public:void f2() {}
};
// 类中什么都没有---空类
class A3
{};
sizeof(A1) : 4__ sizeof(A2) : 1__ sizeof(A3) : 1__
#include iostreamusing namespace std;
class A1 {
public:void f1() {}
private:int _a;
};
// 类中仅有成员函数
class A2 {
public:void f2() {}
};
// 类中什么都没有---空类
class A3
{};
int main()
{printf(%d , sizeof(A1));printf(%d , sizeof(A2));printf(%d , sizeof(A3));} 结论一个类的大小实际就是该类中”成员变量”之和当然要注意内存对齐注意空类的大小空类比较特殊编译器给了空类一个字节来唯一标识这个类的对象。
8.this指针的引出
#include iostreamusing namespace std;class date
{ public:void init(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;date d2;d1.init(2023, 3, 11);d2.init(2023, 1, 12);d1.print();d2.print();}Date类中有 Init 与 Print 两个成员函数函数体中没有关于不同对象的区分那当d1调用 Init 函 数时该函数是如何知道应该设置d1对象而不是设置d2对象呢 C中通过引入this指针解决该问题即C编译器给每个“非静态的成员函数“增加了一个隐藏 的指针参数让该指针指向当前对象(函数运行时调用该函数的对象)在函数体中所有“成员变量” 的操作都是通过该指针去访问。只不过所有的操作对用户是透明的即用户不需要来传递编 译器自动完成。 【面试题】
this指针存在哪里this指针可以为空吗
答this指针存在栈区中类中的函数存在代码段中.
#include iostreamusing namespace std;class date
{ public:void init(int year, int month, int day){_year year;_month month;_day day;}void print(){cout _year - _month - _day endl;}void func(){cout func() endl;}
private:int _year;int _month;int _day;};
int main()
{date d1;date d2;d1.init(2023, 3, 11);d2.init(2023, 1, 12);/*d1.print();d2.print();*/date* ptr nullptr;ptr-func();//可以为空吗//正常运行//在代码段中找ptr-init(2001, 1, 1);//可以为空吗//运行崩溃(*ptr).func();//可以为空吗//正常运行//原理和第一个一样}第一个空指针没有解引用 第二个空指针解引用导致崩溃