【问题标题】:Class inside a class? [duplicate]班内班? [复制]
【发布时间】:2014-04-20 16:44:27
【问题描述】:

我正在尝试将此代码从使用结构转换为使用类。我遇到的问题是,我不知道我应该如何声明、初始化等类,以便它们以与下面发布的代码中相同的方式工作。我必须为每个类使用单独的文件(即 class1.h、class1.cpp、class2.h、class2.cpp、main.cpp),我不知道如何达到与结构相同的效果。如何在类中完成嵌套类?

我的另一个问题是,我应该如何处理枚举列表?何时何地声明/初始化它们?

非常感谢各位!希望我很清楚.. 我尝试用谷歌搜索它,但我发现没有任何用处。

代码如下:

#include <iostream>
#include <string>
using namespace std;

enum TIP_NASLOVA {
    STALNI,
    ZACASNI
};

struct Naslov {
    string ulica;
    string posta;
    int postna_stevilka;
    TIP_NASLOVA tip;
};

struct Oseba {
    string ime;
    string priimek;
    int starost;
    Naslov naslov;
};

void izpis(Oseba oseba) {
    cout << "IZPIS VNOSA" << endl << endl;
    cout << "Ime: " << oseba.ime << endl;
    cout << "Priimek: " << oseba.priimek << endl;
    cout << "Starost: " << oseba.starost << endl;
    cout << "Tip Naslova: ";
            if (oseba.naslov.tip==0){
               cout << "STALNI" << endl;
            }
            else if (oseba.naslov.tip==1){
               cout << "ZACASNI" << endl;
            }
    cout << "Posta: " << oseba.naslov.postna_stevilka << " " << oseba.naslov.posta << endl;
    cout << "Naslov: " << oseba.naslov.ulica << endl;
}

void vpis(Oseba& oseba) {
    int tip;
    cout << "VPIS PODATKOV NOVEGA VNOSA" << endl << endl;
    cout << "VPISI IME: ";
    cin >> oseba.ime;
    cout << "VPISI PRIIMEK: ";
    cin >> oseba.priimek;
    cout << "VPISI STAROST: ";
    cin >> oseba.starost;
    cout << "VPISI TIP NASLOVA ( 1-STALNI / 2-ZACASNI ): ";
    cin >> tip;

    switch (tip){
        case 1:
            oseba.naslov.tip = STALNI;
            break;
        case 2:
            oseba.naslov.tip = ZACASNI;
            break;
        default:
            cout << "Napaka! Izbrali ste napacen tip naslova. " <<endl;
    }

    cout << "VPISI POSTNO STEVILKO: ";
    cin >> oseba.naslov.postna_stevilka;
    cout << "VPISI POSTO: ";
    cin >> oseba.naslov.posta;
    cout << "VPISI NASLOV (FORMAT:'TrgGeneralaMaistra1'): ";
    cin >> oseba.naslov.ulica;
    cout << endl;
}

int main() {
    Oseba oseba;
    int x;
    cout << "VPIS IN IZPIS OSEBNIH PODATKOV" << endl << endl;
    for (;;) {
        cout << "Dolocite zahtevano operacijo (1-VPIS, 2-IZPIS): ";
        cin >> x;
        cout << endl << endl;
        switch (x){
        case 1:
            vpis(oseba);
            break;
        case 2:
            izpis(oseba);
            break;
        default:
            cout << "Izbrali niste nobene operacije!" << endl << endl;
        }
    }

    return 0;
}

编辑: 我知道公共、受保护和私有......如果我不够清楚,很抱歉,但我必须使用单独的文件(每个类都使用 .h 和 .cpp) 我熟悉如何使用单个文件完成这项工作。当我尝试在自己的头文件和 cpp 文件中拆分类时出现问题。

编辑 2: 好的,我看到我的问题很不清楚。原谅我不够努力。我会简化我的问题。

我正在尝试让一个班级成为另一个班级的成员。所以我的文件(目前)如下所示:

个人信息.h

#include <iostream>
#include <string>

using namespace std;

#ifndef PERSONALINFO_H
#define PERSONALINFO_H

class Person {

private:
    string name;
    Location location;

public:
    void setName(string n);
    string getName();
    void setLocation(int post_num, string post, string loc);   //I'm not sure about this part being correct.
    Location getLocation();

};

#endif

个人信息.cpp

#include "PersonalInfo.h"

void Person::setName(string n){
    name = n;
}

string Person::getName(){
    return name;
}

void Person::setLocation(int post_num, string post, string loc){
    Location location;
        location.post_num = post_num;
        location.post = post;
        location.loc = loc;
}

string Oseba::getLocation(){
    return location.post_num, location.post, location.loc;
}

位置.h

#include <iostream>
#include <string>

using namespace std;

#ifndef LOCATION_H
#define LOCATION_H

class Location {
public:                       //I made this public so I don't overcomplicate things but I need them private and set via methods like in the PersonalInfo class.
    int post_num;
    string post;
    string loc;
};


#endif

【问题讨论】:

    标签: c++ class struct enums nested


    【解决方案1】:

    简单,

    struct Naslov {
    string ulica;
    string posta;
    int postna_stevilka;
    TIP_NASLOVA tip;
    };
    
    struct Oseba {
    string ime;
    string priimek;
    int starost;
    Naslov naslov;
    };
    

    会变成

    先说 Oseba.h,

    class Oseba {
    private:    
    string ime;
    string priimek;
    int starost;
    Naslov naslov;
    public:
    string getIme();
    string getPriimek();
    int getStarost();
    .
    .
    .
    void setIme(string value);
    void setPriimek(string value);
    .
    .
    .
    };
    

    现在 Oseba.cpp 包含这些函数的所有实现...

    include "Oseba.h"
    Oseba::getIme(){return this->ime;}
    .
    .
    .
    

    同样,为 Naslov 执行此操作,还创建一个包含您声明的枚举的 Constant.h,然后将此头文件添加到您的每个文件(Oseba.h/Naslov.h)

    最后,您的“main.cpp”将包含这些头文件,您就可以开始了!

    希望这行得通!

    注意:: 我有目的地创建了 getter 设置器,因为访问这样的类变量是一种不好的做法……只需在要访问字段的地方使用这些 getter。干杯!

    【讨论】:

    • 是的,在#ifndef classnameTAG #define classnameTAG .... (code) .... #endif 下声明你的“.h”文件,这样编译器就不会在你扩展时发疯您的代码并多次包含这些文件...
    • 感谢您的回复。它解决了我的一些问题,但我仍然收到一个错误,即 Naslov 未定义。我也更新了这个问题,所以如果你有时间,我需要更多帮助。
    • 在 Oseba.h 文件中添加 Naslov.h 文件。或者干脆添加“class Naslov;”在 Oseba.h 中声明 Oseba 类之前 所以简而言之,如果你使用一个类对象作为其他类的成员,你需要做我提到的。
    【解决方案2】:
    class Naslov {
      public:
        string ulica;
        string posta;
        int postna_stevilka;
        TIP_NASLOVA tip;
    };
    
    class Oseba {
      public:
        string ime;
        string priimek;
        int starost;
        Naslov naslov;
    };
    

    class 的内容默认为private。您必须明确定义哪些部分是public

    【讨论】:

    • 我知道...抱歉,如果我不够清楚,但我必须使用单独的文件(每个类都使用 .h 和 .cpp)
    【解决方案3】:

    只需将每个struct替换为class并将所有成员标记为公开,因为structclass的区别在于,如果不添加访问标签(例如:public,@987654328 @, protected),然后对于结构,您拥有所有具有公共访问权限和私有访问权限的项目:

    struct Naslov {
        string ulica;
        string posta;
        int postna_stevilka;
        TIP_NASLOVA tip;
    };
    

    将是:

    class Naslov {
    public:
        string ulica;
        string posta;
        int postna_stevilka;
        TIP_NASLOVA tip;
    };
    

    UPD: 在您的情况下,无需在 .cpp 文件中执行任何操作。通常,在 .h 文件中,您需要定义类接口:为您的类的用户声明公共方法,为您提供一些帮助的私有方法等等。在你的情况下没有任何类方法,所以你不能将“无”移动到 .cpp 文件(我撒谎 - 有可能 - 你有类成员,所以你可以使用 pimpl idiom,但这不是你的情况)。 但是如果你真的想创建 .cpp 文件,你可以为这两种情况定义类构造函数(在头文件中 - 使用Include guard)。对于Naslov

    // .h file
    #ifndef NASLOW_H_
    #define NASLOW_H_
    
    class Naslov
    {
    public:
        Naslov();
        Naslov(const string& u, const string& p, int p, TIP_NASLOVA t);
    
    public:
        string ulica; // use google translate: улица -> street
        string posta;
        int postna_stevilka;
        TIP_NASLOVA tip;
    };
    #endif
    
    // .cpp file
    #include "Naslov.h"
    Naslov::Naslov(const string& u, const string& p, int ps, TIP_NASLOVA t)
        : ulica(u)
        , posta(p)
        , postna_stevilka(ps)
        , tip(t)
    {
    }
    
    Naslov::Naslov()
        : ulica()
        , posta()
        , postna_stevilka()
        , tip(STALNI)
    {
    }
    

    【讨论】:

    • 我知道...抱歉,如果我不够清楚,但我必须使用单独的文件(每个类都使用 .h 和 .cpp)
    猜你喜欢
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2020-07-17
    • 2011-08-09
    • 2012-09-06
    • 2018-03-27
    相关资源
    最近更新 更多