C ++ ООП Программа вопрос о конструкторе копирования? наследование - PullRequest
0 голосов
/ 15 августа 2011

У меня также есть вопрос о том, как сделать конструктор копирования между двумя классами.

> 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;
    };

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;
    }

Производный заголовок класса

#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

1 Ответ

1 голос
/ 15 августа 2011

неопределенные символы для архитектуры x86_64: "person :: ~ person ()", ссылка от: student :: ~ student () в main.o student :: student () в student.o student :: student (double, std :: string, std :: string, std :: string, std :: string) в student.o

Согласно ошибке компоновщика, вам не хватает определения person::~person(), деструктора person. В вашем заголовочном файле объявления класса у вас есть:

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

Но в вашем файле реализации у вас есть:

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

Добавьте определение person::~person(), и оно должно устранить вышеуказанную ошибку, по крайней мере.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...