如何解决学生用书程序 - 需要在其中插入复制构造函数
我需要编写一个程序,定义一个存储姓名信息的学生类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
复制到 this
,然后在运算符重载中复制 return *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->specialty);
this->facnum = other->facnum;
return *this;
}
有关复制构造函数 here 的更多信息。
有关复制赋值运算符 here 的更多信息。
,首先,请使用 std::string 而不是原始字符数组,这样更容易存储、修改和移动字符串数据:
#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,specialty); // the same
auto b3 = CStudent(name,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,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,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,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 equal\n";
// else
// cout << "Objects are not equal\n";
//
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
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。