学生书籍程序-需要在其中插入一个副本构造函数



我需要制作一个程序,定义一个存储姓名信息的学生类CStudent,学生提供的教师人数和专业:使用复制构造函数创建对象使用显式构造函数(带参数(创建对象

问题是,我不知道在哪里输入构造函数。这是我到达的代码以及我是如何写的。

#include <iostream>

using namespace std;
#define max 10
class CStudent
{
private:
char name[30];
int facnum;
char specialty[20];
public:
void getDetails(void);
void putDetails(void);
};
void CStudent::getDetails(void) {
cout << "enter name: ";
cin >> name;
cout << "enter facnum:";
cin >> facnum;
cout << "Enter student speacialty: ";
cin >> specialty;
}
void CStudent::putDetails(void) {
cout << " Student details: n";
cout << " Name:" << name << " ,facnum: " << facnum << ",Specialty: " << specialty;
}
int main()
{
CStudent std[max];     
int n, loop;
cout << "Enter total number of students: ";
cin >> n;
for (loop = 0; loop < n; loop++) {
cout << "Enter details of student " << loop + 1 << ":n";
std[loop].getDetails();
}
cout << endl;
for (loop = 0; loop < n; loop++) {
cout << "Details of student " << (loop + 1) << ":n";
std[loop].putDetails();
}
return 0;
}

复制构造函数的工作原理类似于常规构造函数,但唯一的参数是对另一个相同类型对象的引用。在这种情况下,您应该在CSStudent类中创建一个签名为:

CSStudent(const CSStudent &other);

如果你想在分配时实现这一点,你还需要以类似的方式重载=运算符:

CSStudent& operator=(const CSStudent &other);

在这些构造函数中,您需要将数据从other复制到运算符重载中的thisreturn *this。您可以在C++网站上找到更多信息:https://en.cppreference.com/w/cpp/language/copy_constructor

通常复制构造函数和复制赋值运算符都有下面的签名。

class_name ( const class_name & )
class_name& operator = (const class_name &t)

在你的情况下,它会变成:

CStudent(const CStudent& other)
{
strcpy (this->name,other->name);
strcpy (this->specialty,other->specialty);
this->facnum = other->facnum;
}

上面的代码将复制给定的CStudent,但复制分配操作员类似于:

CStudent s = anotherCStudentObj;

在您的情况下,复制-分配运算符很像复制构造函数,但除了在复制-分配操作符的末尾应该添加return *this之外。

示例:

CStudent& operator=(const CStudent& other)
{
strcpy (this->name,other->name);
strcpy (this->specialty,other->specialty);
this->facnum = other->facnum;
return *this;
}

有关复制构造函数的更多信息,请点击此处。

有关复制分配运算符的详细信息,请单击此处。

首先,请使用std::string而不是原始的char数组,这样更容易存储、修改和移动字符串数据:

#include <string> // include string header

std::string a = "Hello, world!" // explicit std namespace

using namespace std;
string b = "Hello, world!" // implicit std namespace

其次,您还没有为类提供任何构造函数。构造函数是一个特殊的方法,以类名命名,它没有返回类型:

class CStudent {
private:
string name, specialty;
int facnum;
public:
CStudent() = default; // default constructor
// with no parameters
CStudent(string name, int facnum, string specialty) {
this->name = name;
this->facnum = facnum;
this->specialty = specialty;
} // constructor with specified parameters

// copy constructor is a special constructor,
// that receives a const class reference as it's parameter:
CStudent(const CStudent& ref_obj) {
this->name = ref_obj.name;
this->facnum = ref_obj.facnum;
this->specialty = ref_obj.specialty;
}
};

现在,您可以使用以下构造函数创建对象:

CStudent a; // default constructed

int facnum;
string name, specialty; // set the data from console or manually
CStudent b(name, facnum, specialty);
CStudent b2 = CStudent(name, facnum, specialty); // the same
auto b3 = CStudent(name, facnum, specialty); // also the same

CStudent c(b); // copy constructed
CStudent c2 = b; // the same

通常,类字段有setter/getter是很常见的setter/getter是一种从字段获取或设置数据的方法:

// set of getters:
string get_name() const {
return name;
}

int get_facnum() const {
return facnum;
}

string get_specialty() const {
return specialty;
}

// set of setters:
void set_name(string name) {
this->name = name;
}
void set_facnum(int facnum) {
this->facnum = facnum;
}

void set_specialty(string specialty) {
this->specialty = specialty;
}

现在我们可以制作一个函数,它将创建一个CStudent并从控制台设置其数据:

static CStudent GetFromConsole() {
int facnum;
string name, specialty;

cout << "enter name: ";
cin >> name;
cout << "enter facnum:";
cin >> facnum;
cout << "Enter student speacialty: ";
cin >> specialty;

return CStudent(name, facnum, specialty);
}

以及一种将数据打印到控制台的方法:


void PrintStudent() const {
cout << "Name: " << name << endl;
cout << "Fac. number: " << facnum << endl;
cout << "Specialty: " << specialty << endl;
}

的工作示例

#include <iostream> // cout/cin
#include <string> // string container
#include <vector> // generic container
using namespace std;
class CStudent {
public:
// Default constructor
CStudent() = default;
// Explicit constructor
CStudent(string name, int facnum, string specialty) {
this->name = name;
this->facnum = facnum;
this->specialty = specialty;
}
// Copy constructor
CStudent(const CStudent& obj) {
this->name = obj.name;
this->facnum = obj.facnum;
this->specialty = obj.specialty;
}
// Method, that prints the contents of the object
void PrintStudent() const {
cout << "Name: " << name << endl;
cout << "Fac. num: " << facnum << endl;
cout << "Specialty: " << specialty << endl;
}
private:
string name;
int facnum;
string specialty;
};
// Function, that gets student data from the console
// and creates an instance of the CStudent class using this data
CStudent CreateFromConsole() {
cout << "Enter name: ";
string name;
cin >> name;
cout << "Enter fac. num: ";
int facnum;
cin >> facnum;
cout << "Enter specialty: ";
string specialty;
cin >> specialty;
cout << endl;
return CStudent(name, facnum, specialty); // returns an instance of the class
}
int main() {
vector<CStudent> students; // stack-like container
cout << "Enter total number of students: ";
int count;
cin >> count;
cout << "Enter details of students" << endl << endl;
for (int i = 0; i < count; i++) {
// Create an instance of the class
CStudent student = CreateFromConsole();

// Push a copy of the object into the container
students.push_back(student);
}
cout << endl;
cout << "Details" << endl << endl;
for (int i = 0; i < students.size(); i++) {
students[i].PrintStudent();
cout << endl;
}
fflush(stdin); // Reset input console 
getchar(); // Pause
return 0;
}

对不起,如果我来晚了,下面是复制赋值运算符和比较运算符示例:

class CStudent {
//         ...
//    the rest of the class
//         ...
private:
void swap(CStudent& refObj) {                      // helper method, that would
std::swap(this->name, refObj.name);            // swap data between two 
std::swap(this->facnum, refObj.facnum);        // CStudent objects  
std::swap(this->specialty, refObj.specialty);
}
public:
// copy assignment operator
// allows to copy the data from one object
// to an existing object
// usage:
//  CStudent a, b;
//  // init a and b
//  a = b; // a now holds a copy of the b's data 
CStudent& operator=(const CStudent& refObj) {
CStudent temp(refObj); // create a temporary copy
this->swap(temp);      // swap the contents of this object and the copy
return *this;          // return a reference to this object

// after the method execution, the temp object
// that contains our old data will be destroyed
// this insures safe assignment, even if we assign
// the object to itself
}

// equality comparison operator
// usage:
//  CStudent a, b;
//  // init a and b
//  if (a == b) // the equality comparison operator is called
//      cout << "Objects are equaln";
//  else
//      cout << "Objects are not equaln";
//
friend bool operator==(const CStudent& refA, const CStudent& refB) {
return                                 // checks if
refA.name == refB.name &&          // the names are equal AND
refA.facnum == refB.facnum &&      // the fac.nums are equal AND
refA.specialty == refB.specialty;  // the specialties are equal
}
// this is NOT a method, but a free function,
// that is allowed to access private members and methods of the class
};

相关内容

  • 没有找到相关文章

最新更新