C++测试

news2024/9/19 14:51:30

开始对C嘎嘎下手!

1.有关char数组

定义长度为5,但是实际长度是定义长度减1

突然就想到计网安全中的栈溢出问题了,C语言是不检查你是否越界的,如果通过让实参溢出覆盖掉原程序的返回地址,通过精心控制是可以让计算机执行恶意代码的。这里也是能够覆盖和输出定义的后面,存在问题。char数组慎重使用!

2.

指针常量:int * const ptr 指针的常量类型,指的地址不能改变,但是指向地址的内容可以改变

常量指针:const int * ptr 指向常量的指针,指的地址可以改变,但是指向地址的内容不能改变

 3.回调函数,

具体参考的是这里C语言回调函数详解

int Callback_1(int a)   ///< 回调函数1
{
    printf("Hello, this is Callback_1: a = %d ", a);
    return 0;
}

int Callback_2(int b)  ///< 回调函数2
{
    printf("Hello, this is Callback_2: b = %d ", b);
    return 0;
}

int Callback_3(int c)   ///< 回调函数3
{
    printf("Hello, this is Callback_3: c = %d ", c);
    return 0;
}

int Handle(int x, int (*Callback)(int)) ///< 注意这里用到的函数指针定义
{
    Callback(x);
}

int main()
{
    Handle(4, Callback_1);
    Handle(5, Callback_2);
    Handle(6, Callback_3);
    return 0;
}

指向函数代码首地址的指针变量称为函数指针.

如果少加括号会认为 int * f(int x),是定义了一个函数的定义,(int *)是返回类型。如果加了括号,则代表定义了一种函数指针,int代表了你调用的函数指针的返回值.

顺便看一下传指针和传数值的区别,第一个传数值,在f1函数中传函数指针a,其实就是f2,调用该函数指针的时候,会将t的值传给f2的形参,但返回x2形参的地址,和t的地址不同,所以f1的返回值不是t的地址解析,返回值不是2.

          

 4.str类函数

<cstring>

char *strcpy( char *s1, const char *s2 ):把s2的内容拷贝给s1

char *strncpy( char *s1, const char *s2, size_t n ):把s2的前n个字符内容拷贝给s1

char *strcat( char *s1, const char *s2 ):s1 + s2 + null character

char *strncat( char *s1, const char *s2, size_t n ):s1 + n char of s2 + null character

int strcmp( const char *s1, const char *s2 ):=0: s1 is equal to s2;<0 (usually -1): s1 is less than s2;>0 (usually  1): s1 is greater than s2.

int strncmp( const char *s1, const char *s2, size_t n ):Compares up to n characters of the string s1 with the string s2.

size_t strlen( const char *s ):Determines the length of string s.

char *strtok( char *s1, const char *s2 )根据delimiter分隔符s2,将字符串s1分解为若干个token

<string>

str.substr(start,sublen);

5.类,

在类内定义的全局静态变量只有一个,被每个对象所共享

 

 不要返回临时对象的引用;不要返回私有对象的引用(会破坏类的封装)

6.拷贝构造函数 

class Person; Person(const Person & p)

以下三种情况,不是赋值,而是调用拷贝构造函数:

①使用一个已创建完毕的对象来初始化一个新的对象

②值传递的方式给函数参数传值

③以值方式返回局部变量

在部分情况使用默认的拷贝构造函数会出现问题,如上面在数据成员是指针的时候,默认出错,需要自己定义拷贝构造函数 

7.带默认参数的函数,

对比下面的情况,声明和默认参数应在前面说明,但是函数体应在后面

 

 

8.构造函数初始化列表,

所有的类成员都可以用构造函数初始化列表进行初始化,而以下情况只能如此:

①常成员对象,②引用的数据成员,③数据成员是其他类且没有提供缺省构造函数的,④继承类的基类且没有提供缺省构造函数的

注:1.如果变量b是变量a的引用 那么无论a,b中任何一个值改变,另外一个也相应的改变,在声明一个引用时,必须同时使之初始化,即声明它代表哪一个变量。请注意:由于引用不是独立的变量,编译系统不给它单独分配存储单元,因此在建立引用时只有声明没有定义,只是声明它与原有的某一变量的关系。2.const static integer这种是特例,因为static是静态的所以可以赋值
 

9.友元函数

--可以访问类的非公有成员

10.静态成员

数据成员

Const static integral数据成员可以在类定义中初始化。

所有其它静态数据成员都必须在类外部定义并初始化。

11.继承&多态

(不得不说之前的我真的太认真了,现在只想摆烂,回来整个笔记的合集吧)

 

 12.文件!!!

头文件#include<fstream>

输入流指针:istream读指针(键盘或文件->内存);输出流指针:ostream写指针(内存->文件)

#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
	ofstream ofile;
	ofile.open("file.txt",ios::out|ios::binary);
	int a=7;int aa=4;
	ofile<<a;
	ofile.write(reinterpret_cast<const char *>(&aa),sizeof(int));
	ofile.close();

	ifstream ifile;
	ifile.open("file.txt",ios::in|ios::binary);
	int b;
	ifile>>b;
	int c;
	ifile.read(reinterpret_cast<char *>(&c),sizeof(int));
	ifile.close();

	cout<<b<<" "<<c<<endl;

	system("pause");
	return 0;
}

大致读写文件的过程:

#include<iostream>
#include<fstream>
#include<iomanip>
#include"5.h"
using namespace std;

int main()
{
	int idnum=0;
	char first[20]={'\0'};
	char last[20]={'\0'};
	double grade=0;

	ofstream ofile("file.txt",ios::out|ios::binary);
	if(!ofile)
	{
		cout<<"file.txt can not be open!!"<<endl;
		return -1;
	}
	ifstream ifile("file.txt",ios::in|ios::binary);
	
	if(!ifile)
	{
		cout<<"file.txt can not be open!!"<<endl;
		return -1;
	}


	double number;
	cout<<"How many students in the class?";
	cin>>number;
	double addgrade=0;


	for(int i=0;i<number;i++)
	{
		char first[20]={'\0'};
		char last[20]={'\0'};
		Student s;
		Student s1;
		cout<<"Please input the "<<i+1<<" student's id,firstname,lastname,score"<<endl;
		cin>>idnum>>setw(20)>>first>>setw(20)>>last>>grade;
		s.setid(idnum);s.setfirstname(first);s.setlastname(last);s.setscore(grade);
		ofile.seekp((idnum-1)*sizeof(Student));
		ofile.write(reinterpret_cast<const char *>(&s),sizeof(Student));

		ofile.flush();

		ifile.seekg((idnum-1)*sizeof(Student));
		ifile.read(reinterpret_cast<char *>(&s1),sizeof(Student));
		cout<<i<<": "<<s1.getid()<<" "<<s1.getfirstname()<<" "
			<<s1.getlastname()<<" "<<s1.getscore()<<endl;
		addgrade +=grade;
	}
	ofile.flush();
	cout<<"Average score: "<<addgrade/number<<endl;



	
	int want;
	cout<<"\nEnter how many student's ID number to delete ?";
	cin>>want;
	for(int i=0;i<want;i++)
	{
		Student s;
		cout << "Enter student's ID number to delete : ";
		cin>>idnum;
		ifile.seekg((idnum-1)*sizeof(Student));
		ifile.read(reinterpret_cast<char *>(&s),sizeof(Student));
		cout<<"Before edit: "<<s.getid()<<" "<<s.getfirstname()<<" "
			<<s.getlastname()<<" "<<s.getscore()<<endl;
		addgrade -=s.getscore();

		s.setid(0);s.setlastname("");s.setfirstname("");s.setscore(0);
		ofile.seekp((idnum-1)*sizeof(Student));
		ofile.write(reinterpret_cast<const char *>(&s),sizeof(Student));
	}
	number -= want;
	cout<<"Now Average score: "<<addgrade/number<<endl;
	ofile.flush();


	cout<<"\nEnter how many student's ID number to add ?";
	cin>>want;
	for(int i=0;i<want;i++)
	{
		Student s1;
		cout << "Enter student's ID number,firstname,lastname,score to add : "<<endl;
		cin>>idnum>>first>>last>>grade;
		s1.setid(idnum);s1.setlastname(first);s1.setfirstname(last);s1.setscore(grade);
		ofile.seekp((idnum-1)*sizeof(Student));
		ofile.write(reinterpret_cast<const char *>(&s1),sizeof(Student));

		ofile.flush();

		ifile.seekg((idnum-1)*sizeof(Student));
		ifile.read(reinterpret_cast<char *>(&s1),sizeof(Student));
		addgrade +=grade;
	}
	number += want;
	cout<<"Now Average score: "<<addgrade/number<<endl;


	cout << "\nEnter student's ID number to edit the record?(0 to end input)\n ";
	cin>>idnum;
	while ( idnum > 0)
	{
		Student s;
		int choose;
		cout << "Which one do you want to edit?\n1:ID number\n2:firstname\n3:lastname\n4:grade\n ";
		cin>>choose;
		ifile.seekg((idnum-1)*sizeof(Student));
		ifile.read( reinterpret_cast< char * >(&s),sizeof( Student ) );
		cout<<"Before edit: "<<s.getid()<<" "<<s.getfirstname()<<" "
			<<s.getlastname()<<" "<<s.getscore()<<endl;
		switch(choose)
		{
		case 1:cin>>idnum;s.setid(idnum);break;
		case 2:cin>>setw(10)>>first;s.setfirstname(first);break;
		case 3:cin>>setw(15)>>last;s.setlastname(last);break;
		case 4:cin>>grade;addgrade=addgrade-s.getscore()+grade;s.setscore(grade);break;
		default:cout<<"wrong edit"<<endl;break;
		}
		ofile.seekp((idnum-1) *sizeof(Student));
		ofile.write( reinterpret_cast< const char * >(&s),sizeof(Student));
		ofile.flush();

		ifile.seekg((idnum-1)*sizeof(Student));
		ifile.read( reinterpret_cast< char * >(&s),sizeof( Student ) );
		cout<<"After edit: "<<s.getid()<<" "<<s.getfirstname()<<" "
			<<s.getlastname()<<" "<<s.getscore()<<endl;


		cout << "Enter student's ID number to edit the record(0 to end input)\n ";
		cin >>idnum;
	} 
	cout<<"Now Average score: "<<addgrade/number<<endl;
	//averageScore=score/i;
	ofile.flush();
	
	cout<<"ALL: "<<endl;
	for(int i=0;i<10;i++)
	{
		Student s;
		ifile.seekg((i)*sizeof(Student));
		ifile.read( reinterpret_cast< char * >(&s),sizeof( Student ) );
		if(s.getid() != 0)
		{
			cout<<s.getid()<<" "<<s.getfirstname()<<" "
				<<s.getlastname()<<" "<<s.getscore()<<endl;
		}
	}


	ifile.close();
	ofile.close();

	system("pause");
	return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/674652.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

高级数据结构——红黑树

目录 1. 红黑树的概念 2. 红黑树的性质 3. 红黑树 6. 红黑树的验证 7. 红黑树的删除 8. 红黑树与AVL数的比较 9. 红黑树的应用 10. 完整代码 10.1 RBTree.h 10.2 test.cpp 1. 红黑树的概念 红黑树&#xff0c;是一种二叉搜索树&#xff0c;但在每个结点上增加一个存…

49天精通Java,第37天,可变参数列表

目录 一、可变参数列表二、可变参数列表的优缺点1、优点2、缺点 三、可变参数列表的适用场景1、函数重载2、命令行参数解析3、集合操作4、函数式编程 大家好&#xff0c;我是哪吒。 &#x1f3c6;本文收录于&#xff0c;49天精通Java从入门到就业。 全网最细Java零基础手把手…

SpringBoot 如何使用 @ResponseStatus 注解处理异常状态码

SpringBoot 如何使用 ResponseStatus 注解处理异常状态码 在 SpringBoot 应用程序中&#xff0c;异常处理是一个非常重要的话题。当应用程序出现异常时&#xff0c;我们需要对异常进行处理&#xff0c;以保证应用程序的稳定性和可靠性。除了使用异常处理器外&#xff0c;Sprin…

重新理解微服务之终究绕不过这4个坎之(一)

写在前头 大家曾经有没有遇过日常技术交流的时候&#xff0c;会讨论某某技术之间的关系是什么&#xff0c;某些技术是否应该用到微服务。我相信热爱技术交流的您&#xff0c;就算不是在微服务这里领域&#xff0c;或多或少都会跟其他同行会做一些争议话题的探讨&#xff0c;而…

华为OD机试真题B卷 JavaScript 实现【字符串分隔】,附详细解题思路

一、题目描述 输入一个字符串&#xff0c;请按长度为8拆分每个输入字符串并进行输出&#xff0c;长度不是8整数倍的字符串请在后面补数字0&#xff0c;空字符串不处理。 二、输入描述 连续输入字符串(每个字符串长度小于等于100)。 三、输出描述 依次输出所有分割后的长度…

k8s使用ceph存储

文章目录 初始化操作k8s使用ceph rbdvolumePV静态pv动态pv k8s使用cephfsvolume静态pv 初始化操作 ceph创建rbd存储池 ceph osd pool create k8s-data 32 32 replicated ceph osd pool application enable k8s-data rbd rbd pool init -p k8s-dataceph添加授权&#xff0c;需…

指针和数组--指针数组及其应用

目录 一、指针数组用于表示多个字符串 二、指针数组用于表示命令行参数 一、指针数组用于表示多个字符串 一维数组可存储一个字符串&#xff0c;二维数组可存储多个字符串。 二维数组的元素在内存中是连续存放的&#xff0c;存完第一行后&#xff0c;再存第二行&#xff0c;以…

多线程之JUC

写在前面 本文一起看下jdk并发包的相关内容。 1&#xff1a;JUC包提供了哪些功能 先通过包结构看下JUC提供的功能&#xff1a; 接下来分别看下。 1.1&#xff1a;锁 JUC中的锁机制提供了比synchronized&#xff0c;wait/notify更加灵活的同步控制&#xff0c;在java.util.…

大数据基础平台实施及运维进阶

1、完全分布式部署介绍 完全分部式是真正利用多台Linux主机来进行部署Hadoop&#xff0c;对Linux机器集群进行规划&#xff0c;使得Hadoop各个模块分别部署在不同的多台机器上。 2、nameNode HA完全分布式部署 2.1、nameNode切换方法 分别处于Active和Standby中 hadoop可以…

操作系统复习笔记4

1、queueType队列类型 队列中的数据也呈线性排列。虽然与栈有些相似&#xff0c;但队列中添加和删除数据的操作分别是在两端进行的。 线性表有顺序存储和链式存储&#xff0c;队列作为一种特殊的线性表&#xff0c;也同样存在这两种存储方式。 1.1 顺序队列 用数组存储队列…

C语言学习(二十五)---指针练习题(一)

在上一节内容中&#xff0c;我们学习了递归与冒泡排序法的有关内容&#xff0c;今天我们将继续往下学习&#xff0c;主要内容为指针练习题&#xff0c;好了&#xff0c;话不多说&#xff0c;开整&#xff01;&#xff01;&#xff01; 在之前的第18—22的内容中&#xff0c;我…

lnmp框架的应用

目录 应用一 nginx访问状态统计 1.先查看http_stub_status有没有安装 2.进入nginx的配置文件改配置 3.nginx-检查配置 重启服务 最后这个20就是显示的状态统计 应用二 给网站加密 1.首先安装http-tools软软件 2.把nginx设置锁也要有执行权限 3.进入nginx配置文件 4. 检查…

【Windows个性化设置篇】StartAllBack更改win11任务栏设置

【Windows个性化设置篇】StartAllBack更改win11任务栏设置 Windows11目前不支持更改任务栏位置固定的修改&#xff0c;因为想把任务栏固定到旁边&#xff0c;从而充分利用电脑屏幕位置。之前试过TranslucentTB可以把任务栏透明化&#xff0c;很漂亮&#xff0c;但在分屏操作时…

【Vue3】Vue3+Vite+TS使用npm包引入百度地图

文章目录 Vue3ViteTS引入百度地图一、注册二、安装依赖包三、参考文档四、全局注册五、局部导入六、断网地图的使用八、项目使用成功图片九、使用卫星图 Vue3ViteTS引入高德地图npm包查找地图依赖包 Vue3ViteTS引入百度地图 一、注册 官网&#x1f449;百度地图开放平台 注册…

python---案例分析(1)

标准库 python自带的 第三方库 其他人做出来的 例1: 实现一个日期计算器 EG: 计算2012年2月14日和2016年2月3日之间的差值 使用datetime 1.根据日期构造出datetime类型的变量 2.把两个变量进行相减,得到的结果即为所求 1) 2) 3) 例2: 实现单词逆序 翻转单词顺序 i am a s…

MySQL数据库表的操作

创建表 语法&#xff1a; CREATE TABLE table_name (field1 datatype,field2 datatype,field3 datatype ) character set 字符集 collate 校验规则 engine 存储引擎; 说明&#xff1a; field 表示列名。 datatype 表示列的类型。 character set 字符集&#xff0c;如果没有指…

hutool包下的BeanUtil工具使用、SQL中的and和OR的优先级

SQL中的and和OR的优先级 首先and的优先级大于or&#xff0c;通俗理解其实or查询其实会把条件分为左右两边来查。 如select * from user where id 1 and status 2 or status 3,本来想查询user表中id为1的状态为2或者3的数据&#xff0c;其实只会这样执行&#xff0c;and比or…

大数据分析案例-基于LightGBM算法构建航空公司满意度预测模型

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

DOD Digital Engineering Vision 数字工程策略与实施

DOD Digital Engineering Vision DOD的数字工程策略与实施&#xff0c;仅供个人学习使用&#xff0c;不代表个人意见和观点&#xff01;&#xff01;&#xff01; Digital Engineering Strategy and Implementation Ms. Philomena Zimmerman Office of the Under Secretary …

day1

在linux内核中&#xff0c;当用户打开设备文件时&#xff0c;内核中的VFS层会调用设备驱动中的sys_open()函数&#xff0c;在sys_open()函数中&#xff0c;内核会根据文件的inode号判断文件是否存在于文件系统中&#xff0c;如果存在&#xff0c;内核会找到这个文件的文件信息结…