inheritance problem.

Hi,
i have a problem when running a code like this: ba(11,"Title","Name");
is there a mistake with my code ?

LibraryItem.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef _LIBRARYITEM_H_
#define _LIBRARYITEM_H_

#include <iostream>
#include <string>
#include <time.h>

using namespace std;
class LibraryItem {
public:
	//Consturcor
	LibraryItem(long cNum = 0, string _bookName = "", int days = 0): catalogNumber(cNum), bookName(_bookName), maxLoaningDays(days), isLoaned(false){};
	virtual long getCatalogId() const { 
		return catalogNumber;
	}
	virtual void setLoanStatus(bool st){
		isLoaned = st;
	}
	virtual bool getLoanStatus(){
		return isLoaned;
	}
	virtual void setLoaningDate(time_t today){
		loaningDate = today;
	}
	virtual time_t getLoaningDate() const{
		return loaningDate;
	}
	virtual int getMaxDays() const{
		return maxLoaningDays;
	}

	//Distructor
	virtual ~LibraryItem(){}
private:
	long catalogNumber; 
	string bookName;
	bool isLoaned;
	const int maxLoaningDays;
	time_t loaningDate;
};	
#endif 


Book.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef _BOOK_H_
#define _BOOK_H_

#include <iostream>
#include <string>
#include <time.h>
#include "LibraryItem.h"

using namespace std;
class Book: public LibraryItem {
public:
	//Consturcor
	Book(long cNum = 0, string _bookName = "", string _authorName = ""): authorName(_authorName) { 
		LibraryItem(cNum,_bookName,14);
	}
	//Distructor
	virtual ~Book(){}
private:

	string authorName;

};
#endif 
What is your problem?
it "builds" all the Books with the same default values, like:
long catalogNumber == 0
string bookName == ""
bool isLoaned == false
const int maxLoaningDays == 0

1
2
3
Book(long cNum = 0, string _bookName = "", string _authorName = ""): authorName(_authorName) { 
		LibraryItem(cNum,_bookName,14);
	}

It's wrong.
It should be
1
2
3
Book(long cNum = 0, string _bookName = "", string _authorName = ""): LibraryItem(cNum,_bookName,14),authorName(_authorName) { 
		
	}

The base part of a derived class should be initialized in the constructor's initializer list.
Last edited on
Well, first of all you can cut the leading underscores out - your compiler can deduct what is a member and what is an argument in your initialization list.

Next - Initializer lists are part of the implementation. Those don't belong into the class declaration.

Last: You actual problem is that you call the LibraryItem constructor inside of the constructor body - you need to call it in the initialization list. Preferably first.
Last edited on
closed account (1vRz3TCk)
Try something like:
1
2
3
4
5
6
Book(long cNum = 0, string _bookName = "", string _authorName = "")
        : LibraryItem(cNum,_bookName,14), 
        authorName(_authorName) 
{ 
		
}
thnx guys, adding LibraryItem(cNum,_bookName,14) to the initialization list resolved the problem.
thnx you very much guys.
Topic archived. No new replies allowed.