C++测试

本文介绍了C语言中的一些关键概念,包括数组边界安全、指针常量与常量指针的差异、回调函数的使用、字符串处理函数、类的成员变量特别是静态成员和拷贝构造函数的作用。还讨论了带默认参数的函数、构造函数初始化列表以及友元函数。此外,详细阐述了文件I/O操作,包括读写文件的基本过程和二进制文件的操作示例。
摘要由CSDN通过智能技术生成

开始对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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值