C++关于复制构造函数的 OOP 程序问题?遗产

发布于 2024-11-29 13:38:06 字数 5189 浏览 0 评论 0原文

我还有一个关于如何在两个类之间创建复制构造函数的问题。

> person(const person& a) : name(a.getName()), address(a.getAddress()),
> schoolname(a.getSchoolname()), ssn(a.getSsn()),
> idnumber(a.getIdnumber())
>         {
> 
>         }
> 
>         person& operator =(const person& b);

这是我的构造函数尝试?我失败得很惨吗?

谢谢你!

标头

#ifndef person_h
#define person_h

#include <string>
using namespace std;

    class person
    {


    public:
        person();
        person(const person& a) : name(a.getName()), address(a.getAddress()), schoolname(a.getSchoolname()), ssn(a.getSsn()), idnumber(a.getIdnumber())
        {

        }

        person& operator =(const person& b);

        person(string theName, string theAddress, string theSchoolname, string theSsn, double theIdnumber);
        string getName() const;
        void setName(string newName);
        string getSchoolname()const;
        void setSchoolname(string newSchoolname);
        string getAddress() const;
        void setAddress(string newAddress);
        string getSsn() const;
        void setSsn(string newSsn);
        double getIdnumber()const;
        void setIdnumber(double newIdnumber);
        ~person();
    protected:
        string name;
        string address;
        string schoolname;
        string ssn;
        double idnumber;
    };

实现

#include "person.h"
#include <string>
#include <iostream>
using std::string;
using std::cout;


    person::person() : name("no name yet"), address("no address yet"), schoolname("no school name"), ssn("no number") , idnumber(0)
    {

    }



    person :: person(string theName, string theAddress, string theSchoolname, string theSsn, double theIdnumber): name(theName), address(theAddress), schoolname(theSchoolname), ssn(theSsn), idnumber(0)
    {

    }





    string person :: getName() const
    {
        return name;

    }
    string person :: getAddress() const
    {
        return address;
    }
    string person :: getSchoolname() const
    {
        return schoolname;

    }

    string person ::getSsn()const
    {
        return ssn;
    }

    double person :: getIdnumber() const
    {
        return idnumber;
    }

    void person :: setName(string newName)
    {
        name = newName;

    }
    void person :: setAddress(string newAddress)
    {
        address = newAddress;
    }
    void person :: setSchoolname( string newSchoolname)
    {
        schoolname = newSchoolname;

    }

    void person ::setSsn(string newSsn)
    {
        ssn =  newSsn;
    }

    void person ::setIdnumber( double newIdnumber)
    {
        idnumber = newIdnumber;
    }

派生类 标头

#ifndef student_h
#define student_h

#include "person.h"
#include <string>

class student : public person
{
public:
    student();
    student(student const& s);

    student( double theStudentid, string theGpa, string theCounselor, string theMajor, string theGraduatingclass );

    double getStudentid() const;
    void setStudentid(double newStudentid);

    string getGpa()const;
    void setGpa(string newGpa);

    string getCounselor() const;
    void setCounselor(string newCounselor);

    string getMajor() const;
    void setMajor(string newMajor);

    string getGraduatingclass() const;
    void setGraduatingclass(string newGraduatingclass);

    void readClasses();
    void printTranscript();
    void addClass(string , string , string );

protected: 
    double studentid;
    string gpa;
    string counselor; 
    string major;
    string graduatingclass;
    string classes[40];
    string transcript[40];
    string Class[100];
    char array[100];
    int counter;
};

#endif

实现

#include "student.h"
#include <string>
using namespace std;



    student::student() : gpa("none"), counselor("none"), major("none"), graduatingclass("none"), studentid(0)
    {

    }

    student :: student ( double theStudentid, string theGpa, string theCounselor, string theMajor, string theGraduatingclass):  studentid(0), gpa(theGpa), counselor(theCounselor), major(theMajor), graduatingclass(theGraduatingclass)
    {

    }


    double student :: getStudentid() const
    {
        return studentid;
    }
    string student :: getGpa() const
    {
        return gpa;
    }
    string student :: getCounselor() const
    {
        return counselor;
    }
    string student :: getMajor() const
    {
        return major;
    }
    string student :: getGraduatingclass() const
    {
        return graduatingclass;

    }

    void student :: setStudentid(double newStudentid)
    {
        studentid = newStudentid;
    }

    void student :: setGpa(string newGpa)
    {
        gpa = newGpa;
    }
    void student :: setCounselor(string newCounselor)
    {
        counselor = newCounselor;
    }

    void student :: setMajor(string newMajor)
    {
        major = newMajor;

    }


    void student :: setGraduatingclass(string newGraduatingclass)
    {
        graduatingclass = newGraduatingclass;
    }

Main.cpp

I also have a question about how to make a copy constructor between the two classes.

> person(const person& a) : name(a.getName()), address(a.getAddress()),
> schoolname(a.getSchoolname()), ssn(a.getSsn()),
> idnumber(a.getIdnumber())
>         {
> 
>         }
> 
>         person& operator =(const person& b);

that is my constructor attempt? did I fail miserably?

Thank You!

Header

#ifndef person_h
#define person_h

#include <string>
using namespace std;

    class person
    {


    public:
        person();
        person(const person& a) : name(a.getName()), address(a.getAddress()), schoolname(a.getSchoolname()), ssn(a.getSsn()), idnumber(a.getIdnumber())
        {

        }

        person& operator =(const person& b);

        person(string theName, string theAddress, string theSchoolname, string theSsn, double theIdnumber);
        string getName() const;
        void setName(string newName);
        string getSchoolname()const;
        void setSchoolname(string newSchoolname);
        string getAddress() const;
        void setAddress(string newAddress);
        string getSsn() const;
        void setSsn(string newSsn);
        double getIdnumber()const;
        void setIdnumber(double newIdnumber);
        ~person();
    protected:
        string name;
        string address;
        string schoolname;
        string ssn;
        double idnumber;
    };

impliment

#include "person.h"
#include <string>
#include <iostream>
using std::string;
using std::cout;


    person::person() : name("no name yet"), address("no address yet"), schoolname("no school name"), ssn("no number") , idnumber(0)
    {

    }



    person :: person(string theName, string theAddress, string theSchoolname, string theSsn, double theIdnumber): name(theName), address(theAddress), schoolname(theSchoolname), ssn(theSsn), idnumber(0)
    {

    }





    string person :: getName() const
    {
        return name;

    }
    string person :: getAddress() const
    {
        return address;
    }
    string person :: getSchoolname() const
    {
        return schoolname;

    }

    string person ::getSsn()const
    {
        return ssn;
    }

    double person :: getIdnumber() const
    {
        return idnumber;
    }

    void person :: setName(string newName)
    {
        name = newName;

    }
    void person :: setAddress(string newAddress)
    {
        address = newAddress;
    }
    void person :: setSchoolname( string newSchoolname)
    {
        schoolname = newSchoolname;

    }

    void person ::setSsn(string newSsn)
    {
        ssn =  newSsn;
    }

    void person ::setIdnumber( double newIdnumber)
    {
        idnumber = newIdnumber;
    }

Derived Class Header

#ifndef student_h
#define student_h

#include "person.h"
#include <string>

class student : public person
{
public:
    student();
    student(student const& s);

    student( double theStudentid, string theGpa, string theCounselor, string theMajor, string theGraduatingclass );

    double getStudentid() const;
    void setStudentid(double newStudentid);

    string getGpa()const;
    void setGpa(string newGpa);

    string getCounselor() const;
    void setCounselor(string newCounselor);

    string getMajor() const;
    void setMajor(string newMajor);

    string getGraduatingclass() const;
    void setGraduatingclass(string newGraduatingclass);

    void readClasses();
    void printTranscript();
    void addClass(string , string , string );

protected: 
    double studentid;
    string gpa;
    string counselor; 
    string major;
    string graduatingclass;
    string classes[40];
    string transcript[40];
    string Class[100];
    char array[100];
    int counter;
};

#endif

Impliment

#include "student.h"
#include <string>
using namespace std;



    student::student() : gpa("none"), counselor("none"), major("none"), graduatingclass("none"), studentid(0)
    {

    }

    student :: student ( double theStudentid, string theGpa, string theCounselor, string theMajor, string theGraduatingclass):  studentid(0), gpa(theGpa), counselor(theCounselor), major(theMajor), graduatingclass(theGraduatingclass)
    {

    }


    double student :: getStudentid() const
    {
        return studentid;
    }
    string student :: getGpa() const
    {
        return gpa;
    }
    string student :: getCounselor() const
    {
        return counselor;
    }
    string student :: getMajor() const
    {
        return major;
    }
    string student :: getGraduatingclass() const
    {
        return graduatingclass;

    }

    void student :: setStudentid(double newStudentid)
    {
        studentid = newStudentid;
    }

    void student :: setGpa(string newGpa)
    {
        gpa = newGpa;
    }
    void student :: setCounselor(string newCounselor)
    {
        counselor = newCounselor;
    }

    void student :: setMajor(string newMajor)
    {
        major = newMajor;

    }


    void student :: setGraduatingclass(string newGraduatingclass)
    {
        graduatingclass = newGraduatingclass;
    }

Main.cpp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2024-12-06 13:38:06

体系结构 x86_64 的未定义符号:“person::~person()”,
引用自: main.o 中的 Student::~student() 中的 Student::student()
学生.o 学生::学生(双, std::string, std::string,
Student.o 中的 std::string、std::string)

根据链接器错误,您缺少 person::~person() 的定义,即 person 的析构函数。在类声明头文件中,您有:

class person
{
public:
    // ...
    person();
    ~person();
    // ...
};

但在实现文件中,您有:

// ...
person::person() {}
// Where's person::~person() {} ?
// ...

添加 person::~person() 的定义,它至少应该解决上述错误。

Undefined symbols for architecture x86_64: "person::~person()",
referenced from: student::~student() in main.o student::student() in
student.o student::student(double, std::string, std::string,
std::string, std::string) in student.o

According to the linker error, you're missing a definition of person::~person(), the person's destructor. In your class declaration header file, you have:

class person
{
public:
    // ...
    person();
    ~person();
    // ...
};

But in your implementation file, you have:

// ...
person::person() {}
// Where's person::~person() {} ?
// ...

Add a definition of person::~person() and it should resolve the above error, at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文