C++学习笔记02-面向对象及类的引入

1 面向对象 与 面向过程

面向过程 : 关注的是解决问题的步骤。
面向对象 : 关注的是对象。将问题拆分不同对象,依靠参数完成对象之间的交互

2 类的引入

C语言中,用结构体把变量中组合在一起。类的本质就是结构体,只是在结构体上做了扩展。

结构体中只能组合变量

struct Studet
{
    char Name[20];
    int age;
}

结构体中不能定义函数,下面是错误的。

struct Studet
{
    char Name[20];
    int Age;
    
    void SetStudetInfo(const char *name, int age)
    {
        strcpy(Name, name);
        Age = age;
    }
};

int main(void)
{
    Studet s;
    
    s.SetStudetInfo("李三", 20);
    
    return 0;
}

3 类的定义

前面说了 类的本质 是结构体。把上面 struct 换成 class 就是类的了。
#include <cstring>

class Studet
{
public: //共有的
    char Name[20]; //属性
    int Age; //属性
    
    void SetStudetInfo(const char *name, int age) //方法
    {
        strcpy(Name, name);
        Age = age;
    }
};

int main(void)
{
    Studet s;
    
    s.SetStudetInfo("李三", 20);
    
    return 0;
}

类的定义

class className
{
    //成员函数
    //成员变量
};

类 :是 属性 和 方法的 结合。 类中全局变量就是 属性;函数就是 方法。
例如:上例,学生的姓名、年龄就是属性;学生的行为就是方法。

类的推荐定义方式
定义和声明分开。

class Studet
{
public: //共有的
    void SetStudetInfo(const char *name, int age) //方法

public: //共有的
    char Name[20]; //属性
    int Age; //属性
};

void Studet::SetStudetInfo(const char *name, int age) //方法
{
    strcpy(Name, name);
    Age = age;
}

4 类访问限定符号及封装

类的限定符号如下:

限定符号名词限定范围
public公共的可以被外界访问
private私有的不能被外界访问
protected私有的
class Studet
{
public: //共有的
    void SetStudetInfo(const char *name, int age) //方法

private: //私有的
    char Name[20]; //属性
    int Age; //属性
};

void Studet::SetStudetInfo(const char *name, int age) //方法
{
    strcpy(Name, name);
    Age = age;
}

面向对象的三大特征 封装、继承、多态

class 就是 属性 和 方法封装在一起了,这就是封装。

5 类的实例化

类房子的图纸,根据房子的属性朝向,颜色…可以造出很多房子,造出来的房子就是类的实例化。
类的定义并占用了内存空间就是类的实例化。

6 类对象的模型

类只是计算成员变量大小,不管成员函数。例如:

class A
{
public: //共有的
    void Print() //方法
    {
        std::cout << a << std::endl;
    }
private: //私有的
    char a; //属性
};

int main()
{
    A t;
    
    std::cout << sizeof(t) << std::endl;
}

结果:1

类实例化函数存储在公共区域。

7 this指针

先看个例子

#include <cstring>
#include <iostream> //头文件,C++11可以不用加.h、*hxx

using namespace std; //命名空间声明

class Studet
{
public: //共有的
    void SetStudetInfo(const char *name, int age); //方法
    void ShowStudetInfo(void);
    
private: //私有的
    char Name[20]; //属性
    int Age; //属性
};

void Studet::SetStudetInfo(const char *name, int age) //方法
{
    strcpy(Name, name);
    Age = age;
}

void Studet::ShowStudetInfo(void)
{
    std::cout << Name << " " << Age << std::endl; //连续输出 
}

int main(void)
{
    Studet s1, s2;
    
    s1.SetStudetInfo("李三", 20);
    s2.SetStudetInfo("王二", 21);
    
    s1.ShowStudetInfo();
    s2.ShowStudetInfo();
    
    return 0;
}

上例看上去感觉没啥问题。前面知道类的成员函数是在公共区的,编译器是如何知道是s1设置的,而不是s2呢?
若知道成员函数地址是不是可以直接调用了。所以就引入了 this 指针 来解决这个问题。

#include <cstring>
#include <iostream> //头文件,C++11可以不用加.h、*hxx

using namespace std; //命名空间声明

class Studet
{
public: //共有的
    void SetStudetInfo(Studet* this, const char *name, int age); //方法
    void ShowStudetInfo(Studet* this);
    
private: //私有的
    char Name[20]; //属性
    int Age; //属性
};

void Studet::SetStudetInfo(Studet* this, const char *name, int age) //方法
{
    strcpy(this->Name, name);
    this->Age = age;
}

void Studet::ShowStudetInfo(Studet* this)
{
    std::cout << this->Name << " " << this->Age << std::endl; //连续输出 
}

int main(void)
{
    Studet s1, s2;
    
    s1.SetStudetInfo(&s1, "李三", 20);
    s2.SetStudetInfo(&s2, "王二", 21);
    
    s1.ShowStudetInfo(&s1);
    s2.ShowStudetInfo(&s2);
    
    return 0;
}

编译器在实例化中已经生成了类的地址,进行传递。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值