(二)C++学习笔记(满满的都是水货咕噜!)

1、引用

变量名是用来表示内存地址的别名,一般定义变量时就会开辟内存空间
引用就是给当前地址空间重新再定义一个别名。相当于两个变量名的地址是同一个。

#include <iostream>

using namespace std;

void test1()
{
    int a = 100;
    cout << "a = " << a << endl;
    cout << "&a = " << &a << endl;

    //定义一个引用
    //引用就是又给当前空间起了一个别名,所以b的值和地址跟a完全一样
    int &b = a;
    cout << "b = " << b << endl;
    cout << "&b = " << &b << endl;

    b = 200;
    cout << "a = " << a << endl;

    //注意:引用必须初始化,否则报错
    //int &c;

    //注意:如果引用保存常量,则需要使用const修饰
    const int &c = 666;
    cout << "c = " << c << endl;

    //c = 555;
    //cout << "c = " << c << endl;

}

void MyChange1(int a)
{
    a = 888;
}

void MyChange2(int *p)
{
    *p = 888;
}

//引用在C++中主要就是用于函数传参
//既减少了指针的使用,有不会再多开辟空间,所以非常好用
void MyChange3(int &b)
{
    cout << "&b = " << &b << endl;
    b = 888;
}

void test2()
{
    int a = 100;
    cout << "a = " << a << endl;
    cout << "&a = " << &a << endl;

    MyChange3(a);
    cout << "a = " << a << endl;
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

执行结果:
在这里插入图片描述

2、C++堆区开辟空间 – new、delete

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

//C语言在堆区开辟空间
void test1()
{
    //C语言中在堆区开辟空间一般使用malloc
    char *s = (char *)malloc(32);
    strcpy(s, "hello world");

    cout << "s = " << s << endl;

    //释放空间
    free(s);
    s = NULL;
}

//C++在堆区开辟空间
void test2()
{
    //C++在堆区开辟空间使用new
    //int *a = new int;
    //*a = 100;

    //开辟空间并赋初值使用()
    int *a = new int(888);
    cout << "*a = " << *a << endl;

    //释放一块空间
    delete a;

    //开辟多个同类型的空间使用[]
    char *s = new char[32];
    strcpy(s, "hello world");
    cout << "s = " << s << endl;

    //释放多块空间
    delete []s;
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

3、类和对象

1 – 类的构成

对于研究的一个事物,可以称之为数据或者称之为一个对象,研究一个对象时,主要分为属性和行为,数据就相当于成员变量,行为就是函数,每一个对象应该都是由这两部分组成,并且对于每一个对象都有公有的属性和行为以及私有的属性和行为,所以对于对象还要设置访问权限

2 – 类的定义以及实例化对象

所以类具有封装性即可保存变量,又可以保存函数,并且具有访问权限
定义类:
class 类名{
成员变量或者成员函数;
};

实例化对象就是定义类的变量:
类名 对象名;

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

using namespace std;

//定义一个类
class Person
{
    //类中成员的访问权限:public、private、protected
    //public:公有的权限,类外可以直接访问,struct默认就是公有权限
    //private:私有的权限,类内可以随便访问,但是类外不能直接访问,类的默认权限是私有权限
    //protected:受保护的权限,主要涉及到继承的时候使用,不涉及继承跟private一样
public:
    int p_ID;
    char p_Name[32];
    char p_Sex;
    int p_Age;
};

void test1()
{
    //定义一个类的变量,称之为对象
    //定义变量称之为实例化对象
    Person p;
    p.p_ID = 1001;
    strcpy(p.p_Name, "zhangsan");
    p.p_Sex = 'B';
    p.p_Age = 20;

    cout << p.p_ID << " " << p.p_Name << " " << p.p_Sex << " " << p.p_Age << endl;
}

int main(int argc, char *argv[])
{
    test1();

    return 0;
}

3 – 私有成员的访问方式

(1)使用公有的成员函数。
(2)友元函数
先讲公有的成员函数

#include <iostream>
#include <string>

using namespace std;

class Person{
public:
    int p_ID;
    string p_Name;

    //私有成员通过公有的成员函数对其进行操作并在外部访问
    void setMsg(int age, string eid)
    {
        p_Age = age;
        p_Eid = eid;
    }

    void getMsg()
    {
        cout << p_ID << "-" << p_Name << "-" << p_Age << "-" << p_Eid << endl;
    }

private:
    int p_Age;
    string p_Eid;
};

void test1()
{
    Person p1;
    p1.p_ID = 1001;
    p1.p_Name = "zhangsan";
    //私有成员无法在类外直接访问
    //p1.p_Age = 20;
    //p1.p_Eid = "19990901";
    //cout << p1.p_ID << "-" << p1.p_Name << "-" << p1.p_Age << "-" << p1.p_Eid << endl;

    p1.setMsg(20, "19990901");
    p1.getMsg();
}

int main(int argc, char *argv[])
{
    test1();

    return 0;
}

4 – 类所占空间问题

#include <iostream>

using namespace std;

//类所占内存空间与结构体一样
class c1{
    int a;
    char b;
};

class c2{
    int a;
    char b;
    int c;
};

//访问权限和成员函数不占类的空间
class c3{
public:
    int a;

    void myfun()
    {
        cout << "hello world" << endl;
    }

private:
    char b;
    int c;
};

//空的类或者结构体占一个字节得空间,如果非空,则这一个字节不再算进去
class c4{

};

struct stu{

};

int main(int argc, char *argv[])
{
    cout << "sizeof(c1) = " << sizeof(c1) << endl;
    cout << "sizeof(c2) = " << sizeof(c2) << endl;
    cout << "sizeof(c3) = " << sizeof(c3) << endl;
    cout << "sizeof(c4) = " << sizeof(c4) << endl;
    cout << "sizeof(stu) = " << sizeof(stu) << endl;

    return 0;
}

执行结果:
在这里插入图片描述

5 – 成员函数类内声明类外实现

#include <iostream>
#include <string>

using namespace std;

//类的成员函数类内声明,类外实现

class Person{
public:
    void setID(int id);
    void setName(string name);
    void setAge(int age);
    void setEid(string eid);
    void PrintMsg();
private:
    int p_ID;
    string p_Name;
    int p_Age;
    string p_Eid;
};

//如果类外实现成员函数,则需要通过类名访问成员函数,需要使用域解析符
void Person::setID(int id)
{
    p_ID = id;
}

void Person::setName(string name)
{
    p_Name = name;
}

void Person::setAge(int age)
{
    p_Age = age;
}

void Person::setEid(string eid)
{
    p_Eid = eid;
}

void Person::PrintMsg()
{
    cout << p_ID << " " << p_Name << " " << p_Age << " " << p_Eid << endl;
}

void test1()
{
    Person p;
    p.setID(1001);
    p.setName("zhangsan");
    p.setAge(20);
    p.setEid("19990901");

    p.PrintMsg();
}

int main(int argc, char *argv[])
{
    test1();
    return 0;
}
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路漫漫其远,吾求索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值