相关概念的理解容易混淆,记住之前学习理解的一个准则!*p 的理解意义就是p 所指向的那个东西
&
*
参考:
- 乘号(Multiply): 2*3 就是 6
- 声明指针(Pointer Statement): int a =5; int* ptr=&a;就是声明变量 a 是 5,把 a 的地址附到指针 ptr 上
- 解引用 (Dereference): ptr 单独拿出来就是找出 ptr 指针指向的值,按照第二点的说法就是 5.
&叫做取地址符号
• 一般指针只能接受一个内存地址而不能接受一个值
• int a =5; int ptr=a;就是错误的,指针不能接受一个值
• int a =5; int* ptr=&a;才对,把 a 的地址给指针 ptr
指针理解的话你可以这么想。
你问我书在哪。我说在书架第一层。
你问我书在哪。我说在那个桌子上的纸条上写着位置。这样的话那个纸条就是指针。
纸条上写着,书在书架第一层,这就是地址,他并不是书的实体
真正的书在书架第一层,顺着地址去找吧
- 结构体:构造(定义新的数据类型)
- 结构体变量:定义(两种);结构体变量访问结构体里面的成员:
.
;指针访问成员的时候->
- 结构体数组:变量很多
- 结构体指针:指向结构体变量的指针,定义:
struct Student *p
;指向结构体数组的指针
typedef int Count
上面的代码可以分解为:
typedef struct LNode LNode; //将结构体类型struct LNode重命名为LNode
typedef struct LNode *LinkList; //将struct LNode *重命名为LinkList
创建的时候,可以有下面三种方法
【L 是指向结构体类型(struct LNode 型)指针变量】
只不过是用第 1 种方式创建更加简单,具体的使用,new 的时候一般使用前者,定义一个该类型变量的时候一般用后者
LinkList L;理解:一个结构体变量的指针就是该结构体变量所占据的内存段的起始地址,而我定义的是 LinkList 而不是 int,只是和计算机说明我需要的内存不一样了这种作用
参考:https://blog.csdn.net/GRoads/article/details/104155255
在学习单链表的时候,编写前插法的函数,函数参数传递的是&L 而不是*L,这个相关知识的解释,参考这个帖子https://fishc.com.cn/thread-98021-1-1.html,里面的榜首答案下图这么一句话
图 1 是可以运行的,图 2 不可以(传递错误的格式)
函数参数的传递,还要好好再复习复习……
在数据结构课程里面的类 C 语言里面,new、delete 分别为在内存中分配/释放空间
https://www.cnblogs.com/mr-wid/archive/2013/01/22/2871105.html
C 语言中,可以用 #define 定义一个标识符来表示一个常量。其特点是:定义的标识符不占内存,只是一个临时的符号,预编译后这个符号就不存在了。
预编译又叫预处理。预编译不是编译,而是编译前的处理。这个操作是在正式编译之前由系统自动完成的。
const 定义常量
之前 C 语言里面用的是宏定义#define Π 3.1415927,但是宏定义有时候太“死板”
这不只是形式上的改变,其实现也有所不同。
C++标准库中所有标识符并定义于一个名为 std 的命名空间中,std 称为标准命名空间(standard,标准的)。
C++中的输入、输出头文件 iostream
i ——input 输入
o ——output 输出
stream —— 流
操作符 | 功能 |
---|---|
setfill(char c) | 设置以 c 表示的填充字符 |
setprecision(int n) | 设置以 n 表示的数值精度 |
setw(int n) | 设置以 n 表示的域宽 |
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(){
double num=sqrt(3.0);
for (int i = 0; i < 6; i++){
cout <<"当精度为"<<i<<"时:"<<setprecision(i)<<num<<endl;//设置不同的精度
}
cout << "当前cout精度为:" <<cout.precision() << endl;
cout << "当前域宽:" <<cout.width() << endl;
cout << "=================================="
<< "\n";
cout <<setw(7)<<num << endl; //默认是右对齐
cout << "当前填充字符:" << endl;
cout<<setfill('*') <<setw(10)<<num << endl; //setfill()函数可以直接插入流中
return 0;
}
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <Licenses - GNU Project - Free Software Foundation>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
C++
Printf()
Scanf()
Putchar()
Getchar()
Puts(str)
Gets(str)
Strcat(str1,str2):连接字符串
复制函数:将 str2 复制给 str1
Strcpy(str1,str2)
在 C++里面会报错
解决 ①:将函数 strcpy 改成 strcpy_s,中间加一个参数——复制的长度
Strncpy(str1,str2,n):复制函数,将 str2 的前 n 个字符复制给 str1
Strlen(str):检测 str 的字符串长度
Fopen(文件名,使用文件方式)
fclose(文件指针)
类的形式?
Class wuzutao
{
private:
public:
protect:
};
数据成员
成员函数:
类内实现=和普通函数的实现一样
类外实现=void Date::Display(){}
类定义对象?
① 花括号内定义
② 花括号外定义
访问对象?
访问的是类内的成员
① 圆点访问形式
② 指针访问形式
this 指针?
每个成员函数都会有一个特殊的隐含指针——this 指针。
调用形式:cout<<this<year<day<<endl;
构造函数和析构函数
构造函数的作用就是适当地给类进行初始化的作用
无参数的构造函数
带参数的构造函数
拷贝构造函数,这里不是太懂!
拷贝构造函数声明
拷贝构造函数定义和实现
析构函数
一般默认,如果自己写的话就是
析构函数的声明:类名();类名(){……}
析构函数的定义和实现:
类名::
数据共享 static
数据保护 const
类的组合
直观:B 类中有 A 类定义的对象
构造函数的顺序:
对象成员的构造函数
自身的构造函数
构造函数有参数的时候:
怎么个表现形式呢?
B 类中 B 的构造函数后面带有 A 类在 B 类中所定义的对象,这个对象的参数用做左边 B 类构造函数的参数、
类的依赖
赌徒和骰子,这就是一种依赖关系,如果用组合关系的话,就会影响到生命周期问题
Class 🎲{};
Class 🕵️♂️
{
public:
Void play(🎲1,🎲2,🎲3)
{……}
};
类的继承和派生
一、派生类的定义:
父类 class Base{}
派生子类:单一继承 class Derived:public Base{};
多重继承 Class Derived:public Base1,pretect Base2{};
继承的类型:
单一继承:只有一个基类
多重继承:有多个基类
继承方式:
private 继承:所有的数据成员都为子类的的私有成员
public 继承:父类的数据类型怎样,在子类类型不变
protect 继承:所有的数据成员和成员函数都为字类的保护成员
ps:① 父类的 private 数据成员和成员函数不能被子类所继承
② 一般很少用 protect 和 private 两种继承,因为两种改变了之前基类的访问属性,限制了这些的进一步派生,所以很少使用
二、派生类的构造和析构
构造和析构的调用顺序:
① 所有虚基类的构造函数
② 基类的构造函数
③ 对象成员的构造函数
④ 自身派生类的构造函数
ps:析构函数的调用顺序则正好相反、
虚基类保证调用一次构造函数。两个子类都有同一个基类的虚基类继承,构造函数只调用一次
带参数的构造函数:
子类里面,构造函数的初始化要连带着基类的构造函数的初始化
eg:classA(int a,int b,int c):classB(a*2),classC(a,b){}
三、同名冲突和解决方案
解决 ① 双冒号法::
基类和子类当中有相同名字的数据成员和成员函数,
访问的时候:相同的名字::数据成员/成员函数
解决 ② 定义虚基类 virtual
eg:class furniture{};
Class sofa :virtual public furniture{};
Class bed:virtual public furniture{};
Class sofabed:public sofa,public bed{};
四、赋值兼容规则
主函数 main()是程序的入口,每个程序都需要一个主函数。主函数返回值为 int 型。
int main()
{
cout<<”hello,world!”<<endl;
return 0;
}
注意:程序末尾返回 0
评论区