On Mon, Mar 22, 2010 at 12:51 PM, Jalpan <[email protected]> wrote:
> does anyone have any idea how should we use a function ...that will perform
> the same activity for the two diffrent classes...
>
> i have give my problem below...
> =====================================
>
> class person
> {
> char nam[10];
> int age;
> };
If I were doing this, I'd create person as an abstract class by
declaring a public function as a pure function (polymorphism)
public:
void read() = 0;
> class employ : person
> {
> char e_id;
> public:
> void get();
> void put();
void read(); //implemention for employ
> };
>
> class customer : person
> {
> char p_id;
> public:
> void get();
> void put();
void read(); //implementation for customer
> };
Then each child class implements read() as is necessary for the
specific data in the class.
> void read(fstream &f,<pointer to class>)
> {
> saves data to file
> }
For this non-class function you can pass in a reference to person class
void read(fstream &f, person & someperson) {
someperson.read();
}
In fact, I wouldn't even mess with passing in the fstream, just use a
local object inside and pass in the filename:
void read(string & file, person & someperson) {
fstream.open(...);
someperson.read();
fstream.close();
}
> int main()
> {
> customer c;
> customer * c1;
> employ e;
> employ * e1;
> fstream c,e;
> c.open("cus.txt",ios::in);
> read(&c,custoemer * c1);
> c.close();
> e.open("emp.txt",ios::in);
> read(&e,employ * e1);
> return 0;
> }
Not sure why you are playing around with pointers here. References are
much easier.
int main()
{
customer cust;
employ emp;
read("emp.txt", emp);
read("cust.txt", cust);
return 0;
}
-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
If I were to divulge it, it would overturn the world."
-- Jelaleddin Rumi