Witam mam problem z zad.1 z rozdzialu 14 z ksiazki szkola programownia C++. Poniżej zamiesczam kod:
Plika naglowkowy:
#ifndef WINEC_H_
#define WINEC_H_
#include <valarray>
#include <iostream>
#include <string>
#include <cstring>
template <class Tl, class T2>
class Pair
{
private:
Tl Year;
T2 Bottles;
public:
Tl & first () ;
T2 & second();
Tl first() const{return Year;}
T2 second() const {return Bottles;}
Pair(const Tl & aval, const T2 & bval) : Year (aval), Bottles(bval)
{}
Pair() {}
};
template<class Tl, class T2>
Tl & Pair<Tl, T2>::first()
{
return Year;
}
template<class Tl, class T2>
T2 & Pair<Tl,T2>::second()
{
return Bottles;
}
class Wine
{
private:
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
std::string label;
int count;
PairArray ilosc;
public:
Wine():ilosc()
{}
// Inicjalizuje skladowe label parametrem l, liczbe lat parametrem y,
// roczniki parametrem yr[], skladowe bottles parametrem bot[]
Wine(const char * l, int y, const int yr[], const int bot[]);
//Inicjalizuje skladowe label parametrem l. liczbe lat parametrem y,
// tworzy tablice obiektów o dlugosci y
Wine(const char * l, int y);
void GetBottles() const;
void Show() const;
std::string & Label() ;
};
Wine::Wine(const char * l, int y, const int yr[], const int bot[]):label(l),count(y),ilosc(ArrayInt(y),ArrayInt(y))
{
for (int i=0;i<count;i++)
{
ilosc.first()[i]=yr[i];
ilosc.second()[i]=bot[i];
}
}
Wine::Wine(const char * l, int y):label(l),count(y)//,ilosc(ArrayInt(y),ArrayInt(y))
{}
void Wine::GetBottles() const
{
using std::cout;
using std::cin;
cout<<"Podaj dane o winie"<<label<<"dla"<<count<<"rocznikow\n";
for (int i=0;i<count;i++)
{
cout<<"Podaj rocznik ";
cin>>ilosc.first()[i];
cout<<"Podaj ilosc butelek ";
cin>>ilosc.second()[i];
}
}
void Wine::Show()const
{
std::cout<<"Wino"<<label<<"\n";
std::cout<<"Rocznik Butelki\n";
for (int i=0;i<count;i++)
{
std::cout<<ilosc.first()[i];
std::cout<<" ";
std::cout<<ilosc.second()[i];
std::cout<<"\n";
}
}
std::string & Wine::Label()
{
return label;
}
#endif
Program testowy:
#include <iostream>
#include "winec.h"
int main (void)
{
using std::cin;
using std::cout;
using std::endl;
cout << "Podaj nazwe wina: ";
char lab[50];
cin.getline (lab, 50);
cout << "Podaj liczbe roczników: ";
int yrs;
cin >> yrs;
Wine holding (lab, yrs) ; // Zapisuje nazwe, liczbe roczników i przekazuje
// informacje o tej liczbie do tablicy
holding.GetBottles () ; // Pobiera dane o roczniku i liczbie butelek
holding.Show () ; // Wyswietla zawartosc obiektu
const int YRS = 3;
int y[YRS] = { 1993, 1995, 1998 };
int b[YRS] = { 48, 60, 72 } ;
// Tworzy nowy obiekt, inicjalizuje za pomoca danych w tablicach y oraz b
Wine more("Gushing Grape Red", YRS, y, b ) ;
more.Show();
//cout << "Laczna liczba butelek wina " << more.Label();
// Uzywa metody Label!)
//<< ": " << more.sum() << endl;
// Uzywa metody sum()
cout << "Koniec\n";
return 0;
}
Mianowicie gdy program pobiera dane z klawiatury to w kolumnie wyswietla mi 0, natomiast gdy pobiera dane z tablicy wszystko dziala jak nalezy. Wytłumaczcie mi co robie nie tak, z góry dziękuje.