c++笔记
小树苗m
硕士研究生,主要研究方向人工智能、数据处理。
展开
-
13、友元(全局函数做友元、类做友元、成员函数做友元)
1、全局函数做友元#include<iostream>#include<string>using namespace std;class Buiding{ //goodGay全局函数是Buiding的好朋友,可以访问Buiding中的私有成员,友元 friend void goodGay(Buiding *buiding);public: Buiding() { m_sintingroom="dsdsd";原创 2021-11-04 00:49:23 · 181 阅读 · 0 评论 -
12、成员变量和成员函数的分开存储、this指针、const修饰成员函数
1、成员变量成员函数分开存储#include<iostream>#include<string>using namespace std;class Person{ int m_name;//非静态成员属于类对象上。,占用内存 static int m_b; //静态成员变量不属于类对象上,访问内存是发现不占内存 void func(){}//非静态成员函数也不占内存,不属于类的对象上};void test01(){ Person p;原创 2021-11-04 00:01:37 · 68 阅读 · 0 评论 -
11、构造函数和析构函数
#include<iostream>#include<string>using namespace std;//1、构造函数分为:// 按参数:无参构造、有参构造// 按类型:普通构造、拷贝构造class Person{//构造函数private: string m_name;public: Person()//无参构造 { cout<<"你执行了构造函数!"<<endl; } Person原创 2021-10-23 01:05:02 · 56 阅读 · 0 评论 -
10、类封装成员私有化案例
#include<iostream>#include<string>using namespace std;//1、姓名 可读可写//2、年龄 只读//3、情人 只写class Person{private: string m_name; int m_age; string m_qingren;public: void setname(string name) { m_name=name; } string sh原创 2021-10-23 00:29:30 · 56 阅读 · 0 评论 -
9、类的封装(类的定义、类的私有、公有、保护属性、class和struct区别)
一、类的定义#include<iostream>#include<string>using namespace std;class Student{public: //属性 string name; int m_id; //行为 void showstudent() { cout<<"姓名:"<<name<<" 学号:"<<m_id<<endl; }};i原创 2021-10-22 23:58:58 · 442 阅读 · 0 评论 -
8、结构体定义及访问、结构体与数组、结构体与指针、结构体的嵌套
1、结构体数据定义及访问#include<iostream>#include<string>using namespace std;//1、结构体定义:struct 结构体名 {结构体成员列表};struct Student{ string name; int age; int score;};//2、通过学生类型创建具体的学生(3种方法)int main(){ //2.1 struct Student s1 struct Stud原创 2021-10-21 23:31:11 · 262 阅读 · 0 评论 -
7、指针和数组、指针和函数、指针配合数组函数案例
1、指针和数组:#include<iostream>using namespace std;int main(){ int arr[5]={10,30,50,20,100}; int * p= arr; for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++) { cout<<*p<<endl; p++; }}2、指针与函数:#include<iostre原创 2021-10-21 21:43:27 · 92 阅读 · 0 评论 -
6、指正的定义、内存、使用、指针常量、常量指针
#include<iostream>using namespace std;int main(){ //1、定义指针与使用 int a=12; // int *p; // p=&a; int *p=&a; cout<<p<<endl; cout<<*p<<endl; cout<<&a<<endl; //2、指正所占用的内存,64位系统占8字节原创 2021-10-21 20:43:32 · 79 阅读 · 0 评论 -
5、函数的定义、调用及分文件编写
1、函数的定义与调用:#include<iostream>using namespace std;//函数的声明(当函数写在main函数后面时,告诉编译器,这个函数存在)int add(int a,int b);int main(){ int a=10; int b=11; add(a,b);}int add(int a,int b){ int res; res = a+b; cout<<res<<endl;原创 2021-10-21 18:26:38 · 180 阅读 · 0 评论 -
4、数组的定义(一维和二维数组的定义及访问其行列)
1、一维数组定义:#include<iostream>using namespace std;int main(){ //定义数组: //(1)数据类型 数组名[数组长度]; int score[10]; score[0]=10; score[1]=20; //cout<<sizeof(score)<<endl; for(int i =0;i<10;i++) { cout<原创 2021-10-21 17:52:28 · 565 阅读 · 0 评论 -
3、while/do...while/for语句及嵌套/跳转语句goto
#include<iostream>using namespace std;int main(){ //循环结构:while(循环条件){循环语句} int i =0; while(i<10) { cout<<i<<endl; i++; } //do...while循环语句:do{循环语句}while(循环条件) int c = 0; do {原创 2021-10-21 16:04:41 · 104 阅读 · 0 评论 -
2、选择结构(if语句、if语句的嵌套、switch语句)
1、单行格式if语句:#include<iostream>using namespace std;int main(){ int score; cout<<"please input a number:"<<endl; cin>>score; //单行if语句:if(条件){条件满足执行的语句} if (score>100) { cout<<"the input number is:原创 2021-10-21 02:08:00 · 209 阅读 · 0 评论 -
1、常量定义、实型、科学计数法、字符及字符串定义
1、#define和const#include<iostream>using namespace std;int main(){ /*----------------常量定义、实型、科学计数法、字符及字符串定义-----------------*/ //1、#define 宏常量,定义之后其值不可修改 //2、const修饰的变量,定义之后其值不可修改 #define Day 7 const int month=12; cout<&原创 2021-10-21 01:49:28 · 378 阅读 · 0 评论