Alexanderkrup
05-12-2011, 19:55
Помогите пожалуйста разобраться...
1. В классах есть переопределенные виртуальные функции класса base. Как сделать так, что бы я мог вызывать эти функции. Например вызвать их из класса name, а не только из publication.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string.h>
using namespace std;
#define SIZE 256
class base {
public:
virtual void print()=0;
virtual void out_file()=0;
};
//-------------------------------------------------------
class name: public base {
public:
char *strname;
name(char *sname) {
strname=new char[256];
strname=sname;
}
~name() {
cout<<" 'имя' уничтожили"<<endl;
}
void print() {
cout<<"Название публикации: "<<strname<<endl;
}
void out_file() {
ofstream stream("result.txt", ios::out | ios::app);
stream<<strname<<" ";
stream.close();
}
};
//-------------------------------------------------------
class type: public base {
public:
char *strtype;
type(char *stype) {
strtype=new char[256];
strtype=stype;
}
~type() {
cout<<" 'тип' уничтожен "<<endl;
}
void print() {
cout<<"Тип публикации: "<<strtype<<endl;
}
void out_file() {
ofstream stream("result.txt", ios::out | ios::app);
stream<<strtype<<" ";
stream.close();
}
};
//-------------------------------------------------------
class publication: public name, public type
{
public:
char *p_name, *p_type;
int p_np, p_freq, p_nc;
double p_yc;
publication(char *name, char *type, int numofpages, int freq, int numofcopies):name(name),type(type)
{
p_name=new char[256];
p_name=name;
p_type=new char[256];
p_type=type;
p_np=numofpages;
p_freq=freq;
p_nc=numofcopies;
cout<<"Объект 'публикация' создан всё гут"<<endl<<"------------------------"<<endl;
}
~publication() {
cout<<" 'публикации' стерлись "<<endl;
}
void print(){
cout<<"Название публикации '"<<p_name<<"' "<<endl;
cout<<"Тип публикации '"<<p_type<<"'"<<endl;
cout<<"Кол-во страниц: "<<p_np<<endl;
cout<<"Частота выпуска: каждые "<<p_freq<<" дней(я)"<<endl;
cout<<"Тираж: "<<p_nc<<endl;
cout<<"Копий в год: "<<p_yc<<endl<<"------------------------"<<endl;
}
void out_file() {
ofstream stream("publications.txt", ios::out | ios::app);
stream<<"Название публикации '"<<p_name<<"' "<<endl;
stream<<"Тип публикации '"<<p_type<<"'"<<endl;
stream<<"Кол-во страниц: "<<p_np<<endl;
stream<<"Частота выпуска: каждые "<<p_freq<<" дней(я)"<<endl;
stream<<"Тираж: "<<p_nc<<endl;
stream<<"Копий в год: "<<p_yc<<endl<<endl;
stream.close();
}
friend void Run(publication *);
};
void run (publication *p){
p->p_yc=(365/(p->p_freq))*(p->p_nc);
}
//---------------------------------------------
void main (void)
{
setlocale(0, "rus");
int numofpages, freq, numofcopies;
char *name, *type;
name=new char[256];
type=new char[256];
cout<<"Введите название: "<<endl;
cin>>name;
cout<<"Введите тип: "<<endl;
cin>>type;
try{
cout<<"Введите кол-во страниц: "<<endl;
cin>>numofpages;
if(numofpages<0) throw 1;
}
catch(...) {
cout<<"Ошибка!Нужно вводить номер!\n";
ofstream stream("publications.txt", ios::out | ios::app);
stream<<"Ошибка"<<endl<<endl;
exit(0);
}
cout<<"Введите частоту выпуска: "<<endl;
cin>>freq;
cout<<"Введите тираж: "<<endl;
cin>>numofcopies;
publication pubobj(name,type,numofpages,freq,numofcopies);
run(&pubobj);
pubobj.print();
pubobj.out_file();
getch();
}
1. В классах есть переопределенные виртуальные функции класса base. Как сделать так, что бы я мог вызывать эти функции. Например вызвать их из класса name, а не только из publication.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string.h>
using namespace std;
#define SIZE 256
class base {
public:
virtual void print()=0;
virtual void out_file()=0;
};
//-------------------------------------------------------
class name: public base {
public:
char *strname;
name(char *sname) {
strname=new char[256];
strname=sname;
}
~name() {
cout<<" 'имя' уничтожили"<<endl;
}
void print() {
cout<<"Название публикации: "<<strname<<endl;
}
void out_file() {
ofstream stream("result.txt", ios::out | ios::app);
stream<<strname<<" ";
stream.close();
}
};
//-------------------------------------------------------
class type: public base {
public:
char *strtype;
type(char *stype) {
strtype=new char[256];
strtype=stype;
}
~type() {
cout<<" 'тип' уничтожен "<<endl;
}
void print() {
cout<<"Тип публикации: "<<strtype<<endl;
}
void out_file() {
ofstream stream("result.txt", ios::out | ios::app);
stream<<strtype<<" ";
stream.close();
}
};
//-------------------------------------------------------
class publication: public name, public type
{
public:
char *p_name, *p_type;
int p_np, p_freq, p_nc;
double p_yc;
publication(char *name, char *type, int numofpages, int freq, int numofcopies):name(name),type(type)
{
p_name=new char[256];
p_name=name;
p_type=new char[256];
p_type=type;
p_np=numofpages;
p_freq=freq;
p_nc=numofcopies;
cout<<"Объект 'публикация' создан всё гут"<<endl<<"------------------------"<<endl;
}
~publication() {
cout<<" 'публикации' стерлись "<<endl;
}
void print(){
cout<<"Название публикации '"<<p_name<<"' "<<endl;
cout<<"Тип публикации '"<<p_type<<"'"<<endl;
cout<<"Кол-во страниц: "<<p_np<<endl;
cout<<"Частота выпуска: каждые "<<p_freq<<" дней(я)"<<endl;
cout<<"Тираж: "<<p_nc<<endl;
cout<<"Копий в год: "<<p_yc<<endl<<"------------------------"<<endl;
}
void out_file() {
ofstream stream("publications.txt", ios::out | ios::app);
stream<<"Название публикации '"<<p_name<<"' "<<endl;
stream<<"Тип публикации '"<<p_type<<"'"<<endl;
stream<<"Кол-во страниц: "<<p_np<<endl;
stream<<"Частота выпуска: каждые "<<p_freq<<" дней(я)"<<endl;
stream<<"Тираж: "<<p_nc<<endl;
stream<<"Копий в год: "<<p_yc<<endl<<endl;
stream.close();
}
friend void Run(publication *);
};
void run (publication *p){
p->p_yc=(365/(p->p_freq))*(p->p_nc);
}
//---------------------------------------------
void main (void)
{
setlocale(0, "rus");
int numofpages, freq, numofcopies;
char *name, *type;
name=new char[256];
type=new char[256];
cout<<"Введите название: "<<endl;
cin>>name;
cout<<"Введите тип: "<<endl;
cin>>type;
try{
cout<<"Введите кол-во страниц: "<<endl;
cin>>numofpages;
if(numofpages<0) throw 1;
}
catch(...) {
cout<<"Ошибка!Нужно вводить номер!\n";
ofstream stream("publications.txt", ios::out | ios::app);
stream<<"Ошибка"<<endl<<endl;
exit(0);
}
cout<<"Введите частоту выпуска: "<<endl;
cin>>freq;
cout<<"Введите тираж: "<<endl;
cin>>numofcopies;
publication pubobj(name,type,numofpages,freq,numofcopies);
run(&pubobj);
pubobj.print();
pubobj.out_file();
getch();
}