#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio>
class Tags
{
protected:
char nik[10];
int tel;
public:
Tags(char*,int);
void virtual show();
// здесь надо написать еще один метод для создания объектов класса Tags и размещения их в массиве
int getphone()
{
return tel;
}
};
class XTags:public Tags
{
int telr;
public:
XTags(char*,int,int);
void show();
// тут тоже нужен, вроде, метод
int getphone()
{
return tel;
}
};
Tags::Tags(char* nnik,int ntel)
{
strcpy(nik,nnik);
tel=ntel;
}
XTags::XTags(char* nnik,int ntel,int ntelr):Tags(nnik,ntel)
{
telr=ntelr;
}
void Tags::show()
{
cout<<nik<<" "<<tel<<" ";
}
void XTags::show()
{
Tags::show();
cout<<telr;
}
void main()
{
}
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio>
class Tags
{protected:
char nik[10];
int tel;
public:
Tags(char*,int);
void virtual show();
char* getnik() // тут надо получить кличку собаки
{return nik;}
int getphone() // тут получаем номер телефона владельца
{return tel;}
};
class XTags:public Tags
{int telr;
public:
XTags(char*,int,int);
void show();
char* getnik(){return nik;}
int getphone(){return tel;}
};
Tags::Tags(char* nnik,int ntel)
{
strcpy(nik,nnik);
tel=ntel;
}
XTags::XTags(char* nnik,int ntel,int ntelr):Tags(nnik,ntel)
{
telr=ntelr;
}
void Tags::show()
{
cout<<nik<<" "<<tel<<" ";
}
void XTags::show()
{
Tags::show();
cout<<telr;
}
Tags* mas[100];
void main()
{char n[10],s[10]; int t,tr,k,i=0,m;
FILE *f;
f=fopen("d:\\bb.txt","r");
while (!feof(f))
{
fscanf(f,"%d",&k);
if (k==1) // проверка новая бирка или нет
{
fscanf(f,"%s%d%d",n,&t,&tr);
*(mas+i)=new XTags(n,t,tr);
}
else
{
fscanf(f,"%s%d",n,&t);
*(mas+i)=new Tags(n,t);
}
i++;
}
m=i;
for(i=0;i<m;i++)
{
(*(mas+i))->show();
cout<<"\n";
}
cout << "Enter nik of dog\n";
cin >> s;
for(i=0;i<m;i++)
{
if (!strcmp((*(mas+i))->getnik(),s))
cout << (*(mas+i))->getphone() << "\n";
}
getch();
}
Ну, что-то такого типа:
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
using namespace std;
class Tags
{
protected:
char nick[10];
int tel;
public:
virtual ostream& print(ostream &os) const {
os << nick << " " << tel;
return os;
}
Tags(char*,int);
char* get_nick() { // тут надо получить кличку собаки
return nick;
}
int get_phone() { // тут получаем номер телефона владельца
return tel;
}
};
class XTags: public Tags
{
int telr;
public:
virtual ostream& print(ostream &os) const {
Tags::print(os) << " " << telr;
return os;
}
XTags(char*, int, int);
};
ostream& operator << (ostream &os, const Tags* tg) {
tg -> print(os);
return os;
}
Tags::Tags(char* nnik, int ntel) {
strcpy(nick, nnik);
tel = ntel;
}
XTags::XTags(char* nnik, int ntel, int ntelr): Tags(nnik,ntel) {
telr = ntelr;
}
void makeTags(vector<Tags*>& vec, ifstream& is) {
int is_new, phone;
char nick[50];
while((is_new = is.get()) != EOF) {
is >> nick;
is >> phone;
if(is_new == '1') {
int add_phone;
is >> add_phone;
vec.push_back(new XTags(nick, phone, add_phone));
}
else {
vec.push_back(new Tags(nick, phone));
}
// reading up to "\n"
int ch;
while(is && (ch = is.get()) != '\n');
}
}
void showTags(vector<Tags*>& vec) {
copy(vec.begin(), vec.end(), ostream_iterator<Tags*>(cout, "\n"));
/*
for(vector<Tags*>::iterator it = vec.begin(); it != vec.end(); ++it) {
cout << *(*it) << endl;
}
*/
}
void checkTags(vector<Tags*>& vec) {
char s[20];
cout << "nick to find: " << endl;
cin >> s;
vector<Tags*>::iterator it = vec.begin();
for(; it != vec.end(); it++) {
if(!strcmp((*it)->get_nick(), s)) break;
}
if(it != vec.end()) {
cout << "phone number = " << (*it)->get_phone() << endl;
}
else {
cout << "nick was not found" << endl;
}
}
int main()
{
vector<Tags*> mas;
ifstream inFile("g:\\birka.txt", ios::in);
makeTags(mas, inFile);
inFile.close();
showTags(mas);
checkTags(mas);
return 0;
}