Vous êtes sur la page 1sur 11

Ministerul Educaiei Republicii Moldova

Universitatea Tehnic a Moldovei

Facultatea Calculatoare, Informatic i


Microelectronic
Filiera anglofon (FAF)

Raport
La lucrarea de laborator la programarea C++ Nr. 5

Varianta 11

Efectuat de: studentul gr. FAF-151, Varacuta A.

Verificat de: Doctor Conf. Universitar, Kulev M.

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.

Condition of the problem: Create class hierarchies of classes. First


without an abstract base class and no virtual inheritance, the other one with virtual
inheritance and an abstract base class, namely Paper.

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.

Input/Output Streams operators:


All classes overloads the >>/<< operators. Both were implemented as friend
functions, because they need the first parameter of type istream/ostream respectively.
The output stream operator was not hard to implement. Both operators are overridden
in concrete classes.
Program text in C++ language:
a/src/Book.h file:
#include <iostream>

using namespace std;

class Book {
protected:
string author;
string publishedAt;
string summary;
long id;
public:
Book();
~Book();
Book(string _author, string _publishedAt, long _id);

void setSummary(string text);


friend ostream& operator << (ostream& out, Book &book);
friend istream& operator >> (istream& in, Book &book);
};

a/src/Book.cpp file:
#include "Book.h"

Book::Book() {};

Book::~Book() {};

Book::Book(string _author, string _publishedAt, long _id) {


author = _author;
publishedAt = _publishedAt;
id = _id;
summary = (string)"None";
};

void Book::setSummary(string text) { summary = text; };

ostream& operator << (ostream& out, Book &book) {


out << "Author: " << book.author << endl;
out << "Book ID: " << book.id << endl;
out << "Published At: " << book.publishedAt << endl;
out << '"' << book.summary << '"' << endl;
return out;
};

istream& operator >> (istream& in, Book &book) {


cout << "Author: ";
in >> book.author;
cout << "Book ID: ";
in >> book.id;
cout << "Published At: ";
in >> book.publishedAt;
cout << "Summary: ";
in >> book.summary;
return in;
};

a/src/Copybook.h file:
#include <iostream>

using namespace std;

class Copybook {
protected:
string holder;
string content;
public:
Copybook();
~Copybook();
Copybook(string _holder, string _content);

friend ostream& operator << (ostream& out, Copybook


&copybook);
friend istream& operator >> (istream& in, Copybook &copybook);

void addContent(string newContent);


};

a/src/Copybook.cpp file:
#include "Copybook.h"

Copybook::Copybook() {};

Copybook::~Copybook() {};

Copybook::Copybook(string _holder, string _content) {


holder = _holder;
content = _content;
};

void Copybook::addContent(string newContent) { content +=


newContent; };

ostream& operator << (ostream& out, Copybook &copybook) {


out << "It's " << copybook.holder + "'s" << " copybook" << endl;
out << '"' << copybook.content << '"' << endl;
return out;
};

istream& operator >> (istream& in, Copybook &copybook) {


cout << "Holder: ";
in >> copybook.holder;
cout << "Content: ";
in >> copybook.content;
return in;
};

a/src/Notebook.h file:
#include "Book.h"
#include "Copybook.h"

using namespace std;

class Notebook: public Book, public Copybook {


public:
Notebook();
~Notebook();

Notebook(string _holder, string _content);

friend ostream& operator << (ostream& out, Notebook


&notebook);
friend istream& operator >> (istream& in, Notebook &notebook);
};

a/src/Notebook.cpp file:
#include "Notebook.h"

Notebook::Notebook() {};

Notebook::~Notebook() {};
Notebook::Notebook(string _holder, string _content):
Copybook(_holder, _content), Book() {};

ostream& operator << (ostream& out, Notebook &notebook) {


out << "It's " << notebook.holder + "'s" << " notebook" << endl;
out << '"' << notebook.content << '"' << endl;
return out;
};

istream& operator >> (istream& in, Notebook &notebook) {


cout << "Holder: ";
in >> notebook.holder;
cout << "Content: ";
in >> notebook.content;
return in;
};

a/main.cpp file:
#include "./src/Notebook.h"

using namespace std;

int main(int argc, char const *argv[]) {


Book myBook;
Copybook myCopybook;
Notebook myNotebook;

cin >> myBook;


cin >> myCopybook;
cin >> myNotebook;

cout << myBook;


cout << myCopybook;
cout << myNotebook;

return 0;
}

b/src/Paper.h file:
#include <iostream>

using namespace std;

#ifndef PAPER_H
#define PAPER_H

class Paper {
protected:
string holder;
string content;
public:
Paper();
Paper(string _holder, string _content);
virtual ~Paper() = 0;

friend ostream& operator << (ostream& out, Paper &paper);


friend istream& operator >> (istream& in, Paper &paper);
};

#endif

b/src/Book.h file:
#include "Paper.h"

#ifndef BOOK_H
#define BOOK_H

class Book: virtual public Paper {


protected:
string author;
string publishedAt;
string summary;
long id;
public:
Book();
~Book();
Book(string _author, string _publishedAt, long _id);

void setSummary(string text);


friend ostream& operator << (ostream& out, Book &book);
friend istream& operator >> (istream& in, Book &book);
};

#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);

friend ostream& operator << (ostream& out, Copybook


&copybook);
friend istream& operator >> (istream& in, Copybook &copybook);

void addContent(string newContent);


};

#endif

b/src/Notebook.h file:
#include "Book.h"
#include "Copybook.h"

#ifndef NOTEBOOK_H
#define NOTEBOOK_H

class Notebook: public Copybook, public Book {


public:
Notebook();
~Notebook();

Notebook(string _holder, string _content);

friend ostream& operator << (ostream& out, Notebook


&notebook);
friend istream& operator >> (istream& in, Notebook &notebook);
};

#endif

b/src/Paper.cpp file:
#include "Paper.h"

Paper::Paper() {};

Paper::Paper(string _holder, string _content): holder(_holder),


content(_content) {};

Paper::~Paper() {};

ostream& operator << (ostream& out, Paper &paper) {


out << "It's " << paper.holder + "'s" << " paper" << endl;
out << '"' << paper.content << '"' << endl;
return out;
};

istream& operator >> (istream& in, Paper &paper) {


cout << "Holder: ";
in >> paper.holder;
cout << "Content: ";
in >> paper.content;
return in;
};

b/src/Book.cpp file:
#include "Book.h"

Book::Book() {};

Book::~Book() {};

Book::Book(string _author, string _publishedAt, long _id):


Paper(author, "None") {
publishedAt = _publishedAt;
author = _author;
id = _id;
summary = "None";
};

void Book::setSummary(string text) { summary = text; };

ostream& operator << (ostream& out, Book &book) {


out << "Author: " << book.author << endl;
out << "Book ID: " << book.id << endl;
out << "Published At: " << book.publishedAt << endl;
out << '"' << book.summary << '"' << endl;
return out;
};

istream& operator >> (istream& in, Book &book) {


cout << "Author: ";
in >> book.author;
cout << "Book ID: ";
in >> book.id;
cout << "Published At: ";
in >> book.publishedAt;
cout << "Summary: ";
in >> book.summary;
return in;
};

b/src/Copybook.cpp file:
#include "Copybook.h"

Copybook::Copybook() {};

Copybook::~Copybook() {};

Copybook::Copybook(string _holder, string _content):


Paper(_holder, _content) {};

void Copybook::addContent(string newContent) { content +=


newContent; }

ostream& operator << (ostream& out, Copybook &copybook) {


out << "It's " << copybook.holder + "'s" << " copybook" << endl;
out << '"' << copybook.content << '"' << endl;
return out;
};

istream& operator >> (istream& in, Copybook &copybook) {


cout << "Holder: ";
in >> copybook.holder;
cout << "Content: ";
in >> copybook.content;
return in;
};

b/src/Notebook.cpp file:
#include "Notebook.h"

Notebook::Notebook() {};

Notebook::~Notebook() {};

Notebook::Notebook(string _holder, string _content):


Paper(_holder, _content), Copybook(), Book() {};

ostream& operator << (ostream& out, Notebook &notebook) {


out << "It's " << notebook.holder + "'s" << " notebook" << endl;
out << '"' << notebook.content << '"' << endl;
return out;
};

istream& operator >> (istream& in, Notebook &notebook) {


cout << "Holder: ";
in >> notebook.holder;
cout << "Content: ";
in >> notebook.content;
return in;
};

Analysis of results and conclusions:


1. Multiple inheritance can dramatically reduce the size of the
codebase.
2. Although powerful, multiple inheritance comes at the cost of
possible ambiguity in method using in subclasses.
3. Virtual inheritance is a way C++ solves this problem, creating an
unique vtable for the class that inherits from 2 or more classes with
the same pattern.
4. Even if it is a solvable problem, multiple inheritance should be
avoided, or at least implemented carefully, using it as alternative for
interfaces or similar constructs.

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

Vous aimerez peut-être aussi