【问题标题】:C++ getting values from parent and grandparent that were initialized in mainC ++从在main中初始化的父母和祖父母获取值
【发布时间】:2014-12-14 20:33:32
【问题描述】:

我是 C++ 初学者,遇到以下问题。我有三个班级:祖父母,父母和孩子。思路是这样的

#include <iostream>
#include <stdlib.h>
#include <string.h>

class Book
{   protected: 
        long int number; 
        char author[25];
        int year;
        bool lent;

        void setLent(bool x);
        bool getLent(); 
    public: 
        Book(long int n, char a[25], int j, bool x);
        long int getNr();
        int getYear();
        void print();
        };


class UBook: public Book
{   protected: 
        int categ;
        char country[15];
    private:
        int for_age;   
    public: 
        UBook(int t, int k, char l[15]);
        void setAge(int a);
        int getAge();
        };      


class PicBook: public UBook
{   private:
        static const int for_age=6;
    public: 
        PicBook(long int n, char a[25], int j,int k, char l[15]);
        };      

Book::Book(long int n, char a[25], int j, bool x)
    {number=n;
    strncpy(author, a, 25);
    year=j;
    lent=x;}

long int Book::getNr()
    {return number; }

int Book::getYear()
    {return year;}

void Book::setLent(bool x)
    {lent=x;}

bool Book::getLent()
    {return lent;}

void Book::print()
    {
    std::cout << "Booknumber: " << number << std::endl;
    std::cout << "Author: " << author << std::endl;
    std::cout << "Year: " << year << std::endl;
    if (lent==0)
    std::cout << "Lentiehen [ja/nein]: nein" << std::endl;
    else
    std::cout << "Lentiehen [ja/nein]: ja" << std::endl;
    }

UBook::UBook(int t, int k, char l[15]): Book(number, author, year, lent)
    {for_age=t;
    categ=k;
    strncpy(country, l, 15);
    }

void UBook::setAge(int a)
    {for_age = a;} 

int UBook::getAge()
    {return for_age;}      


PicBook::PicBook(long int n, char a[25], int j,int k, char l[15]): UBook(for_age, categ, country)
    {
    std::cout << "Booknumber: " << number << std::endl;
    std::cout << "Author: " << author << std::endl;
    std::cout << "Year: " << year << std::endl;
    std::cout << "For age: " << for_age << std::endl;
    std::cout << "Categorie: " << categ << " [Bildband]" << std::endl;
    std::cout << "Country: " << country << std::endl;
    }


int main()
{   
    PicBook somebook(356780, "test", 2010, 4, "France");

    system("pause");
    return 0;
}

但是,如果我在“child”中进行测试输出,则会出现一些奇怪的输出:

Book Nr: 4283296
Author: ð■(
Year: 1988844484
For age: 6 /*(the only correct output)*/
Categorie: 2686760 [Bildband]
Country: ♠\A
Press any key to continue . . .

所以我的参数没有正确传递。前 3 个参数是祖父类的成员,后两个是父类的成员。我认为“孩子”中的构造函数可能存在问题。

提前感谢您的帮助!

【问题讨论】:

  • child::other 隐藏成员变量 parent::otherchild::other 在您将其传递给 parent 时未初始化,从而导致未定义的行为。将grandparent::test 传递给grandparent 的构造函数也是如此。帮自己一个忙,获得good book on C++
  • 这不是 C++ 代码。 Class 是什么?你的意思是class?类声明也需要以; 结束。如果您正在寻求帮助,请复制并粘贴有问题的代码; 不要重新输入。
  • 那么可能的解决方案是什么?对不起...我是真正的初学者
  • childother 初始化parent,而不是用abchild 从不使用 ab,因此从不使用 16 应该不是什么秘密。
  • 我按要求添加了真实代码

标签: c++ parameter-passing parent grandchild


【解决方案1】:

很高兴您正在学习 C++。最佳语言;)从您发布的代码中,您并不清楚您想要什么,因此我扩展了您的示例,使其更加清晰。我将protected 更改为public,因此您可以直接使用cout 打印数据以查看屏幕上的输出。此外,我在每个类中添加了一个变量。与类同名的函数称为“构造函数”,它们的输入参数通常用于设置类成员(test,...)。试试这个代码,如果您对结果满意,请告诉我们。

#include <iostream>
#include <stdlib.h>

using namespace std;

class grandparent
{
public:
    int test;
public:
    grandparent(int a): test(a){};
};

class parent: public grandparent
{
public:
    int other;
public:
    parent(int a, int b): grandparent(a), other(b) {};
};

class child: public parent
{
public:
    int child_other;
public:
    child(int a, int b, int c): parent(a, b), child_other(c) {};
};

int main()
{
    child sometest(1,6, 7);

    cout << sometest.test << endl;
    cout << sometest.other << endl;
    cout << sometest.child_other << endl;

    return 0;
}

希望这会有所帮助。

【讨论】:

  • 当您在 C++ 中学习继承时,最好在所有构造函数和析构函数中添加一个 cout。它可以让您很好地了解正在发生的事情。
  • @WolfCoder:感谢您的回答。我现在把真正的代码放在
  • 我仍然不确定您的目标,因此很难看到任何逻辑错误。但是UBook 的构造函数肯定有一些需要纠正的地方。冒号: Book(number, author, year, lent) 之后的部分是调用基类的构造函数,并作为参数传递基类本身的变量。基本上你正在用它自己定义一个变量number。结果将是一个随机数。而是将您作为输入参数获得的变量传递给 UBook 构造函数(我认为您需要更多参数)。
  • 其实我只需要这些:PicBook somebook(356780, "test", 2010, 4, "France") = (Booknumber, author, year, category, country)。我只需要正确实例化这个表达式。但是,我在构造函数中的测试 cout 中看到参数未正确传递,输出如下:Book Nr:4283296,作者:ð■(,Jahr:1988844484,年龄:6(唯一正确的输出) , 类别: 2686760 [Bildband], 国家: ♠\A, 按任意键继续...
【解决方案2】:

我的天啊,我找到了解决方案,而且确实非常简单。我只需要更改我的 std::couts:

PicBook::PikBook(long int n, char a[25], int j,int k, char l[15]): UBook (for_age, categ, country)
    {
    std::cout << "Book Nr:: " << n << std::endl;
    std::cout << "Author: " << a << std::endl;
    std::cout << "Year: " << j << std::endl; 
    std::cout << "Category: " << k << " [Picture Book]" << std::endl;
    std::cout << "country: " << l << std::endl;
    }

【讨论】:

    猜你喜欢
    • 2015-08-30
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    相关资源
    最近更新 更多