构造函数
构造函数的作用:构造函数用于对对象中的类型成员变量初始化
构造函数的特殊之处
1、构造函数的函数名 和它所属类的**类名 ** 相同。(不包含形参)
2、 构造函数只能在类创建对象时被自动调用,不能按普通成员函数进行显式调用
3、 构造函数可以重载
构造函数使用时的注意事项
1、创建对象之后会自动调用
2、构造函数没有返回值
3、构造函数只会在创建对象后由系统自动调用,且只调用一次
无参初始化
#include<iostream>
#include<cstdio>
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<<id<<endl;
cout<<name<<endl;
cout<<age<<endl;
cout<<specialty<<endl;
}
int main()
{
Student s;
s.Show();
return 0;
}
class Student{
private:
int math,eng;
public:
Student():math(0),eng(0){} //初始化方式2
void Get(int _math,int _eng)
{
math = _math;
eng = _eng;
}
};
带参初始化
#include<iostream>
#include<cstdio>
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<<year<<"-"<<month<<"-"<<day;
};
};
int main()
{
int a,b,c;
cin>>a>>b>>c;
Date d(a,b,c);
d.Display();
return 0;
}
用参数初始化表初始化对象
语法形式:
类名::构造函数名([参数表])[:(成员初始化列表)]
{
[构造函数体]
}
//[] 指可选选项
#include<iostream>
#include<cstdio>
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<<year<<"-"<<month<<"-"<<day;
};
};
int main()
{
int a,b,c;
cin>>a>>b>>c;
Date d(a,b,c);
d.Display();
return 0;
}
适用于某个成员被声明为const类型,或是引用类型。
因为这两种在函数体内不允许用赋值语句为其赋值,但它又需要初始化,就必须通过成员初始化列表的方式
拷贝构造函数
拷贝构造函数也是构造函数,但是它只有一个参数,这个参数就是本类的对象(不能是其他类的对象),而且要采用对象的引用形式,通常还要加const声明。拷贝构造函数的作用简单来说就是用一个对象初始化另一个对象
拷贝构造函数的格式为:
类名(const 类名 & 引用对象名)
{
拷贝构造函数体
}
深拷贝与浅拷贝
深拷贝:不只是对指针变量储存的值进行拷贝,而且把指针变量所指向的储存空间里的储存的值进行拷贝。经过拷贝后指针是指向两个不同的地址的指针
浅拷贝:只是对指针变量的直接拷贝,拷贝后两个指针变量的值相同,即指向同一个内存空间
同一个类创建不同对象间的赋值
可以直接当做变量进行赋值
形式为:对象名1 = 对象名2
二者必须是同一个类
#include<iostream>
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<<year<<"-"<<month<<"-"<<day;
}
Date(const Date&d) // d 换成别的也可以
{
year = d.year;
month = d.month;
day = d.day;
}
};
int main()
{
int a,b,c;
cin>>a>>b>>c;
Date d1(a,b,c);
d2.display();
cout<<endl;
Date d2(d1); //写成: Date d2 = d1; 也可以
/* 但是写成 Date d2;
d2 = d1;
这样不可以引发拷贝构造函数
*/
d3.display();
return 0;
}
//提前判断是否是老师 ,如果是老师则输出职业,如果是学生则输出年级
#include<fstream>
#include<bits/stdc++.h>
#include<windows.h>
#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<<"已完成操作"<<endl;
cout<<"退出程序(y/n)";
cin>>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<<endl; //已存入的对象总数
for(int i=0;i<=n;i++) //将对象数据写入文件
{
File <<r[i].name<<"\t"
<<r[i].id<<"\t"
<<r[i].grade<<"\t"
<<r[i].gender<<"\t"
<<r[i].special<<"\t"
<<r[i].phone<<"\t"
<<r[i].book<<"\t"
<<r[i].ISBN<<endl;
}
File.close();
}
bool repeat_information(string ID,int n)
{
int t;
bool flag = false;
system("cls");
if(!r)
{
cout<<"文件打开失败";
exit(0);
}
else
for(int j=1;j<n;j++)
{
if(ID == r[j].id)
{
t = j;
flag = true;
break;
}
}
return flag;
}
void input_information(Reader r[])
{
system("cls");
char flag = '0';
int n;
int i = information_read(r);
int sum = i;
cout<<"读者信息录入"<<endl<<"请输入录入人次:";
cin>>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<<"已录入"<<endl;
system("pause");
continue;
}
i++;
}
information_write(r,i); //把对象作为形参 进入 写文件函数中
}
void all_information(Reader r[])
{
system("cls");
int n = information_read(r); //获取已存入的对象总数量
cout<<"显示所有读者信息"<<endl;
if(!r){
cout<<"无记录";
}
else{
for(int i=0;i<n;i++) //从头开始全部输出
{
cout<<"姓名:"<<r[i].name<<endl;
cout<<"学号:"<<r[i].id<<endl;
cout<<"年级:"<<r[i].grade<<endl;
cout<<"性别:"<<r[i].gender<<endl;
cout<<"专业:"<<r[i].special<<endl;
cout<<"联系方式:"<<r[i].phone<<endl;
cout<<"借阅书籍(ISBN):"<<r[i].book<<"("<<r[i].ISBN<<")"<<endl;
if((i+1)%10 == 0)
system("pause");
}
}
}
void search_information(Reader r[])
{
int t;
bool flag;
system("cls");
int n = information_read(r);
cout<<"请输入学号"<<endl;
cout<<"学号:";
cin>>ID;
for(int i=1;i<n;i++) //从头开始全部输出
{
if(ID == r[i].id)
{
t = i;
flag = true;
break;
}
}
if(flag)
{
system("cls");
cout<<"已查询到的信息:"<<endl;
cout<<"姓名:"<<r[t].name<<endl;
cout<<"学号:"<<r[t].id<<endl;
cout<<"年级:"<<r[t].grade<<endl;
cout<<"性别:"<<r[t].gender<<endl;
cout<<"专业:"<<r[t].special<<endl;
cout<<"联系方式:"<<r[t].phone<<endl;
cout<<"借阅书籍(ISBN):"<<r[t].book<<"("<<r[t].ISBN<<")"<<endl;
system("pause");
}
else
cout<<"未查询到信息"<<endl;
flag = false;
}
void revise_information(Reader r[])
{
string ID;
int t,flag;
system("cls");
int n = information_read(r);
cout<<"请输入待修改信息的读者学号"<<endl;
cout<<"学号:";
cin>>ID;
for(int i=1;i<=n;i++) //从头开始全部输出
{
if(ID == r[i].id)
{
t = i;
flag =1;
break;
}
}
if(flag)
{
cout<<"请输入待修改信息"<<endl;
cout<<"姓名:";
cin>>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<<"请输入待删除信息的读者学号"<<endl;
cout<<"学号:";
cin>>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<<"未查询到信息";
}
#include<iostream>
#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
//提前判断是否是老师 ,如果是老师则输出职业,如果是学生则输出年级
#include<fstream>
#include<bits/stdc++.h>
#include<windows.h>
#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<<"已完成操作"<<endl;
cout<<"退出程序(y/n)";
cin>>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<<endl; //已存入的对象总数
for(int i=0;i<=n;i++) //将对象数据写入文件
{
File <<r[i].name<<"\t"
<<r[i].id<<"\t"
<<r[i].grade<<"\t"
<<r[i].gender<<"\t"
<<r[i].special<<"\t"
<<r[i].phone<<"\t"
<<r[i].book<<"\t"
<<r[i].ISBN<<endl;
}
File.close();
}
bool repeat_information(string ID,int n)
{
int t;
bool flag = false;
system("cls");
if(!r)
{
cout<<"文件打开失败";
exit(0);
}
else
for(int j=1;j<n;j++)
{
if(ID == r[j].id)
{
t = j;
flag = true;
break;
}
}
return flag;
}
void input_information(Reader r[])
{
system("cls");
char flag = '0';
int n;
int i = information_read(r);
int sum = i;
cout<<"读者信息录入"<<endl<<"请输入录入人次:";
cin>>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<<"已录入"<<endl;
system("pause");
continue;
}
i++;
}
information_write(r,i); //把对象作为形参 进入 写文件函数中
}
void all_information(Reader r[])
{
system("cls");
int n = information_read(r); //获取已存入的对象总数量
cout<<"显示所有读者信息"<<endl;
if(!r){
cout<<"无记录";
}
else{
for(int i=0;i<n;i++) //从头开始全部输出
{
cout<<"姓名:"<<r[i].name<<endl;
cout<<"学号:"<<r[i].id<<endl;
cout<<"年级:"<<r[i].grade<<endl;
cout<<"性别:"<<r[i].gender<<endl;
cout<<"专业:"<<r[i].special<<endl;
cout<<"联系方式:"<<r[i].phone<<endl;
cout<<"借阅书籍(ISBN):"<<r[i].book<<"("<<r[i].ISBN<<")"<<endl;
if((i+1)%10 == 0)
system("pause");
}
}
}
void search_information(Reader r[])
{
int t;
bool flag;
system("cls");
int n = information_read(r);
cout<<"请输入学号"<<endl;
cout<<"学号:";
cin>>ID;
for(int i=1;i<n;i++) //从头开始全部输出
{
if(ID == r[i].id)
{
t = i;
flag = true;
break;
}
}
if(flag)
{
system("cls");
cout<<"已查询到的信息:"<<endl;
cout<<"姓名:"<<r[t].name<<endl;
cout<<"学号:"<<r[t].id<<endl;
cout<<"年级:"<<r[t].grade<<endl;
cout<<"性别:"<<r[t].gender<<endl;
cout<<"专业:"<<r[t].special<<endl;
cout<<"联系方式:"<<r[t].phone<<endl;
cout<<"借阅书籍(ISBN):"<<r[t].book<<"("<<r[t].ISBN<<")"<<endl;
system("pause");
}
else
cout<<"未查询到信息"<<endl;
flag = false;
}
void revise_information(Reader r[])
{
string ID;
int t,flag;
system("cls");
int n = information_read(r);
cout<<"请输入待修改信息的读者学号"<<endl;
cout<<"学号:";
cin>>ID;
for(int i=1;i<=n;i++) //从头开始全部输出
{
if(ID == r[i].id)
{
t = i;
flag =1;
break;
}
}
if(flag)
{
cout<<"请输入待修改信息"<<endl;
cout<<"姓名:";
cin>>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<<"请输入待删除信息的读者学号"<<endl;
cout<<"学号:";
cin>>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<<"未查询到信息";
}
#include<iostream>
#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