【发布时间】: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