hi all, me again,
i need to create an array within a function but somehow i'm getting errors. anyone can enlighten me?

string TravelVoucher::exchange(int i)
{
const int tix = 3;
TravelVoucher::exchItems[tix]={"Dinner for TWO at BestFood Restaurant", "Buffet for TWO at EatTillDrop", "Ticket for TWO for Singapore Flyer", "High Tea for TWO at TeaForYou"};
	return TravelVoucher::exchItems[i];
}

Recommended Answers

All 9 Replies

How is that array declared in the class? Array's can't be initialized like that after they have been declared. After the array has been declared that is has to be initialized the hard way, like below.

TravelVoucher::exchItems[0]="Dinner for TWO at BestFood Restaurant";
TravelVoucher::exchItems[1]= "Buffet for TWO at EatTillDrop";

TravelVoucher::exchItems[2]= "Ticket for TWO for Singapore Flyer";
TravelVoucher::exchItems[3]= "High Tea for TWO at TeaForYou";

If it was not originally declared in the class, then try this:

const char* array[]={"Dinner for TWO at BestFood Restaurant", "Buffet for TWO at EatTillDrop", "Ticket for TWO for Singapore Flyer", "High Tea for TWO at TeaForYou"};

return array[i]; // this is dangerous if the value of i > 3

is it possible to put a constructor into an array?

k the qns given to me is to either exchange/transfer the compnay variable inside Travelvoucher. i know how to do it with an array, but not with using the constructor object. any advise? as usual i'm limited by the qns.

#include "STDAFX.H" 
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// declaration section
class Transfer
{
protected:
	string Name;

public:
	void transfer();
	void xfer(string);
};

class Exchange
{
protected:
	string exchItems[4];
public:
    void exchange();
	string exchange(int);
};

class Voucher
{
protected:
	string vnumber; 
	string company;
	double amount;

public:
	Voucher(string, string, double);
	void displayVoucher();
};

class TravelVoucher : public Voucher, public Transfer, public Exchange
{
  protected:
    string expiryDate;
  public:
    TravelVoucher(string, string, double , string);        // constructor
	void displayVoucher();
    void transfer(string);  
	string exchange(int);
};
// implementation section

string TravelVoucher::exchange(int choice)
{
TravelVoucher::exchItems[0]="Dinner for TWO at BestFood Restaurant";
TravelVoucher::exchItems[1]= "Buffet for TWO at EatTillDrop";
TravelVoucher::exchItems[2]= "Ticket for TWO for Singapore Flyer";
TravelVoucher::exchItems[3]= "High Tea for TWO at TeaForYou";
return TravelVoucher::exchItems[choice];
}
void Voucher::displayVoucher()
{
	cout << "Voucher Number = " << vnumber << endl;
	cout << "Company =  " << company<< endl;
	cout << "Amount = " <<setprecision(2)<< fixed << amount << endl;
	cout<< endl;
}
Voucher::Voucher(string Vnumber, string Company, double Amount)
{
  vnumber = Vnumber;
  company = Company;
  amount = Amount;
}
TravelVoucher::TravelVoucher(string Vnumber,string Company, double Amount, string ExpiryDate):Voucher( Vnumber,  Company,  Amount)
{
  expiryDate = ExpiryDate;
}
void TravelVoucher::displayVoucher()
{	
	cout << "Voucher Number = " << vnumber << endl;
	cout << "Company =  " << company<< endl;
	cout << "Amount = " <<setprecision(2)<< fixed << amount << endl;
	cout << "Expiry Date =  " << expiryDate<< endl;
	cout<< endl;
}


int main()
{
int choice;
Voucher v1=("T001", "SIM University",100.00);
Voucher v2= ("T002", "SIA Flying School",200.00);
  TravelVoucher Tv1("T003", "SIM University",150.00, "12 April 2009"); 
  TravelVoucher Tv2("T004", "Jubilee Company",250.00, "12 June 2009"); 
  v1.displayVoucher();
  v2.displayVoucher();
  Tv1.displayVoucher();
  Tv2.displayVoucher();

		cout<<"Enter choice to exchange travel voucher for:1,2,3,4";
	  cin>> choice;
	 // TravelVoucher::exchange(choice);
  cin.ignore();
  cin.ignore();

  return 0;
}

is it possible to put a constructor into an array?

Yes, take a look here for an example: http://www.daniweb.com/forums/post847675-7.html

The size specifier for the array (base* [[b]3[/b]]) is not necessary, but it will make sure the array is of size 3 of course.

Comment on Orignal Post:
When defiing an array in C++: A[n] , the subscript n can go from 0 to n-1
Both the OP and Ancient have not considered this. You have defined array as: A[3] but you are using n from 0 to 3

>>is it possible to put a constructor into an array?
I don't know what this sentence means, but it gives me a hint that the OP is trying to say if he can code the constructor to initialize the array? Yes, he can.

IMHO, you should retain your latest code intact, its fine according to me except that you should move the definition of the array, on line 21 inside the function exchange since you don't need that array outside that function.

i'm wondering now how to exchange the company attribute inside the vouchers using the exchange function.

so far everything is running, but it doesn't really make sense if i'm trying to change the company attribute.

My first piece of advice is to not have the same name for a class, a class member variable and a class method.

Since the TravelVoucher class is derived publically from the Voucher class and company is a protected variable within the Voucher class then the public method exchange() within the TravelVoucher class has full and unfettered access to the the company variable of all it's objects. The problem I see is that as declared the exhange() function accepts an int to act as an index in the array of options provided but there is no clear way to change the company's name per user's descretion. You could pass to parameter to exchange, one of which is a new string value to assign to the company variable, or you could ask for user input from within the function itself.

hi all, thx for all the advice and tips. thanks to u guys i've managed to finish most of the assignment.

hi guys,is it possible to create a vector that puts both types of vouchers inside?

using something like

#include <vector>

vector<Voucher *>vouchers(4)

this is more of just for fun. i had use vectors for objects with same data but never tried before to put 2 different kind of objects into the same vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.