C++学习笔记 入门篇-1

前言

本系列内容为程序媛学习C++时做的笔记。以代码为主,并备注了打印结果以及详尽的解释注释。希望对你有所帮助。

ps:如果没有C语言或C++的基础,建议从这篇文章看起。
在这里插入图片描述

第一个demo

#include <iostream>//c++标准支持

using namespace std;//命名空间  C++的特性(类似java的内部类)

int main() {
    // C++语言面向对象 + 标准特性
    // C语言面向过程,函数+结构体
    // C++里面可以运行C语言,可以调用C语言,反之 就不行C语言无法运行C++

    // endl == \n  都是换行的含义一样
    std::cout << "Hello, World1!" << std::endl;
    // 因为你前面引入了命名空间,省略std::
    cout << "Hello, World2!" << endl;
    // C++里面可以运行C语言,可以调用C语言;反之 就不行,C语言无法运行C++
    printf("Hello, World3!");
    return 0;
}

C语言常量/C++常量

C语言的常量值,通过指针可修改。

const int i = 100;
int *iP = &i;
*iP = 200;
printf("i的值:%d", i);//200

CPP里面修改编译器不通过,不可修改。

  const int i = 100;
//Cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'
//    int *iP = &i;//这行代码爆红
//    *iP = 200;

引用&对地址的影响

不采用&

int n1 = 999;
int n2 = n1;
//n1 , n2 分别对应不同的地址(值传递,内存地址不一样)
cout << &n1 << "---" << &n2 << endl;//0x61ff0c---0x61ff08

在这里插入图片描述

采用&

int n1 = 999;
int &n2 = n1;
//n1 , n2 地址相同(再取一个名字而已,都是执行同一个地址)
cout << &n1 << "---" << &n2 << endl;//0x61ff08---0x61ff08
cout << n1 << "---" << n2 << endl;//999---999

n2 = 777;//会把n1的值一同改掉,因为n1 n2对应同一个地址。
cout << n1 << "---" << n2 << endl;//777---777

在这里插入图片描述

函数里面的&: 1

void numberChange(int &n1, int &n2) {//交换两个值
    int temp = n1;
    n1 = n2;
    n2 = temp;
}

void numberChange2(int n1, int n2) {
    int temp = n1;
    n1 = n2;
    n2 = temp;
}

//使用测试
int n1 = 111;
int n2 = 222;
int n3 = 333;
int n4 = 444;
cout << "换之前 地址 :" << &n1 << "---" << &n2 << endl;//0x61fef4---0x61fef0
numberChange(n1, n2);
cout << "换之后 地址 :" << &n1 << "---" << &n2 << endl;//0x61fef4---0x61fef0(地址未变)
cout << "值 n1 n2     :" << n1 << "---" << n2 << endl;//222---111(仅值改变)

numberChange2(n3, n4);
cout << "值 n3 n4     :" << n3 << "---" << n4 << endl;//333---444(地址和值均未改)

函数里面的&: 2

typedef struct {
    char name[20];
    int age;
} Student;

// 常量引用:Student不准你改 == const Student &
// 插入数据库,Student的信息给插入数据库
void insertStudent(const Student &student) {
//    内鬼 卧底
//    strcpy(student.name, "李元霸"); //不能这样修改 爆红

    Student student2 = {"刘奋", 43};
//    student = student2; //不能这样修改 爆红

//    只读的了,可以安心插入数据库了
    cout << student.name << "," << student.age << endl;
}

函数重载

#include <iostream>
using namespace std;

//C++支持函数重载 (C语言不支持)
int add(int number1) {
    return number1;
}

int add(int number1, int number2) {
    return number1 + number2;
}

// C++重载 == Java重载
int add(int number1, int number2, int number3) {
    return number1 + number2 + number3;
}

// 默认行参赋值
int add(bool isOK = 0, int n1 = 200, int n2 = 300) {
    return n1 + n2;
}

//无形参名的特殊写法
//前期的时候,没有考虑好,为了防止扩展功能,你必须多传一个int类型参数
int test(int i,int){ }

int main() {
    cout << add(1) << endl;        //1
    cout << add(1, 2) << endl;      //3
    cout << add(1, 2, 3) << endl;    //6
    cout << add(false) << endl;     //500

    test(1,1);
    return 0;
}

头文件的使用

Student.h

// Student.h 头文件  只写声明,不写实现
class Student {
private: //下面的代码(成员和函数),都是私有
    int age;
    char *name;

public://下面的代码(成员和函数),都是公开
    void setAge(int age);//声明函数
    void setName(char *name);
    int getAge();
    char *getName();
};

Student.cpp

#include "Student.h"

// 根据 Student.h 头文件 ,写实现

//这样写和实现头文件那个函数,没有任何关系,相当于另外一个函数
/*void setAge(int age) {
}*/
void Student::setAge(int age) {//实现函数
    // C++对象指向的是一个指针
    // -> 调用一级指针的成员
    this->age = age;
}

void Student::setName(char *name) {
    this->name = name;
}

int Student::getAge() {
    return this->age;
}

char *Student::getName() {
    return this->name;
}

CMakeLists.txt

add_executable(CPPFirst  T2.cpp  Student.h  Student.cpp)

调用

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

using namespace std;

int main() {
    Student stu1;   //栈区开辟空间的,当前函数调用完会自动弹栈
    //赋值用.
    stu1.setName("张三");
    stu1.setAge(18);
    cout << "stu1 name:" << stu1.getName() << "  age:" << stu1.getAge() << endl; //张三  age:18

    Student *stu2 = new Student; //堆区开辟空间的,要手动释放
    //赋值用->
    stu2->setName("李四");
    stu2->setAge(22);
    cout << "stu2 name:" << stu2->getName() << "  age:" << stu2->getAge() << endl; //李四  age:22

    if (stu2) {
        delete stu2;//手动释放堆对象stu2,不是用free了。
        stu2 = NULL;//指向NULL的地址区域
    }
    return 0;
}

布尔值

非0就是true

bool isOk1 = 2;
bool isOk2 = 0;
cout << "打印isOk1:" << isOk1 << endl;//打印isOk1:1
cout << "打印isOk2:" << isOk2 << endl;//打印isOk2:0
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值