-1

I am a C++ beginner and I have the following problem. I have three classes: a grandparent, a parent and a child. The idea is like this

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

However, if I do a test output in "child" some weird output:

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

So my parameters are not passed correctly. The first 3 parameter are members from the grandparent class the last two from the parent class. I think there is a problem maybe with my constructor in "child".

Thanks for your help in advance!

Martine
  • 11
  • 6
  • `child::other` shadows the member variable `parent::other`. `child::other` is uninitialized when you pass it to `parent` resulting in undefined behavior. Same for passing `grandparent::test` to the constructor of `grandparent`. Do yourself a favor and get a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Captain Obvlious Oct 19 '14 at 21:58
  • 1
    This isn't C++ code. What is `Class`? Do you mean `class`? Class declarations also need to be terminated with a `;`. If you're asking for help, copy and paste the problematic code; do *not* retype it. – jamesdlin Oct 19 '14 at 22:00
  • so what could be a possible solution? I am sorry... I am real beginner – Martine Oct 19 '14 at 22:01
  • `child` initializes `parent` with `other`, not with `a` or `b`. `child` never does anything with `a` or `b`, so it should be no mystery that `1` and `6` are never used. – jamesdlin Oct 19 '14 at 22:03
  • I added the real code as requested – Martine Oct 19 '14 at 23:17

2 Answers2

1

nice that you are learning C++. Best language ;) From the code you posted it wasn't really clear what you wanted, so I expanded your example so it is a bit more clear. I changed protected to public so you can print data with cout directly to see the output on the screen. Additionally I added a variable in each of the classes. The functions that have the same name as the class are called "constructors" and their input parameters are usually used to set the class members (test, ...). Try this code and let us know if you are happy with the result.

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

Hope this helps.

WolfCoder
  • 127
  • 8
  • When you're learning inheritance in c++ its a good idea to put a cout in all your constructors and destructors. It gives you a good idea what is going on. – Jason Ball Oct 19 '14 at 22:31
  • @WolfCoder: thanks for your answer. I put now the real code in – Martine Oct 19 '14 at 23:19
  • I am still unsure about your goal so it's hart to see any logical errors. But there is definitely something to correct in constructor of `UBook`. The part after the colon `: Book(number, author, year, lent)` is calling constructor of your base class, and as parameters you pass the variables of your base class itself. Basically you are defining a variable `number` with it self. The result will be a random number. Instead pass a variable that you get as an input parameter to `UBook` constructor (I think you want more parameters). – WolfCoder Oct 20 '14 at 07:04
  • Actually, I need just these: PicBook somebook(356780, "test", 2010, 4, "France") = (Booknumber, author, year, category, country). I need just to correctly instantiate this expression. However, I see with my test cout in the constructor that the parameters are not correctly passed, The output is as follows: Book Nr: 4283296, Author: ð■(, Jahr: 1988844484, For age: 6 (the only correct output), Categorie: 2686760 [Bildband], Country: ♠\A, Press any key to continue . . . – Martine Oct 20 '14 at 17:46
1

OMG I found the solution and indeed it was soooo simple. I simply had to change my 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;
    }
Martine
  • 11
  • 6