C++ -- 类的介绍和使用

01, C++中的 struct 和C语言的区别

a) C++中的struct 不仅可以声明成员变量, 还可以声明成员函数

struct Person
{
    string name;
    int age;
    int hight;

    void print()
    {
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
        cout << "hight: " << hight << endl;
    }
};

b) C++中声明一个 struct 对象

//C++中就不用再写成 struct Person t_man 了
Person t_man = { "Jack", 20, 180 };

在C语言中要写成 struct Person t_man; , 而在C++中省略了 struct

02, 声明类的关键字 class, struct 也可以声明类
  • 在类中声明的变量叫类的成员变量, 在类中声明的函数叫类的成员函数
    • 1, 类的成员函数可以访问类中的所有成员
    • 2, 在类外, 类的对象, 引用, 指针, 只能访问类的public成员
      这里写图片描述
  • 类的三个区间: public protected private

  • 类的作用域: 类定义的部分和类的成员函数定义的部分

  • 类内和类外: 类的作用域内/类的作用域外
  • 类中的内联函数, 必须声明定义在一个文件, 也可以直接在类中定义
  • 类中的函数重载
  • this 指针
    • this 指针, 存放类对象的首地址
      这里写图片描述
    • this 指针是隐藏在成员函数中的第一个形参

Student.h

class Student //声明一个学生类
{
    //类的3个区间

    //公有的
public:
    string name;
    int age;
    int hight; //这3个成员变量是属于public区的,是公有的


    string getName();//类的成员函数

    //类中的内联函数, 必须声明定义在一个文件, 也可以直接在类中定义
    inline void print()
    {
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
        cout << "hight: " << hight << endl;
    }

    //类中的函数重载, 默认参数必须写在函数定义处
    /*void setInfo(const string &t_name);*/
    void setInfo(const string & t_name = "Jack", const int t_age = 21, const int t_hight = 180);



    类的成员函数可以访问类的所有成员
    void test_fun1()
    {
        name;
        age;
        getName();
        getWeight();
        getSalary();
    }
    //受保护的
protected:
    int weight;
    char tel[12]; //这2个成员变量属于protected区,是受保护的
    int getWeight();

    //私有的
private:
    int salary; //这个成员变量属于private区,是私有的
    int getSalary();

};

Student.cpp

#include "Student.h"
#include <string>

using namespace std;

//this 指针, 存放类对象的首地址
//this 指针是隐藏在成员函数中的第一个形参
//     return this->name;
string Student::getName()
{
    return name;
}

//void Student::setInfo(const string & t_name)
//{
//  name = t_name;
//}

void Student::setInfo(const string & t_name, const int t_age, const int t_hight)
{
    name = t_name;
    age = t_age;
    hight = t_hight;
}

main.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
#include "Student.h"

using namespace std;

//1, C++中的struct 不仅可以声明成员变量, 还可以声明成员函数
struct Person
{
    string name;
    int age;
    int hight;

    void print()
    {
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
        cout << "hight: " << hight << endl;
    }
};



int main()
{
    //C++中就不用再写成 struct Person t_man 了
    Person t_man = { "Jack", 20, 180 };
    t_man.print();

    //声明一个类的对象
    Student t_student;
    t_student.setInfo("张三");
    cout << endl;
    t_student.print();

    t_student.setInfo("李四", 88);
    cout << endl;
    t_student.print();

    Student& t_student01 = t_student;//类的引用
    t_student01.name;
    t_student.getName();

    Student* p_student = &t_student;//类的指针
    p_student->age;
    p_student->getName();

    t_student.age;//在类外, 类的对象, 引用, 指针, 只能访问类的public成员
    //t_student.weight;//在类外, 无法访问类的protected成员
    //t_student.salary;//在类外, 无法访问类的private成员
    t_student.getName();

    cout << endl;
    cout << "t_student address: " << &t_student << endl;
    cout << endl;


    system("pause");
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值