Otchyot_OOPro_Laba2
.docxОтчёт по Лабораторной работе №2
По дисциплине «Объектно-ориентированное программирование»
Выполнила ст. гр. ИНФ-10-1
Михайлова Алексендра
Необходимо решить задание из предыдущей лабораторной работы в следующем виде:
Операции 1-3 необходимо реализовать в базовом классе;
Операции 4-7 необходимо реализовать в классе-наследнике;
Примечание: поскольку операция присваивания не наследуется, ее необходимо определить также в классе-наследнике.
Код программы:
Файл Header.h
#pragma once
#include<iostream>
#include <algorithm>
#include<list>
#include<vector>
#include<string>
using namespace std;
//Дата
struct Date{
short day;
short month;
short year;
void operator ()(short d,short m, short y);
void operator = (Date &d);
bool operator == (Date &d);
};
ostream& operator<<(ostream& out,Date &d);
//автор
struct Author
{
char name[20];
char surname[30];
char department[20];
void operator ()(char* n, char*s_n, char*dep);
void operator = (Author a);
};
ostream& operator<<(ostream& out, Author &a);
struct Student
{
char name[30];
char surname[30];
void operator ()(char* n,char* s_n);
void operator = (Student &s);
bool operator == (Student &s);
};
ostream& operator<<(ostream& out, Student &s);
//выдача
struct Delivery
{
Date date;
Student stud;
void operator ()(Date d,Student s);
};
ostream& operator<<(ostream& out, Delivery &d);
class Book
{
protected:
Author author;//информация об авторе
char name[50];//имя пособия
char subject[20];//предмет
bool is_on_hands;//выдано ли пособие?
list<Delivery> deliveries;//список выдач
public:
Book();
Book(const Book &obj);
Book(Author a_inf, char*n, char*s);
~Book();
Author ReturnAuthorOfManual();//1)Узнать автора пособия+
char* ReturnNameOfManual();//2)получить название пособия+
char* ReturnSubjectOfManual();//2)получить предмет пособия+
int ReturnCount();//3)Получить количество выдач
bool AddDelivery(Delivery d);
list<Delivery> ReturnDeliveries();
void GetBack();
void operator =(const Book& obj);
friend ostream& operator << (ostream &out, Book &b);
};
//методическое пособие
class MethodicalManual:public Book
{
public:
MethodicalManual();//конструктор по умолчанию
MethodicalManual(Author a_inf, char*n, char*s);//конструктор с параметрами
MethodicalManual(const MethodicalManual& obj);//конструктор копирования
~MethodicalManual();// деструктор+
void GiveManual(Delivery d);//4)выдать пособие на руки+
void GiveManual(Date date, Student s);//4)выдать пособие на руки
void GetBackManual();//5)получить пособие обратно
int ReturnCountOfDeliveries();
int ReturnCountOfDeliveries(Date d);//7)узнать число выдач для заданной даты
void FindStudent();//6)найти студента с наибольшим числом выдач
friend ostream& operator << (ostream &out, MethodicalManual &m);
void operator =(const MethodicalManual& obj);
};
Файл Book.cpp
#include "Header.h"
Book::Book()
{
Author a;
a("No name","No surname","Unknown");
strcpy(name,"Untitled");
strcpy(subject,"Unknown");
is_on_hands=false;
}
Book::Book(Author a_inf, char*n, char*s)
{
author=a_inf;
strcpy(name,n);
strcpy(subject,s);
is_on_hands=false;
}
Book::Book(const Book &obj)
{
author=obj.author;
strcpy(name,obj.name);
strcpy(subject,obj.subject);
deliveries = obj.deliveries;
}
Book::~Book()
{
deliveries.clear();
}
bool Book::AddDelivery(Delivery d)
{
if(!is_on_hands)
{
deliveries.push_back(d);
is_on_hands=true;
return true;
}
else
return false;
}
char* Book::ReturnNameOfManual()
{
return name;
}
char* Book::ReturnSubjectOfManual()
{
return subject;
}
Author Book::ReturnAuthorOfManual()
{
return author;
}
void Book::GetBack()
{
if(is_on_hands)
is_on_hands=false;
}
int Book::ReturnCount()
{
return (int)deliveries.size();
}
list<Delivery> Book::ReturnDeliveries()
{
return deliveries;
}
void Book::operator=(const Book& obj)
{
author=obj.author;
strcpy(name,obj.name);
strcpy(subject,obj.subject);
deliveries = obj.deliveries;
}
Файл MethodicalManual.cpp
#include "Header.h"
MethodicalManual::MethodicalManual():Book(){}
MethodicalManual::MethodicalManual(Author a_inf, char*n, char*s):Book(a_inf,n,s){}
MethodicalManual::~MethodicalManual(){}
void MethodicalManual::GiveManual(Date date, Student s)
{
Delivery d;
d(date,s);
if(AddDelivery(d))
cout<<"Manual was get to "<<d.stud<<"("<<d.date<<")"<<endl;
else
cout<<"Manual is already issued!"<<endl;
}
void MethodicalManual::GiveManual(Delivery d)
{
if(AddDelivery(d))
cout<<"Manual was get to "<<d.stud<<"("<<d.date<<")"<<endl;
else
cout<<"Manual is already issued!"<<endl;
}
int MethodicalManual::ReturnCountOfDeliveries(Date d)
{
int count=0;
list<Delivery> deliv = ReturnDeliveries();
list<Delivery>::iterator iter = deliv.begin();
cout<<"Deliveries on "<<d<<":";
while(iter!=deliv.end())
{
Delivery del=*iter;
if(d==del.date)
count++;
++iter;
}
cout<<count<<endl;
return count;
}
int MethodicalManual::ReturnCountOfDeliveries()
{
return ReturnCount();
}
void MethodicalManual::FindStudent()
{
list<Delivery> del = ReturnDeliveries();
if(del.size()!=0)
{
int max_count=0,temp_count=0;
Student max_stud;
list<Delivery>::iterator iter = del.begin();
list<Delivery>::iterator it = del.begin();
while(iter!=del.end())
{
Delivery d = *iter;
Student s = d.stud;
max_stud = s;
while(it!=del.end())
{
Delivery temp_d = *it;
Student temp_s = temp_d.stud;
if(s==temp_s)
++temp_count;
++it;
}
if(temp_count>max_count)
{
max_count=temp_count;
max_stud = s;
}
++iter;
}
cout<<"Student with max count of deliveries "<<max_stud<<endl;
}
}
void MethodicalManual::GetBackManual()
{
GetBack();
}
ostream& operator<<(ostream &out, MethodicalManual &m)
{
out<<"Manual Information:\nName: "<<m.ReturnNameOfManual()<<"\nSubject: "<<m.ReturnSubjectOfManual()<<"\nAuthor Info:\n"<<m.ReturnAuthorOfManual()<<endl;
return out;
}
void MethodicalManual::operator=(const MethodicalManual& obj)
{
author=obj.author;
strcpy(name,obj.name);
strcpy(subject,obj.subject);
deliveries = obj.deliveries;
}
MethodicalManual::MethodicalManual(const MethodicalManual& obj)
{
author=obj.author;
strcpy(name,obj.name);
strcpy(subject,obj.subject);
deliveries = obj.deliveries;
}
Файл Student.cpp
#include "Header.h"
void Student::operator ()(char *n, char *s_n)
{
strcpy(name,n);
strcpy(surname,s_n);
}
void Student::operator =(Student &s)
{
strcpy(name,s.name);
strcpy(surname,s.surname);
}
bool Student::operator ==(Student &s)
{
if(strcmp(name,s.name)!=0 || strcmp(surname,s.name)!=0)
return false;
else
return true;
}
ostream& operator <<(ostream& out, Student &s)
{
out<<s.name<<" "<<s.surname<<endl;
return out;
}
Файл Delivery.cpp
#include "Header.h"
ostream& operator<<(ostream& out,Delivery &d)
{
out<<"Delivery date: "<<d.date<<"\nStudent:\n"<<d.stud<<endl;
return out;
}
void Delivery:: operator ()(Date d, Student s)
{
date=d;
stud=s;
}
Файл Date.cpp
#include <string>
#include "Header.h"
void Date:: operator ()(short d,short m, short y)
{
if(d>0 && d<32)
day=d;
else
{
cout<<"Error! Invalid \"day\" value "<<d<<"! \"day\" is equal 1\n";
day=1;
}
if(m>0 && m<13)
month=m;
else
{
cout<<"Error! Invalid \"month\" value "<<m<<"! \"month\" is equal 1\n";
month=1;
}
if(y>0)
year=y;
else
{
cout<<"Error! Invalid \"year\" value "<<y<<"! \"year\" is equal 1900\n";
year=1900;
}
}
void Date::operator =(Date &d)
{
day=d.day;
month = d.month;
year = d.year;
}
bool Date::operator ==(Date &d)
{
if(year != d.year || month !=d.month || day!=d.day)
return false;
else
return true;
}
ostream& operator<<(ostream& out,Date &d)
{
if(d.day>9)
out<<d.day;
else
out<<"0"<<d.day;
out<<".";
if(d.month>9)
out<<d.month;
else
out<<"0"<<d.month;
out<<"."<<d.year;
return out;
}
Файл Author.cpp
#include "Header.h"
ostream& operator<<(ostream& out, Author &a)
{
out<<"Name: "<<a.name<<"\nSurname: "<<a.surname<<"\nDepartment: "<<a.department<<endl;
return out;
}
void Author:: operator ()(char* n, char*s_n, char*dep)
{
strcpy(name,n);
strcpy(surname,s_n);
strcpy(department,dep);
}
void Author::operator = (Author a)
{
strcpy(name,a.name);
strcpy(surname,a.surname);
strcpy(department,a.department);
}
Файл main.cpp
#include<iostream>
#include"Header.h"
using namespace std;
void main()
{
setlocale(LC_CTYPE,"rus");
Author a1;
a1("Василий","Пупкин","ИНФ");
MethodicalManual m1(a1,"Философия программирования","Программирование");
cout<<m1;
Student s1,s2;
s1("Петя","Васечкин");
s2("Вася","Петечкин");
Date d1,d2;
d1(31,10,2011);
d2(29,10,2011);
m1.GiveManual(d2,s2);
m1.GetBackManual();
m1.GiveManual(d1,s1);
m1.GetBackManual();
m1.GiveManual(d1,s2);
m1.GetBackManual();
m1.GiveManual(d1,s2);
cout<<"Total count of deliveries:"<<m1.ReturnCountOfDeliveries()<<endl;
m1.ReturnCountOfDeliveries(d1);
m1.FindStudent();
system("pause");
}