GreatBloke
Programmer
Hi,
I'm trying to create a very small and simple template class but am having problems.
here is my header file code...
//BookStock.h
#ifndef BookStock
#define BookStock
template< class T >
class BookStocks
{
private:
int numInStock;
T price; //price either int or float
char author[20], title[20];
public: //author//title //Number //price
BookStocks(char[], char[], int, T);
T ChangePrice( T );//recieves amount to change price by and returns new price
void Display();
};
here is my BookStock.cpp file...
//BookStocks.cpp - the base class or superclass
#include <iostream>
#include <iomanip>
#include "bookstock.h"
template< class T >
BookStocks< T >::BookStocks(char auth[], char tit[] , int num, T p)
{
author = auth;
title = tit;
numInStock = num;
price = p;
}
template< class T >
void BookStocks< T >:
isplay()
{
cout << "Details of bookstock are as follows...\n\n";
cout << "Author: " << author << endl
<< "Title: " << title << endl
<< "Number in Stock: " << numInStock << endl
<< "Price in Euros: " << priceEuro << endl;
}
template< class T >
T BookStocks< T >::ChangePrice( T )
{
//p is the amount to be altered by either negative or positive
if (T > 0) //positive
price += T;
else //negative
price -= T;
return price;
}
and here is my main.cpp file...
#include <iomanip>
#include "Bookstock.h"
int main()
{
cout << "Enter the author: " << endl;
char a[20];
cin >> a;
cout << "Enter the title: " << endl;
char ti[20];
cin >> ti;
cout << "Enter the number in stock: " << endl;
int n;
cin >> n;
cout << "Do you want to enter price in..." << endl
<< "1. Sterling" << endl
<< "2. Euro" << endl;
int option;
cin >> option;
if (option == 1)
{
cout << "Enter the price in sterling (float): " << endl;
float fp;
cin >> fp;
// BookStocks< float > floatBook(a, ti, n, ps);
// floatBook bs1(a, ti, n, ps);
// bs1.Display;
BookStocks bs1(a, ti, n, T fp);
}
else
{
cout << "Enter the price in euros (int): " << endl;
int ip;
cin >> ip;
}
return 0;
}
#endif
As you can see, I'm having trouble creating a new instance of class bookstock with the T parameter.
Any help with this would be excellent..
Thanks
John
I'm trying to create a very small and simple template class but am having problems.
here is my header file code...
//BookStock.h
#ifndef BookStock
#define BookStock
template< class T >
class BookStocks
{
private:
int numInStock;
T price; //price either int or float
char author[20], title[20];
public: //author//title //Number //price
BookStocks(char[], char[], int, T);
T ChangePrice( T );//recieves amount to change price by and returns new price
void Display();
};
here is my BookStock.cpp file...
//BookStocks.cpp - the base class or superclass
#include <iostream>
#include <iomanip>
#include "bookstock.h"
template< class T >
BookStocks< T >::BookStocks(char auth[], char tit[] , int num, T p)
{
author = auth;
title = tit;
numInStock = num;
price = p;
}
template< class T >
void BookStocks< T >:
{
cout << "Details of bookstock are as follows...\n\n";
cout << "Author: " << author << endl
<< "Title: " << title << endl
<< "Number in Stock: " << numInStock << endl
<< "Price in Euros: " << priceEuro << endl;
}
template< class T >
T BookStocks< T >::ChangePrice( T )
{
//p is the amount to be altered by either negative or positive
if (T > 0) //positive
price += T;
else //negative
price -= T;
return price;
}
and here is my main.cpp file...
#include <iomanip>
#include "Bookstock.h"
int main()
{
cout << "Enter the author: " << endl;
char a[20];
cin >> a;
cout << "Enter the title: " << endl;
char ti[20];
cin >> ti;
cout << "Enter the number in stock: " << endl;
int n;
cin >> n;
cout << "Do you want to enter price in..." << endl
<< "1. Sterling" << endl
<< "2. Euro" << endl;
int option;
cin >> option;
if (option == 1)
{
cout << "Enter the price in sterling (float): " << endl;
float fp;
cin >> fp;
// BookStocks< float > floatBook(a, ti, n, ps);
// floatBook bs1(a, ti, n, ps);
// bs1.Display;
BookStocks bs1(a, ti, n, T fp);
}
else
{
cout << "Enter the price in euros (int): " << endl;
int ip;
cin >> ip;
}
return 0;
}
#endif
As you can see, I'm having trouble creating a new instance of class bookstock with the T parameter.
Any help with this would be excellent..
Thanks
John