![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++基础
青春&折腾
这个作者很懒,什么都没留下…
展开
-
类模板案例
.hpp#pragma once#include<iostream>using namespace std;//案例描述: 实现一个通用的数组类,要求如下://可以对内置数据类型以及自定义数据类型的数据进行存储//将数组中的数据存储到堆区//构造函数中可以传入数组的容量//提供对应的拷贝构造函数以及operator = 防止浅拷贝问题//提供尾插法和尾删法对数组中的数据进行增加和删除//可以通过下标的方式访问数组中的元素//可以获取数组中当前元素个数和数组的容量tem原创 2021-06-04 16:26:53 · 110 阅读 · 0 评论 -
运算符重载
加号运算符重载#include<iostream>#include<string>using namespace std;class Person{ friend void printPerson(Person& p);public: Person operator+(Person &p); Person(int age, int weight) { this->m_age = age; this->m_weight =原创 2021-06-03 22:43:35 · 90 阅读 · 0 评论 -
全局函数类外实现 类模板 友元
#include<iostream>#include<string>using namespace std;//template<class T1,class T2>//class Person;template<class T1,class T2>class Person{ template<class T1,class T2> friend void printPerson(Person<T1, T2>&a.原创 2021-06-03 21:47:56 · 114 阅读 · 0 评论 -
友元C++
生活中你的家有客厅(Public),有你的卧室(Private)客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去但是呢,你也可以允许你的好闺蜜好基友进去。在程序里,有些私有属性 也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术友元的目的就是让一个函数或者类 访问另一个类中私有成员友元的关键字为 ==friend==友元的三种实现 全局函数做友元 friend void goodGay(Building* b);//告诉编译器 good...原创 2021-05-08 11:39:38 · 157 阅读 · 0 评论 -
this指针和空指针
C++中成员变量和成员函数是分开存储的每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码那么问题是:这一块代码是如何区分那个对象调用自己的呢?c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象this指针是隐含每一个非静态成员函数内的一种指针this指针不需要定义,直接使用即可this指针的用途: 当形参和成员变量同名时,可用this指针来区分 在类的非静态成员函数中返回对象本身,可使用r转载 2021-05-08 00:12:01 · 376 阅读 · 0 评论 -
class所占内存空间
在C++中,类内的成员变量和成员函数分开存储只有非静态成员变量才属于类的对象上class Person {public: Person() { mA = 0; } //非静态成员变量占对象空间 int mA; //静态成员变量不占对象空间 static int mB; //函数也不占对象空间,所有函数共享一个函数实例 void func() { cout << "mA:" << this->mA << endl; } //静原创 2021-05-08 00:02:10 · 323 阅读 · 0 评论 -
静态成员变量和静态成员函数
#include<iostream>#include<string>#include <fstream>#include<windows.h>using namespace std;class Person{public: static int m_A; //静态成员变量 //静态成员变量特点: //1 在编译阶段分配内存 //2 类内声明,类外初始化 //3 所有对象共享同一份数据private: static int.原创 2021-05-07 22:47:15 · 109 阅读 · 0 评论 -
初始化列表
初始化列表就是一种新型简单的有参构造函数,比以前的构造函数少写一点代码代码一:class Person {public: ////传统方式初始化 //Person(int a, int b, int c) { // m_A = a; // m_B = b; // m_C = c; //} //初始化列表方式初始化 Person() :m_A(10), m_B(20), m_C(30) {} void PrintPerson() { cout << "mA:".转载 2021-05-07 21:52:30 · 1900 阅读 · 0 评论 -
浅拷贝带来的问题
浅拷贝:简单的赋值拷贝操作深拷贝:在堆区重新申请空间,进行拷贝操作问题代码:#include<iostream>#include<string>#include <fstream>#include<windows.h>using namespace std;class Person {public: //无参(默认)构造函数 Person() { cout << "无参构造函数!" << endl;原创 2021-05-07 21:15:43 · 1257 阅读 · 0 评论 -
const修饰指针,const修饰成员函数
const修饰指针有三种情况 const修饰指针 --- 常量指针 const修饰常量 --- 指针常量 const即修饰指针,又修饰常量 int main() { int a = 10; int b = 10; //const修饰的是指针,指针指向可以改,指针指向的值不可以更改 const int* p1 = &a; cout << *p1 << endl; a = 100;//只是不能通过指针修改指向的值,还是可以直接改的原创 2021-05-07 16:01:55 · 148 阅读 · 0 评论 -
地址传递交换a,b值
swap1.h#pragma oncevoid swap1(int *a, int *b){ int temp = *a; *a = *b, *b = temp;}swap.cpp#include<iostream>#include<string>#include <fstream>#include<windows.h>#include"swap1.h"using namespace std;int main...原创 2021-05-07 15:43:40 · 193 阅读 · 0 评论 -
for 循环中的return
for 循环中的return#include<iostream>#include<string>#include <fstream>#include<windows.h>using namespace std;int test(int i){ for ( i = 0; i < 10; (i)++) { cout << i << endl; for (int j = 0...原创 2021-05-07 15:26:17 · 1967 阅读 · 0 评论 -
栈区数据
栈区:由编译器自动分配释放, 存放函数的参数值,局部变量等注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放#include<iostream>#include<string>#include <fstream>#include<windows.h>using namespace std; int* func() { int a = 10;//局部变量,存放在栈区,栈区的数据在函数执行完后自动释放 return原创 2021-05-07 17:10:18 · 261 阅读 · 0 评论 -
结构体和类的区别
1.C的结构体和C++结构体的区别1.1 C的结构体内不允许有函数存在,C++允许有内部成员函数,且允许该函数是虚函数。所以C的结构体是没有构造函数、析构函数、和this指针的。1.2 C的结构体对内部成员变量的访问权限只能是public,而C++允许public,protected,private三种。1.3 C语言的结构体是不可以继承的,C++的结构体是可以从其他的结构体或者类继承过来的。以上都是表面的区别,实际区别就是面向过程和面向对象编程思路的区别:C的结构体只是把数据变量给包裹起来了,并原创 2021-05-07 17:17:25 · 109 阅读 · 0 评论 -
引用做函数返回值
作用:引用是可以作为函数的返回值存在的注意:不要返回局部变量引用用法:函数调用作为左值#include<iostream>#include<string>#include <fstream>#include<windows.h>using namespace std;//返回局部变量引用int& test01() { int a = 10; //局部变量 return a;}//返回静态变量引用int&原创 2021-05-07 17:39:54 · 364 阅读 · 0 评论