【问题标题】:Why is it not changing values of that variables? [closed]为什么它不改变这些变量的值? [关闭]
【发布时间】:2020-03-14 17:33:15
【问题描述】:

我必须更改此程序,以便对象星的数据实际显示名称和 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();
}

【问题讨论】:

  • 请在您的代码中使用缩进,这将使每个人都更容易阅读、理解和改进它。

标签: c++ class object


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多