• C++,构造函数,语法+示例,非常详细!!!


    构造函数

    构造函数的作用:构造函数用于对对象中的类型成员变量初始化

    构造函数的特殊之处

    1、构造函数的函数名 它所属类的**类名 ** 相同。(不包含形参)

    2、 构造函数只能在类创建对象时被自动调用,不能按普通成员函数进行显式调用

    3、 构造函数可以重载

    构造函数使用时的注意事项

    1、创建对象之后会自动调用

    2、构造函数没有返回值

    3、构造函数只会在创建对象后由系统自动调用,且只调用一次

    无参初始化

    
    #include
    #include
    using namespace std;
    class Student
    {
    	int id;
    	string name;
    	int age;
    	string specialty;
    	
    	public:
    		Student()				//无参的初始化
    		{
    			id = 20210801;
    			name = "张三";
    			age = 20;
    			specialty = "计算机"; 
    		};
    		
    		void Show();
    };
    void Student::Show()
    {
    	cout<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    5

    class Student{
    	private:
    		int math,eng;
    	public:
    		Student():math(0),eng(0){}     //初始化方式2
    		void Get(int _math,int _eng)
    		{
    			math = _math;
    			eng = _eng;
    		}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    带参初始化

    #include
    #include
    using namespace std;
    class Date
    {
    	int year,month,day;
    	public:
    		Date(int y,int m,int d)
    		{
    			year = y;
    			month = m;
    			day = d;
    		}
    		void Display()
    		{
    			cout<>a>>b>>c;
    	Date d(a,b,c);
    	d.Display();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    6

    用参数初始化表初始化对象

    语法形式:
    类名::构造函数名([参数表])[:(成员初始化列表)]
    {
    	[构造函数体]
    }
    //[] 指可选选项
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #include
    #include
    using namespace std;
    class Date
    {
    	int year,month,day;
    	public:
    		Date(int y,int m,int d):year(y),month(m),day(d)
    		{	}
    		void Display()
    		{
    			cout<>a>>b>>c;
    	Date d(a,b,c);
    	d.Display();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    适用于某个成员被声明为const类型,或是引用类型。

    因为这两种在函数体内不允许用赋值语句为其赋值,但它又需要初始化,就必须通过成员初始化列表的方式

    拷贝构造函数

    拷贝构造函数也是构造函数,但是它只有一个参数,这个参数就是本类的对象(不能是其他类的对象),而且要采用对象的引用形式,通常还要加const声明。拷贝构造函数的作用简单来说就是用一个对象初始化另一个对象

    拷贝构造函数的格式为:

    类名(const 类名 & 引用对象名)
    {
    	拷贝构造函数体
    }
    
    • 1
    • 2
    • 3
    • 4

    深拷贝与浅拷贝

    深拷贝:不只是对指针变量储存的值进行拷贝,而且把指针变量所指向的储存空间里的储存的值进行拷贝。经过拷贝后指针是指向两个不同的地址的指针

    8

    浅拷贝:只是对指针变量的直接拷贝,拷贝后两个指针变量的值相同,即指向同一个内存空间

    7

    同一个类创建不同对象间的赋值

    可以直接当做变量进行赋值

    形式为:对象名1 = 对象名2

    二者必须是同一个类

    #include
    using namespace std;
    class Date
    {
    	int year,month,day;
    	
    	public:
    		Date(int y,int m,int d)
    		{
    			year = y;
    			month = m;
    			day = d;
    		}
    		void display()
    		{
    			cout<>a>>b>>c;
    	Date d1(a,b,c);
    	d2.display();
    	cout<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    //提前判断是否是老师 ,如果是老师则输出职业,如果是学生则输出年级 
    #include
    #include
    #include
    #include"读者类声明.h"
    #define endl '\n'
    using namespace std;
    int n; 
    string ID; 
    void reader_title()
    {
    	system("mode con cols=60 lines=30");
    	HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleScreenBufferSize(hOut, {100,1000});
    	system("color F4");
    	cout<<"\t*****************************************";
    	cout<<"\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n";
    	cout<<"\t*\t欢迎使用读者信息系统\t\t*";
    	cout<<"\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n";
    	cout<<"\t*****************************************\n";
    	system("pause");	
    }
    int reader_menu()			//选择目录 
    {
    	int flag; 
    	system("cls");
    	do
    	{
    		cout<<"\n\n\n";
    		cout<<"\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"\t\t输入你要使用的功能序号\n";
    		cout<<"\t\t1.读者信息录入\n"; 
    		cout<<"\t\t2.读者信息查询\n"; 
    		cout<<"\t\t3.读者信息总目录\n";
    		cout<<"\t\t4.读者信息修改\n";
    		cout<<"\t\t5.读者信息删除\n";
    		cout<<"\t\t0.退出\n";
    		cout<<"\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"\t\t请输入您的选择:";
    		cin>>flag;
    	}while(flag <0 || flag>5);
    	return flag;
    }
    int main()      
    {
    	int select,n;
    	char t;
    	reader_title();
    	system("cls");
    	
    	for(;;)
    	{
    		select = reader_menu();
    		{
    			if(select == 1 )
    			{
    				input_information(r);
    			}
    			else if(select == 2 )
    			{
    				search_information(r);
    			}
    			else if(select == 3 )
    			{
    				all_information(r);
    			}
    			else if(select == 4 )
    			{
    				revise_information(r);
    			}
    			else if(select == 5 ) 
    			{
    				delete_information(r);
    			}
    			else if(select == 0)
    			{
    				delete []r;
    				return 0;
    			}
    		}
    		cout<<"已完成操作"<>t; 
    		if(t == 'y' || t == 'Y')
    		{
    			delete []r;				//释放内存 
    			return 0;
    		}
    			
    	}
    }
    void Reader::_Reader(string _name,string _id,string _grade,string _gender,string _special,string _phone,string _book,string _ISBN)
    {
    	name="NULL";
    	id="NULL";
    	grade="NULL";
    	gender="NULL";
    	special="NULL";
    	phone="NULL";
    	book = "NULL";
    	ISBN = "NULL";
    	name = _name;
    	id = _id;
    	grade = _grade;
    	gender = _gender;
    	special = _special;
    	phone = _phone;
    	book = _book;
    	ISBN = _ISBN;
    }
    /*Reader::Reader()
    {
    	name="NULL";
    	id="NULL";
    	grade="NULL";
    	gender="NULL";
    	special="NULL";
    	phone="NULL";
    	book = "NULL";
    	ISBN = "NULL";
    }*/
    int information_read(Reader r[])
    {
    	fstream File;
    	File.open("reader.txt",ios::in);
    	if(!File)
    	{
    		cout<<"文件打开失败";
    		exit(0); 
    	}
    	int count=0;
    	File.seekg(0);	//对输入文件进行定位,定位到文件头 
    	File >> count; 
    	for(int i=0;i<=count;i++)
    	{
    	  File  >>r[i].name
    			>>r[i].id
    			>>r[i].grade
    			>>r[i].gender
    			>>r[i].special
    			>>r[i].phone
    			>>r[i].book
    			>>r[i].ISBN; 
    	}
    	File.close();
    	return count;		//输出已存入的对象总数量 
    } 
    void information_write(Reader r[],int n)
    {
    	system("cls");
    	fstream File;
    	File.open("reader.txt",ios::out); //ios::app 在文件的末尾录入 
    	if(!File)
    	{
    		cout<<"文件打开失败";
    		exit(0); 
    	}
    	File <>n; 
    	while(n--)
    	{
    		system("cls");
    		cout<<"姓名:";
    		cin>>r[i].name;
    		cout<<"学号:";
    		cin>>r[i].id;	
    		cout<<"年级:";
    		cin>>r[i].grade;
    		cout<<"性别:";
    		cin>>r[i].gender;
    		cout<<"专业:";
    		cin>>r[i].special;
    		cout<<"联系方式:";
    		cin>>r[i].phone;	
    		cout<<"借阅书籍";
    		cin>>r[i].book;
    		cout<<"ISBN号";
    		cin>>r[i].ISBN;
    		
    		system("cls"); 
    		if(repeat_information(r[i].id,sum) == true)
    		{
    			
    			cout<<"已录入"<>ID;
    	
    	for(int i=1;i>ID;
    	
    	for(int i=1;i<=n;i++)		//从头开始全部输出 
    	{
    		if(ID == r[i].id)
    		{
    			t = i;
    			flag =1;
    			break;	
    		}	
    	}
    	if(flag)
    	{
    		cout<<"请输入待修改信息"<>r[t].name;
    		cout<<"学号:";
    		cin>>r[t].id;
    		cout<<"年级:";
    		cin>>r[t].grade;
    		cout<<"性别:";
    		cin>>r[t].gender;
    		cout<<"专业:";
    		cin>>r[t].special;
    		cout<<"联系方式:";
    		cin>>r[t].phone;
    				
    		information_write(r,n);	//把对象作为形参 进入 写文件函数中 
    	}
    	else
    		cout<<"未查询到信息";
    
    }
    void delete_information(Reader r[]) 
    {
    	string ID; 
    	int t,flag;
    	system("cls");
    	int n = information_read(r);	
    	cout<<"请输入待删除信息的读者学号"<>ID;
    	
    	for(int i=1;i<=n;i++)		//从头开始全部输出 
    	{
    		if(ID == r[i].id)
    		{
    			t = i;
    			flag =1;
    			break;	
    		}	
    	}
    	if(flag)
    	{
    		for(int i=t+1;i<=n;i++)		//定位到要删除对象的后一个对象位置 
    		{							//后面每个对象都向前进一位,覆盖掉要删除的对象 
    			r[i-1].name = r[i].name;		 
    			r[i-1].id = r[i].id;
    			r[i-1].grade = r[i].grade;
    			r[i-1].gender = r[i].gender;
    			r[i-1].special = r[i].special;
    			r[i-1].phone = r[i].phone;
    			r[i-1].book = r[i].book;
    			r[i-1].ISBN = r[i].ISBN;
    			information_write(r,n-1);	//把每一个向前的对象都输入到文件中最后总数减去一个 
    		}		
    	}
    	else
    		cout<<"未查询到信息";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    #include
    #ifndef HEADER_Reader_H__
    #define HEADER_Reader_H__
    using namespace std;
    class Reader 
    {
    	private:
    		string name;
    		string id;
    		string grade;
    		string gender;
    		string special;
    		string phone;
    		string book;
    		string ISBN; 
    	public:
    	//	Reader();
    		void _Reader(string _name,string _id,string _grade,string _gender,string _special,string _phone,string _book,string _ISBN);		 									
    		void reader_title();											//标题
    		int reader_menu();												//菜单 
    		friend void information_write(Reader r[],int n);		//书籍信息的写 
    		friend int information_read(Reader r[]);				//书籍信息的读 
    		friend void input_information(Reader r[]);				//书籍信息的录入 
    		friend void all_information(Reader r[]);				//输出所有信息 
    		friend void search_information(Reader r[]); 			//查询信息 
    		friend void revise_information(Reader r[]);				//修改信息 
    		friend void delete_information(Reader r[]);				//删除信息 
    		friend bool repeat_information(string ID,int n);		//查重 
    }*r = new Reader[100];											//动态创建数组 
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    //提前判断是否是老师 ,如果是老师则输出职业,如果是学生则输出年级 
    #include
    #include
    #include
    #include"读者类声明.h"
    #define endl '\n'
    using namespace std;
    int n; 
    string ID; 
    void reader_title()
    {
    	system("mode con cols=60 lines=30");
    	HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleScreenBufferSize(hOut, {100,1000});
    	system("color F4");
    	cout<<"\t*****************************************";
    	cout<<"\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n";
    	cout<<"\t*\t欢迎使用读者信息系统\t\t*";
    	cout<<"\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n\t*\t\t\t\t\t*\n";
    	cout<<"\t*****************************************\n";
    	system("pause");	
    }
    int reader_menu()			//选择目录 
    {
    	int flag; 
    	system("cls");
    	do
    	{
    		cout<<"\n\n\n";
    		cout<<"\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"\t\t输入你要使用的功能序号\n";
    		cout<<"\t\t1.读者信息录入\n"; 
    		cout<<"\t\t2.读者信息查询\n"; 
    		cout<<"\t\t3.读者信息总目录\n";
    		cout<<"\t\t4.读者信息修改\n";
    		cout<<"\t\t5.读者信息删除\n";
    		cout<<"\t\t0.退出\n";
    		cout<<"\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"\t\t请输入您的选择:";
    		cin>>flag;
    	}while(flag <0 || flag>5);
    	return flag;
    }
    int main()      
    {
    	int select,n;
    	char t;
    	reader_title();
    	system("cls");
    	
    	for(;;)
    	{
    		select = reader_menu();
    		{
    			if(select == 1 )
    			{
    				input_information(r);
    			}
    			else if(select == 2 )
    			{
    				search_information(r);
    			}
    			else if(select == 3 )
    			{
    				all_information(r);
    			}
    			else if(select == 4 )
    			{
    				revise_information(r);
    			}
    			else if(select == 5 ) 
    			{
    				delete_information(r);
    			}
    			else if(select == 0)
    			{
    				delete []r;
    				return 0;
    			}
    		}
    		cout<<"已完成操作"<>t; 
    		if(t == 'y' || t == 'Y')
    		{
    			delete []r;				//释放内存 
    			return 0;
    		}
    			
    	}
    }
    Reader::Reader(string _name,string _id,string _grade,string _gender,string _special,string _phone,string _book,string _ISBN)
    {
    	name = _name;
    	id = _id;
    	grade = _grade;
    	gender = _gender;
    	special = _special;
    	phone = _phone;
    	book = _book;
    	ISBN = _ISBN;
    }
    Reader::Reader()
    {
    	name="NULL";
    	id="NULL";
    	grade="NULL";
    	gender="NULL";
    	special="NULL";
    	phone="NULL";
    	book = "NULL";
    	ISBN = "NULL";
    }
    int information_read(Reader r[])
    {
    	fstream File;
    	File.open("reader.txt",ios::in);
    	if(!File)
    	{
    		cout<<"文件打开失败";
    		exit(0); 
    	}
    	int count=0;
    	File.seekg(0);	//对输入文件进行定位,定位到文件头 
    	File >> count; 
    	for(int i=0;i<=count;i++)
    	{
    	  File  >>r[i].name
    			>>r[i].id
    			>>r[i].grade
    			>>r[i].gender
    			>>r[i].special
    			>>r[i].phone
    			>>r[i].book
    			>>r[i].ISBN; 
    	}
    	File.close();
    	return count;		//输出已存入的对象总数量 
    } 
    void information_write(Reader r[],int n)
    {
    	system("cls");
    	fstream File;
    	File.open("reader.txt",ios::out); //ios::app 在文件的末尾录入 
    	if(!File)
    	{
    		cout<<"文件打开失败";
    		exit(0); 
    	}
    	File <>n; 
    	while(n--)
    	{
    		system("cls");
    		cout<<"姓名:";
    		cin>>r[i].name;
    		cout<<"学号:";
    		cin>>r[i].id;	
    		cout<<"年级:";
    		cin>>r[i].grade;
    		cout<<"性别:";
    		cin>>r[i].gender;
    		cout<<"专业:";
    		cin>>r[i].special;
    		cout<<"联系方式:";
    		cin>>r[i].phone;	
    		cout<<"借阅书籍";
    		cin>>r[i].book;
    		cout<<"ISBN号";
    		cin>>r[i].ISBN;
    		
    		system("cls"); 
    		if(repeat_information(r[i].id,sum) == true)
    		{
    			
    			cout<<"已录入"<>ID;
    	
    	for(int i=1;i>ID;
    	
    	for(int i=1;i<=n;i++)		//从头开始全部输出 
    	{
    		if(ID == r[i].id)
    		{
    			t = i;
    			flag =1;
    			break;	
    		}	
    	}
    	if(flag)
    	{
    		cout<<"请输入待修改信息"<>r[t].name;
    		cout<<"学号:";
    		cin>>r[t].id;
    		cout<<"年级:";
    		cin>>r[t].grade;
    		cout<<"性别:";
    		cin>>r[t].gender;
    		cout<<"专业:";
    		cin>>r[t].special;
    		cout<<"联系方式:";
    		cin>>r[t].phone;
    				
    		information_write(r,n);	//把对象作为形参 进入 写文件函数中 
    	}
    	else
    		cout<<"未查询到信息";
    
    }
    void delete_information(Reader r[]) 
    {
    	string ID; 
    	int t,flag;
    	system("cls");
    	int n = information_read(r);	
    	cout<<"请输入待删除信息的读者学号"<>ID;
    	
    	for(int i=1;i<=n;i++)		//从头开始全部输出 
    	{
    		if(ID == r[i].id)
    		{
    			t = i;
    			flag =1;
    			break;	
    		}	
    	}
    	if(flag)
    	{
    		for(int i=t+1;i<=n;i++)		//定位到要删除对象的后一个对象位置 
    		{							//后面每个对象都向前进一位,覆盖掉要删除的对象 
    			r[i-1].name = r[i].name;		 
    			r[i-1].id = r[i].id;
    			r[i-1].grade = r[i].grade;
    			r[i-1].gender = r[i].gender;
    			r[i-1].special = r[i].special;
    			r[i-1].phone = r[i].phone;
    			r[i-1].book = r[i].book;
    			r[i-1].ISBN = r[i].ISBN;
    			information_write(r,n-1);	//把每一个向前的对象都输入到文件中最后总数减去一个 
    		}		
    	}
    	else
    		cout<<"未查询到信息";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    #include
    #ifndef HEADER_Reader_H__
    #define HEADER_Reader_H__
    using namespace std;
    class Reader 
    {
    	private:
    		string name;
    		string id;
    		string grade;
    		string gender;
    		string special;
    		string phone;
    		string book;
    		string ISBN; 
    	public:
    		Reader();
    		Reader(string _name,string _id,string _grade,string _gender,string _special,string _phone,string _book,string _ISBN);		 									
    		void reader_title();											//标题
    		int reader_menu();												//菜单 
    		friend void information_write(Reader r[],int n);		//书籍信息的写 
    		friend int information_read(Reader r[]);				//书籍信息的读 
    		friend void input_information(Reader r[]);				//书籍信息的录入 
    		friend void all_information(Reader r[]);				//输出所有信息 
    		friend void search_information(Reader r[]); 			//查询信息 
    		friend void revise_information(Reader r[]);				//修改信息 
    		friend void delete_information(Reader r[]);				//删除信息 
    		friend bool repeat_information(string ID,int n);		//查重 
    }*r = new Reader[100];											//动态创建数组 
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    微服务组件之Zuul
    httpd服务
    三车道交通流元胞自动机研究(matlab代码实现)
    Linux常用命令——grep命令和通配符以及
    罗技g402鼠标宏设置教程
    网络安全(黑客)自学
    牛客网:旋转数组
    利用 Window bat 脚本方便日常开发
    4G 云音箱写码
    React 的 useContext 的使用
  • 原文地址:https://blog.csdn.net/m0_62951223/article/details/126924206