我是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”中存在问题。
提前感谢您的帮助!
答案 0 :(得分:0)
protected
更改为public
,因此您可以直接使用cout
打印数据,以查看屏幕上的输出。另外,我在每个类中添加了一个变量。与该类同名的函数称为&#34; constructors&#34;它们的输入参数通常用于设置类成员(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;
}
希望这有帮助。
答案 1 :(得分:0)
OMG我找到了解决方案,确实太简单了。我只需要改变我的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;
}