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