Вопрос

I am trying to convert this code from using structs into using classes. The problem I am having is, I don't know how I should declare, initialize etc. classes in order for them to work in the same way they did in the code posted below. I have to use separate files for each class (i.e. class1.h, class1.cpp, class2.h, class2.cpp, main.cpp) and I have no idea how to achieve the same effect as I did with structs. How can I acomplish nesting a class inside a class?

Another question I have is, how should I handle enum lists? Where and when to declare/initialize them?

Thanks a lot guys! Hope I was clear.. I tried googling it but I found nothing useful really.

Here's the code:

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

EDIT: I know about public, protected and private... sorry if I wasn't clear enough, but I have to use separate files (both .h and .cpp for each class) I am familiar with how to make this work using a single file. The problem appears when I try to split classes in their own header and cpp files.

EDIT 2: Ok I see my question was very unclear. Excuse my lack of effort. I will simplify my problem.

I am trying to make a class member of another class. So my files (at the moment) look like this:

PersonalInfo.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

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

Location.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
Это было полезно?

Решение

Simple,

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

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

Will become

say, Oseba.h first,

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

Now Oseba.cpp with all the implementations of these functions...

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

Similarly, do this for Naslov, also, create a Constant.h which will include the enum you have declared, and then add this header file to each of ur files (Oseba.h/Naslov.h)

Finally, your "main.cpp" will include these headers files and you'll be good to go!

Hope this works!

NOTE:: i have purposefully created the getter setters as it is a bad practice to access class variables like that... just use these getters where you want to access the fields. Cheers!

Другие советы

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

class Oseba {
  public:
    string ime;
    string priimek;
    int starost;
    Naslov naslov;
};

Contents of class are private by default. You have to define which parts are public explicitly.

Just replace every struct to class and mark all members as public, because the difference beatween struct and class is, if you do not add access tag(such as: public, private, protected), then for structs you have all items with public access and wtith private access for classes:

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

Will be:

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

UPD: No need to do something in .cpp file in your case. Ususally, in .h file you need to define class interface: declare public methods for users of your class, private methods for some help for you and so on. There is no any class methods in your case, so yo can't move "nothing" to .cpp file(I lied - it's possible - you have class members, so you can use pimpl idiom, but it's not your case). But if you really want create .cpp file you can define class constructor for both cases(In header file - use Include guard). For 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)
{
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top