exercise help

This is my answer to exercise in "c++ primer plus ch14 ex2".
What's wrong with the following code?
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
#include "Wine.h"
#include <iostream>
using namespace std;

Wine::Wine(const char * l, int y, const int yr[], const int bot[]) :
		string(l), m_iSize(y), PairArray(ArrayInt(yr, y), ArrayInt(bot, y)) {
}

Wine::Wine(const char * l, int y) :
		string(l), m_iSize(y), PairArray(ArrayInt(y), ArrayInt(y)) {

}

Wine::~Wine() {

}
void Wine::Show() const {
	cout << (const string &)(*this) << endl << "year \t bottles\n";
	for (int i = 0; i < m_iSize; i++) {
		cout << PairArray::first[i] << "\t" << PairArray::second[i] << endl;
	}
	return;
}
void Wine::GetBottles() {
	for (int i = 0; i < m_iSize; i++) {

		cout << "Year, please: ";
		cin >> PairArray::first[i];
		cout << "Bottles, please: ";
		cin >> PairArray::second[i];
	}
	cout << "job done!\n";
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef WINE_H_
#define WINE_H_
#include <valarray>
#include <string>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArray;

class Wine : private PairArray, private std::string{
public:
	Wine(const char * l, int y);
	Wine(const char * l, int y, const int yr[], const int bot[]);
	virtual ~Wine();
	void Show() const;
	void GetBottles();
private:
	int m_iSize;


};

#endif /* WINE_H_ */ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "Wine.h"
int main(){

	using namespace std;

	const int YRS=3;
	int y[YRS] = {2000,2001,2003};
	int b[YRS] = {1,2,3};

	Wine one("type one", YRS, y, b);
	one.Show();
	Wine two("type two", 2);
	two.GetBottles();
	two.Show();


	return 0;
}
closed account (o3hC5Di1)
Hi there,

Could you please be a bit more specific on what your code is supposed to do and what errors or unsuspected behaviour you are seeing?

I don't personally own that book you refer to and it's a bit of a hassle to test multifile programs.

Thanks!

All the best,
NwN
Sorry for that, This code just for practicing private inherit
GetBottles() provide user input interface
Show() display the object

I encounter some compiler error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
make all 
Building file: ../src/Wine.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Wine.d" -MT"src/Wine.d" -o"src/Wine.o" "../src/Wine.cpp"
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/i386-redhat-linux/bits/c++config.h:43: error: expected constructor, destructor, or type conversion before ‘namespace’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/i386-redhat-linux/bits/c++config.h:47: error: ‘__gnu_debug_def’ is not a namespace-name
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/i386-redhat-linux/bits/c++config.h:47: error: expected namespace-name before ‘;’ token
../src/Wine.h: In constructor ‘Wine::Wine(const char*, int, const int*, const int*)’:
../src/Wine.h:23: warning: ‘Wine::m_iSize’ will be initialized after
../src/Wine.cpp:13: warning:   base ‘std::pair<std::valarray<int>, std::valarray<int> >’
../src/Wine.cpp:12: warning:   when initialized here
../src/Wine.h: In constructor ‘Wine::Wine(const char*, int)’:
../src/Wine.h:23: warning: ‘Wine::m_iSize’ will be initialized after
../src/Wine.cpp:17: warning:   base ‘std::pair<std::valarray<int>, std::valarray<int> >’
../src/Wine.cpp:16: warning:   when initialized here


Nothing is wrong with the code. Just the order that you have it in.
In the
Wine.h file should be like this.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream> // Have all your includes in the header.
#include <valarray>
#include <string>
using namespace std;



typedef std::valarray<int> ArrayInt;   // This is  good
typedef std::pair<ArrayInt, ArrayInt> PairArray; // And so is this

// FIRST OFF BUILD YOUR CLASS //
class Wine : private PairArray, private std::string{
public:
	Wine(const char * l, int y);
	Wine(const char * l, int y, const int yr[], const int bot[]);
	virtual ~Wine();
	void Show() const;
	void GetBottles();
private:
	int m_iSize;


};

// THEN ALSO IN THE HEADER FILE ADD YOUR MEMBERS AFTER THE CLASS HAS BEEN BUILT //

Wine::Wine(const char * l, int y, const int yr[], const int bot[]) :
		string(l), m_iSize(y), PairArray(ArrayInt(yr, y), ArrayInt(bot, y)) {
}

Wine::Wine(const char * l, int y) :
		string(l), m_iSize(y), PairArray(ArrayInt(y), ArrayInt(y)) {

}

Wine::~Wine() {

}
void Wine::Show() const {
	cout << (const string &)(*this) << endl << "year \t bottles\n";
	for (int i = 0; i < m_iSize; i++) {
		cout << PairArray::first[i] << "\t" << PairArray::second[i] << endl;
	}
	return;
}
void Wine::GetBottles() {
	for (int i = 0; i < m_iSize; i++) {

		cout << "Year, please: ";
		cin >> PairArray::first[i];
		cout << "Bottles, please: ";
		cin >> PairArray::second[i];
	}
	cout << "job done!\n";
}



Then in your .cpp file you don't need to include the <iostream>
But it looks fine. By the looks of your code you choose your wine. Then when its all out that wine program closes. Im not sure if that is what you want it to do but thats what it does.

Good luck.
Last edited on
Yes! nothing was wrong. I just create a new project. anything goes right, bravo!
Topic archived. No new replies allowed.