マルチファイルプログラムを使用して、派生クラスの基本クラスからコンストラクターを呼び出すにはどうすればよいですか。
IE:これを4つのファイルがある構造に分割するにはどうすればよいですか。Person.h、Person.cpp、Student.h、およびStudent.cpp。
class Person
{
protected:
string name;
public:
Person() { setName(""); }
Person(string pName) { setName(pName); }
void setName(string pName) { name = pName; }
string getName() { return name; }
};
class Student:public Person
{
private:
Discipline major;
Person *advisor;
public:
Student(string sname, Discipline d, Person *adv)
: Person(sname)
{
major = d;
advisor = adv;
}
void setMajor(Discipline d) { major = d; }
Discipline getMajor() { return major; }
void setAdvisor(Person *p) { advisor = p; }
Person *getAdvisor() { return advisor; }
};
次のようにしたいと思います:Person.h
class Person
{
protected:
string name;
public:
Person();
Person(string);
void setName(string)
string getName()
};
Person.cpp
#include "Person.h"
Person::Person() { setName(""); }
Person::Person(string pName) { setName(pName); }
void Person::setName(string pName) { name = pName; }
string Person::getName() { return name; }
Student.h
class Student:public Person
{
private:
Discipline major;
Person *advisor;
public:
Student(string, Discipline, Person)//call Base class constructor here or .cpp?
void setMajor(Discipline d) ;
Discipline getMajor() ;
void setAdvisor(Person *p) ;
Person *getAdvisor() ;
};
Student.cpp
#include "Student.h"
#include "Person.h"
Student::Student(string sname, Discipline d, Person *adv)//Call Base class construcotr
{
major = d;
advisor = adv;
}
void Student::setMajor(Discipline d) { major = d; }
Discipline Student::getMajor() { return major; }
void Student::setAdvisor(Person *p) { advisor = p; }
Person* Student::getAdvisor() { return advisor; }
};