为什么不更改该变量的值?

时间:2020-03-14 17:33:15

标签: c++ class object

我必须更改此程序,以便对象星号的数据实际显示名称和nr_indeksu,但我被卡住了。有什么帮助吗?对变量名称感到抱歉,但在我的话中,它们的含义是相信我和混乱的一切。

#include <iostream>
using namespace std;
class student
{
private:
string imie_nazwisko_ = "NO_NAME";
unsigned int nr_indeksu_ = 0;
public:
student(string imie_nazwisko, unsigned int nr_indeksu);
void printDane()
{
cout << " Metoda printDane klasy bazowej" << endl;
cout << " imie nazwisko " << imie_nazwisko_ << endl;
cout << " nr indeksu " << nr_indeksu_ << endl;
}
};
class starosta : public student
{
public:
string imie_nazwisko_ = "NO_NAME";
unsigned int nr_indeksu_ = 0;
string email_ = "no@noemail";
void printDane()
{
cout << " Metoda printDane klasy starosta" << endl;
cout << " imie nazwisko " << imie_nazwisko_ << endl;
cout << " nr indeksu " << nr_indeksu_ << endl;
cout << " email "<< email_<<endl;
}
starosta(string imie_nazwisko, unsigned int nr_indeksu, string email);
};
starosta::starosta(string imie_nazwisko, unsigned int nr_indeksu, string email) :student(imie_nazwisko, nr_indeksu), email_(email)
{
cout << "Tworzenie obiektu klasy starosta "<< endl;
}
student::student(string imie_nazwisko, unsigned int nr_indeksu) : imie_nazwisko_(imie_nazwisko)
{
nr_indeksu_ = nr_indeksu;
cout << "Tworzenie obiektu klasy student" <<endl;
}
int main()
{
student stud("Jan Kowalski",7);
stud.printDane();
starosta star("Aleksandra Nowak",999,"mail@nomail.dot");
cout << "Dane:" << star.imie_nazwisko_ << " " << star.nr_indeksu_ << endl;
star.printDane();
}

1 个答案:

答案 0 :(得分:1)

您在类starosta中声明了已经在类student中存在的新成员。然后,使用其构造函数初始化student的成员,并尝试在其方法starosta中打印出printDane的成员。您应该从类starosta中删除重复的名称。

class starosta : public student
{
 public:
  string imie_nazwisko_ = "NO_NAME";  // duplicates student::imie_nazwisko_
  unsigned int nr_indeksu_ = 0; // duplicates student::nr_indeksu_