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