-1

我是 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;
}

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

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

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

提前感谢您的帮助!

4

2 回答 2

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;
}

希望这可以帮助。

于 2014-10-19T22:17:38.853 回答
1

天哪,我找到了解决方案,确实很简单。我只需要更改我的 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;
    }
于 2014-10-20T22:34:00.763 回答