如何在父类中调用子类?

发布于 2024-12-10 08:29:17 字数 1230 浏览 1 评论 0原文

class Society
{
void add_student(string name,string home_town)//add student to society
{
    Student a;
a.set_name(name);
a.set_home_town(home_town);
}
bool same_community(Student s1, Student s2){}//check if student 1 & student 2 are in the same community
void join_communities(Student s1,Student s2){}//join communities which student 1 & student 2 are in
int num_of_communities(){}//return the total number of communities inside the society
float max_diversity(){}//return the highest diversity between all communities 
};

class Community : public Society
{
void add(Student new_student){}//add new student to community
bool contains(string name_student){}//whether community contains a student named name_student
void join(Community other_community){}//add all students in other_community to this community
float diversity(){}//return the number of distinct hometowns/names for this community
};

class Student :public Community
{
string name, string home_town;
public:
void set_name(string a){name=a;}
void set_home_town(string b){home_town=b;}
string get_name() const{return name;}
string get_home_town() const{return home_town;}
};

我有一个名为 Society 的父类,我想在某些函数中使用其名为 Student 的子类。我该怎么做?

class Society
{
void add_student(string name,string home_town)//add student to society
{
    Student a;
a.set_name(name);
a.set_home_town(home_town);
}
bool same_community(Student s1, Student s2){}//check if student 1 & student 2 are in the same community
void join_communities(Student s1,Student s2){}//join communities which student 1 & student 2 are in
int num_of_communities(){}//return the total number of communities inside the society
float max_diversity(){}//return the highest diversity between all communities 
};

class Community : public Society
{
void add(Student new_student){}//add new student to community
bool contains(string name_student){}//whether community contains a student named name_student
void join(Community other_community){}//add all students in other_community to this community
float diversity(){}//return the number of distinct hometowns/names for this community
};

class Student :public Community
{
string name, string home_town;
public:
void set_name(string a){name=a;}
void set_home_town(string b){home_town=b;}
string get_name() const{return name;}
string get_home_town() const{return home_town;}
};

I have a parent class named Society and I would like to use its child class named Student in some of the functions.how can I do this?

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

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

发布评论

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

评论(1

木有鱼丸 2024-12-17 08:29:17

我同意其他评论,但如果有必要,您可以使用此表单应用 CRTP 模式。但是,此解决方案仅适用于学生社区,您不能混合不同的社区成员:

template<typename T>
class Society
{
    void add_student(string name,string home_town)
    {
        T student;
        student.set_name(name);
        ......
    }
    ....
};

template <typename T>
class Comunity : public Society<T>
{
    void add(T new_student){}//add new student to community
    ....
};

class Student : public Comunity<Student>
{
    .....
}

I agree with other comments, but if necessary, you can apply CRTP pattern with this form. However, this solution only apply to Student communities, you can't mix different community members:

template<typename T>
class Society
{
    void add_student(string name,string home_town)
    {
        T student;
        student.set_name(name);
        ......
    }
    ....
};

template <typename T>
class Comunity : public Society<T>
{
    void add(T new_student){}//add new student to community
    ....
};

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