Académique Documents
Professionnel Documents
Culture Documents
Raport
La lucrarea de laborator la programarea C++ Nr. 5
Varianta 11
Chiinu 2016
Laboratory work No. 5
Topic:Solving Diamond problem using virtual
inheritance n C++.
Objectives: Achieving practical skills for implementing multiple inheritance
and virtual inheritance to avoid diamond problem, in C++ language.
Work processing:
Methods used in laboratory work:
In this laboratory work we have to implement 2 hierarchies of classes, one made of
Book, Copybook and Notebook that inherits from both, and Paper Book, Copybook
and Notebook that inherits from both. For second hierarchy well use virtual inheritance
to avoid method ambiguity in Notebook class.
The main goal of this laboratory is to use virtual inheritance in order to provide a simple
way to deal with multiple inheritance pitfalls.
Data analyzing:
Main function:
Main is for testing purposes is just to show that everything works without runtime
errors or bugs.
Constructors:
All classes does have parametrized constructors as well as default ones. Also
destructors does not have some special behavior, but they are virtual, in order to
make subtyping polymorphism safe, without memory leaks. Also, in derived classes,
constructors of superclasses are called in the same order as in their hierarchy.
class Book {
protected:
string author;
string publishedAt;
string summary;
long id;
public:
Book();
~Book();
Book(string _author, string _publishedAt, long _id);
a/src/Book.cpp file:
#include "Book.h"
Book::Book() {};
Book::~Book() {};
a/src/Copybook.h file:
#include <iostream>
class Copybook {
protected:
string holder;
string content;
public:
Copybook();
~Copybook();
Copybook(string _holder, string _content);
a/src/Copybook.cpp file:
#include "Copybook.h"
Copybook::Copybook() {};
Copybook::~Copybook() {};
a/src/Notebook.h file:
#include "Book.h"
#include "Copybook.h"
a/src/Notebook.cpp file:
#include "Notebook.h"
Notebook::Notebook() {};
Notebook::~Notebook() {};
Notebook::Notebook(string _holder, string _content):
Copybook(_holder, _content), Book() {};
a/main.cpp file:
#include "./src/Notebook.h"
return 0;
}
b/src/Paper.h file:
#include <iostream>
#ifndef PAPER_H
#define PAPER_H
class Paper {
protected:
string holder;
string content;
public:
Paper();
Paper(string _holder, string _content);
virtual ~Paper() = 0;
#endif
b/src/Book.h file:
#include "Paper.h"
#ifndef BOOK_H
#define BOOK_H
#endif
b/src/Copybook.h file:
#include "Paper.h"
#ifndef COPYBOOK_H
#define COPYBOOK_H
class Copybook: virtual public Paper {
public:
Copybook();
~Copybook();
Copybook(string _holder, string _content);
#endif
b/src/Notebook.h file:
#include "Book.h"
#include "Copybook.h"
#ifndef NOTEBOOK_H
#define NOTEBOOK_H
#endif
b/src/Paper.cpp file:
#include "Paper.h"
Paper::Paper() {};
Paper::~Paper() {};
b/src/Book.cpp file:
#include "Book.h"
Book::Book() {};
Book::~Book() {};
b/src/Copybook.cpp file:
#include "Copybook.h"
Copybook::Copybook() {};
Copybook::~Copybook() {};
b/src/Notebook.cpp file:
#include "Notebook.h"
Notebook::Notebook() {};
Notebook::~Notebook() {};
Bibliography
1. Lectures on C++ Programming of dr., conf. univ. Kulev M. Chi inu: UTM,
2016.
2. http://stackoverflow.com/
3. https://tutorialspoint.com
4. https://msdn.microsoft.com