C++ getting values from parent and grandparent that were initialized in main

1k Views Asked by At

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!

2

There are 2 best solutions below

0
Martine On BEST ANSWER

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;
    }
4
WolfCoder On

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.