Hello folks
I'm trying to do my assignment and I'm really stuck in!

I have to classes..Customer and Pensioner (Customer = Super class and Pensioner = Subclass)
The Pensioner has a PensionNo extra than Customer class.

I have to make a array of Customer class (for 10) and put Customer and Pensioner in it. Then I have to override << operator and print out the values of Customer/Pensioner in a file.

Customer.h

#ifndef CUSTOMER_H
#define CUSTOMER_H

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


class Customer {
	public:
		Customer(string _firstname, string _lastname, string _telephone, int _noofcall);
		friend ostream& operator << (ostream& out, Customer _customer); 

		string getFirstName();
		string getLastName();
		string getTelephone();
		int getNoOfCall();

	protected:
		string	firstName;
		string lastName;
		string telephone;
		int noOfCall;

};


#endif

Pensioner.h

#ifndef PENSIONER_H
#define PENSIONER_H

#include <iostream>
#include <fstream>
#include <string>

#include ".\Customer.h"

using namespace std;


class Pensioner : public Customer {
	
	public:
		string pensionNo;
		Pensioner(string firstname, string lastname, string telephone, int noofcall, string pensionno) ;
		friend ostream& operator << (ostream& out, Customer _customer); 

		string getPensionNo();
};


#endif

The main problem:

if (cmd == 'P')
           {
             string pension; 
             oss >> pension;
             customers[top++] = new Pensioner(first, last, phone, Nofcalls, pension);
           }
           else
             customers[top++] = new Customer(first, last, phone, Nofcalls);
           
         
           break;
         }

A Pensioner instance creates but when it put into the Customers[] it lost its pension value.

It really don't know what should i do...
I'll grateful if you can give me any hints...

Cheers

-------------------------------------------------------------------
Here is the assignment text:
The customer is an ordinary customer. In this case the fields are firstName, lastName, telephoneNo and numberOfCalls. A Customer object should be created and initialised with the values from this line.

The customer is a pensioner. In this case, we have a field P which purely signifies that we are dealing with a pensioner, followed by the pension number. A Pensioner object should be created with the four Customer fields and also the pension number.

In either case, the resulting object should be added to an array of ten Customer pointers called ‘customers. This array should be a private data member of the Manager class

--------------------------------------------------------------------------------------------------

Aside from a lot of poor style (e.g. you should never have "using namespace" at global level in any header file) and not showing important parts of the code (e.g. the declaration of customers), you're pretty close.

I'm guessing that customers is:

Customer* customers[10];

because the new operator returns a pointer. If you were trying to store values you'd have the slicing problem, but pointers are polymorphic.

Now you just need to use polymorphism to have different behavior for each class, which means a virtual member function. Implement your operator<< as:

ostream& operator<< (ostream& out, const Customer& customer)
{
  customer.write_to(out);
  return out;
}

And define write_to as a const virtual function in both Customer and Pensioner. Pensioner's write_to can call Customer::write_to in order to take care of the base class variables before writing its own members.

commented: Great explanation +5

Thanks for reply...
Yes I really noob in C++

actually, I wanna use << operator to write in text file.

ofstream output("CustomerOutput.dat", ios_base::out | ios_base::app);
for (int i = 0; i < top; ++i)
output << *customers[i] << "\n\n";

and I had this for << overriding:

ostream& operator<<(ostream& out, Customer _customer) {
return out <<  _customer.getFirstName() << " " << _customer.getLastName() << " " << _customer.getTelephone() << " " << _customer.getNoOfCall() << " " << endl;
}

now, as you siad I should add another function (write_to) in both classes and use polymorphism.
Should put VIRTUAL keyword just in customer class?
and what should be the return type of this "write_to" function?

thanks

Thanks for reply...
Yes I really noob in C++

actually, I wanna use << operator to write in text file.

ofstream output("CustomerOutput.dat", ios_base::out | ios_base::app);
for (int i = 0; i < top; ++i)
output << *customers[i] << "\n\n";

and I had this for << overriding:

ostream& operator<<(ostream& out, Customer _customer) {
return out <<  _customer.getFirstName() << " " << _customer.getLastName() << " " << _customer.getTelephone() << " " << _customer.getNoOfCall() << " " << endl;
}

now, as you siad I should add another function (write_to) in both classes and use polymorphism.
Should put VIRTUAL keyword just in customer class?
and what should be the return type of this "write_to" function?

thanks

The name doesn't matter, but you do need a virtual function. As you can see from my operator<< implementation, it doesn't need to return anything (void), or it can return the ostream (its parameter).

That write_to function is pretty much going to be what you had inside operator<< before. You need the extra function because operators can't be virtual.

And the virtual keyword is only NEEDED in the Customer class, because overrides will be virtual automatically. But it would be a good idea to mark the override as virtual as a reminded to people reading your code that it is virtual.

I almost did that, but can't figure out this error:

error C2662: 'Customer::write_to' : cannot convert 'this' pointer from 'const Customer' to 'Customer &'

void operator<< (ostream& out, const Customer& customer){ 
	customer.write_to(out);  
}



void Customer::write_to (ostream& out) {
	out <<  this->firstName  << endl;
}
void Customer::write_to (ostream& out) /* CHANGED HERE! */ const {
	out <<  this->firstName  << endl;
}

The write_to function doesn't change the object, so it can be marked as const. Then it can be used on const objects.

Of course you must change both the definition, and also the declaration inside your class.

Thanks for reply..
I've tried that one, but still getting an error:

void operator<< (ostream& out, Customer* _customer) { 
	_customer.write_to(out);  
}

Error: error C2228: left of '.write_to' must have class/struct/union

Thanks for reply..
I've tried that one, but still getting an error:

void operator<< (ostream& out, Customer* _customer) { 
	_customer.write_to(out);  
}

Error: error C2228: left of '.write_to' must have class/struct/union

That's because you made it a pointer. With a pointer you need to use -> for member access, but I don't think you want a pointer.

Did you change the second argument to a pointer for a reason?

And why are you always starting variable names with underscores? That's discouraged as names beginning with underscores are reserved for the use of compiler vendors. (Strictly speaking, underscore + lowercase isn't reserved, but I still stay away from it myself)

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.