comp.language, c++

M

Monis Khan

Hello every one
I am working on a project which involve run-time polymorphism and writing data in files. I have created an abstract class and 4 other classes deriving the base class(derived class). here is it like

class prdcts
{
virtual void enter()=0;
}

4 classes deriving from this base class. In main I created option like this

void main()
{
//other codes
if(ch==1)
{
class a;
deploy(&a);
}
// other codes
}

void deploy(prdcts *p)
{
fstream filee;
filee.open("demao.dat",ios::eek:ut|ios::binary);

p->enter();
filee.write((char*)&p,sizeof(p));

//other codes
}

when I am writing this no data is written in the file, plz help me out with correct syntax and explaintaion for writing in file through pointer of base class
thank you in advance
or mail me at :[email protected]
 
Ö

Öö Tiib

Hello every one

I am working on a project which involve run-time polymorphism and writing
data in files. I have created an abstract class and 4 other classes
deriving the base class(derived class). here is it like

I hope it is school assignment, otherwise with such a level of knowledge
you will be kicked far from that "project".
class prdcts

{
virtual void enter()=0;
}

Size of this class is very little. Basically pointer to virtual functions
table.
4 classes deriving from this base class. In main I created option like this

Oh! You think that those 4 are least important for the problem?
void main()

{
//other codes
if(ch==1)

{

class a;

Forward-declaring a class here?
deploy(&a);

You can not take pointer to a class. You need object.
}

// other codes

}

void deploy(prdcts *p)
{
fstream filee;
filee.open("demao.dat",ios::eek:ut|ios::binary);

You do not check how it went?
p->enter();

what that does?
filee.write((char*)&p,sizeof(p));

You take pointer to base class convert pointer to that pointer
into char pointer and write to file?
//other codes
}

when I am writing this no data is written in the file, plz help me out
with correct syntax and explaintaion for writing in file through pointer
of base class

Fuzzy pointer magics that wizards like us will suggest do not exist. You
need to write code that does exactly what the program must do. You need
virtual function in each derived class that writes data of it to file.
'enter()' does not sound like one so add another.
 
M

Monis Khan

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class prdcts;
void detail(prdcts *ptr2); //function recieving adress as an argument for run-time polymorphism
class prdcts
{
protected :
char c,*cptr;
char name[25];
char brndnme[25],*tag;
float price;
long artno;
char splr_nme[25], splr_adrs[30],splr_wbadrs[30];
double splr_cntcno;

int qty;

public :

virtual void enter() =0;
virtual void disp()=0;

prdcts()
{
c='@';
}
};
class b: public prdcts
{

char tg[10];
char flavr[30];
float wt;
public:
b()
{}

void enter()
{
clrscr();
cout<<"\nEnter the Product name :";
gets(name);
cout<<"\nEnter the Brand name :";
gets(brndnme);
cout<<"\nEnter the flavour :";
gets(flavr) ;
cout<<"\nEnter the wt (ml) :" ;
cin>>wt;
cout<<"\nEnter the Tag : ";
gets(tag);

cout<<"\nEnter the Price :";
cin>>price;
cout<<"\nEnter the Article no. :" ;
cin>>artno;
cout<<"\nEnter the Supplier Name : ";
gets(splr_nme);
cout<<"\nEnter the Supplier Adress : ";
gets(splr_adrs);
cout<<"\nEnter the Supplier contact no :";
cin>>splr_cntcno;
cout<<"\nEnter the Web contact :";
agn: //label
gets(splr_wbadrs);
cptr=strchr(splr_wbadrs,c);
if(!cptr)
{

getch();
gotoxy(24,22);
clreol();
goto agn;
}

}
void disp()
{
clrscr();
cout<<"\nProduct Name :"<<name;
cout<<"\nBrand Name\t:"<<brndnme ;
cout<<"\nTag\t :"<<tag;
cout<<"\nFlavour\t :"<<flavr;
cout<<"\nWeight (gm) :"<<wt;
cout<<"\nPrice\t : "<<price;
cout<<"\nArticle No. :"<<artno;

}

void modify()
{


}
~b()
{}

} ;
class cosmetics: public prdcts
{
char tg1[10];
float wght;
public:
void enter()
{
clrscr();
cout<<"\nEnter the Product name :";
gets(name);
cout<<"\nEnter the Brand name :";
gets(brndnme);
cout<<"\nEnter the wt (ml) :" ;
cin>>wght;
cout<<"\nEnter the Tag : ";
gets(tg1);
strcpy(tag,tg1);
cout<<"\nEnter the Price :";
cin>>price;
cout<<"\nEnter the Article no. :" ;
cin>>artno;
cout<<"\nEnter the Supplier Name : ";
gets(splr_nme);
cout<<"\nEnter the Supplier Adress : ";
gets(splr_adrs);
cout<<"\nEnter the Supplier contact no :";
cin>>splr_cntcno;
cout<<"\nEnter the Web contact :";
agn1: //label
gets(splr_wbadrs);
cptr=strchr(splr_wbadrs,c);
if(!cptr)
{

getch();
gotoxy(24,22);
clreol();
goto agn1;
}



}
void disp()
{
clrscr();
cout<<"\nProduct Name :"<<name;
cout<<"\nBrand Name\t:"<<brndnme ;
cout<<"\nTag\t :"<<tag;
//cout<<"\nFlavour\t :"<<flavr;
cout<<"\nWeight (gm) :"<<wght;
cout<<"\nPrice\t : "<<price;
cout<<"\nArticle No. :"<<artno;


}



void modify()
{
cout<<"modify";

}

} ;
class kirana :public prdcts
{ char tg1[10];
float wght;

public:
void enter()
{
clrscr();
cout<<"\nEnter the Product name :";
gets(name);
cout<<"\nEnter the Brand name :";
gets(brndnme);
cout<<"\nEnter the wt (ml) :" ;
cin>>wght;
cout<<"\nEnter the Tag : ";
gets(tg1);
strcpy(tag,tg1);
cout<<"\nEnter the Price :";
cin>>price;
cout<<"\nEnter the Article no. :" ;
cin>>artno;
cout<<"\nEnter the Supplier Name : ";
gets(splr_nme);
cout<<"\nEnter the Supplier Adress : ";
gets(splr_adrs);
cout<<"\nEnter the Supplier contact no :";
cin>>splr_cntcno;
cout<<"\nEnter the Web contact :";
agn: //label
gets(splr_wbadrs);
cptr=strchr(splr_wbadrs,c);
if(!cptr)
{

getch();
gotoxy(24,22);
clreol();
goto agn;
}

}
void disp()
{
clrscr();
cout<<"\nProduct Name :"<<name;
cout<<"\nBrand Name\t:"<<brndnme ;
cout<<"\nTag\t :"<<tag;
// cout<<"\nFlavour\t :"<<flavr;
cout<<"\nWeight (gm) :"<<wght;
cout<<"\nPrice\t : "<<price;
cout<<"\nArticle No. :"<<artno;


}

void modify()
{


}
};
class stationary : public prdcts
{ char tg1[10];
float wght;

public:
void enter()
{
clrscr();
cout<<"\nEnter the Product name :";
gets(name);
cout<<"\nEnter the Brand name :";
gets(brndnme);
cout<<"\nEnter the wt (ml) :" ;
cin>>wght;
cout<<"\nEnter the Tag : ";
gets(tg1);
strcpy(tag,tg1);
cout<<"\nEnter the Price :";
cin>>price;
cout<<"\nEnter the Article no. :" ;
cin>>artno;
cout<<"\nEnter the Supplier Name : ";
gets(splr_nme);
cout<<"\nEnter the Supplier Adress : ";
gets(splr_adrs);
cout<<"\nEnter the Supplier contact no :";
cin>>splr_cntcno;
cout<<"\nEnter the Web contact :";
agn: //label
gets(splr_wbadrs);
cptr=strchr(splr_wbadrs,c);
if(!cptr)
{

getch();
gotoxy(24,22);
clreol();
goto agn;
}

}
void disp()
{
clrscr();
cout<<"\nProduct Name :"<<name;
cout<<"\nBrand Name\t:"<<brndnme ;
cout<<"\nTag\t :"<<tag;
// cout<<"\nFlavour\t :"<<flavr;
cout<<"\nWeight (gm) :"<<wght;
cout<<"\nPrice\t : "<<price;
cout<<"\nArticle No. :"<<artno;


}

void modify()
{


}


} ;


void main()
{
clrscr();
prdcts *p1;


int z,chc;
char ch='y' ;

do
{

cout<<endl<<endl<<"1. Brevages ";
cout<<endl<<"2. Cosmetics";
cout<<endl<<"3. Kirana" ;
cout<<endl<<"4. Stationary " ;
cout<<endl<<"Enter your choice :";
cin>>z;

if(z==1)
{

b b1;
p1=&b1;
detail(p1);
}

if(z==2)
{
cosmetics c;
p1=&c;
detail(p1);


}
if(z==3)
{
kirana k;
p1=&k;
detail(p1);


}
if(z==4)
{
stationary s;
p1=&s;
detail(p1);


}
gotoxy(40,23);
cout<<"\nDo you wish to continue :" ;
cin>>ch;

}while(ch!='n');
getch();

}


void detail(prdcts *p2)
{
int chc;
fstream fi;
clrscr();
gotoxy(5,5);
cout<<"1.Add Record ";
gotoxy(23,5);
cout<<"2.Delete Record ";
gotoxy(43,5);
cout<<"3.Modify Reord ";
gotoxy(63,5);
cout<<"4.View Record";
gotoxy(28,21);
cout<<"Enter your choice :";
cin>>chc;

if(chc==1)
{ fi.open("dexter.txt",ios::eek:ut|ios::app|ios::binary);

p2->enter();
fi.write((char *)&p2,sizeof(*p2));
fi.close();
}

if(chc==3)
{
gotoxy(43,7);
cout<<"Modify 'N'ame ";
gotoxy(43,8);
cout<<"Modify 'P'rice ";
gotoxy(43,9);
cout<<"Modify 'Q'ty ";
gotoxy(43,10);
cout<<"Modify 'T'ag ";
}

if(chc==4)
{
gotoxy(63,7);
cout<<"'S'earch a Record ";
gotoxy(63,8);
cout<<"'L'ook all Record ";
gotoxy(63,9);
cout<<" Your Choice : ";
cin>>chc;
if(chc==2)
{
fi.open("dexter.txt",ios::in);
fi.seekg(0);
while(!fi.eof())
{

fi.read((char *)&p2,sizeof(*p2));
p2->disp();
cout<<"Hello";
}
fi.close();
}
}

}

now check this, and tell
 
Ö

Öö Tiib

now check this, and tell

But I already did: "You need virtual function implemented in each derived
class that writes data of it to file. 'enter()' does not sound like one
so add another." ... or something like that. I can add only that disp()
does not sound like one either.
 
M

Monis Khan

ok,hope run time polymorphism also works in projects other then school level...
and also tell what the syntax is writing in file with the help of pointer.
for normal object writing we usually go for :
"stream_name.write((char *)&class_object,sizeof(class_object));"
and enter is over-rided in all derived class
plz explain also
thank you
 
Ö

Öö Tiib

and also tell what the syntax is writing in file with the help of pointer.
for normal object writing we usually go for :
"stream_name.write((char *)&class_object,sizeof(class_object));"

What syntax? There are no syntax in C++ how to write objects to disk.
What you want to do is called "serialization". That is bit more
difficult art than just "write" and done. You have to decide some
things that you even do not seemingly recognize those exist.

See http://www.parashift.com/c++-faq
Section 36 is about serialization. Read it. All.
and enter is over-rided in all derived class plz explain also thank you

What has the "enter" to do with it? If you have objects of 4 different
types then each type has to be displayed, written to disk and read
from disk differently, depending on of what they consist.
 
M

Monis Khan

Thank you
for helping me,I'll change my code. one more thing to ask I am new to this language, can you help me how shall I excel in this language for strong foundation and to do good projects. Do GUI version of C++ is on demand in comparison to Java/c#.

thank yoi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top