Originally Posted by
Elysia
You're right. I missed that. You should append "= 0" to the virtual destructor to make the function pure virtual and the class abstract.
Can't help you if you don't show your code that does not compile and list your compile errors.
I think that I did it right now.
However I have another doubt. I have XComicBook class which is a Book. If I change the Book constructor how can I set the XComicBook price?
Should I change CalcPrice function of Book to protected and use it at ComicBook? It doesn't seem right because I need to create a Xcomicbook anyway.
XProduct.h
Code:
#ifndef XPRODUCT_H
#define XPRODUCT_H
#include <string>
#include <iostream>
#include <vector>
class XProduct
{
protected:
int m_barCode;
std::string m_title;
std::string m_author;
std::string m_publisher;
int m_year;
float m_price;
public:
XProduct();
XProduct(int BarCode, std::string const Title, std::string const Author,
std::string const Publisher, int Year, float Price);
virtual ~XProduct() = 0;
int GetBarCode() const;
const std::string& GetTitle() const;
const std::string& GetAuthor() const;
const std::string& GetPublisher() const;
int GetYear() const;
float GetPrice() const;
void setBarCode(int BarCode);
void setTitle(std::string Title);
void setAuthor(std::string Author);
void setPublisher(std::string Publisher);
void setYear(int Year);
void setPrice(float Price);
void printProductData() const;
};
#endif // XPRODUCT_H
XProduct.cpp
Code:
#include "xproduct.h"
XProduct::XProduct()
{
}
XProduct::XProduct(int BarCode, const std::string Title, const std::string Author,
const std::string Publisher, int Year, float Price)
: m_barCode(std::move(BarCode)),
m_title(std::move(Title)),
m_author(std::move(Author)),
m_publisher(std::move(Publisher)),
m_year(std::move(Year)),
m_price(std::move(Price))
{}
XProduct::~XProduct() {}
int XProduct::GetBarCode() const {
return m_barCode;
}
const std::string& XProduct::GetTitle() const {
return m_title;
}
const std::string& XProduct::GetAuthor() const{
return m_author;
}
const std::string& XProduct::GetPublisher() const{
return m_publisher;
}
int XProduct::GetYear() const{
return m_year;
}
float XProduct::GetPrice() const {
return m_price;
}
void XProduct::setBarCode(int BarCode) {
m_barCode = std::move(BarCode);
}
void XProduct::setTitle(std::string Title) {
m_title = std::move(Title);
}
void XProduct::setAuthor(std::string Author) {
m_author= std::move(Author);
}
void XProduct::setPublisher(std::string Publisher){
m_publisher = std::move(Publisher);
}
void XProduct::setYear(int Year){
m_year = std::move(Year);
}
void XProduct::setPrice(float Price){
m_price = std::move(Price);
}
void XProduct::printProductData() const {
std::cout << "Bar code: " << m_barCode << std::endl;
std::cout << "Title: " << m_title << std::endl;
std::cout << "Author: " << m_author << std::endl;
std::cout << "Publisher: " << m_publisher << std::endl;
std::cout << "Price: " << m_price << std::endl;
std::cout << "Year: " << m_year << std::endl;
}
Xbook.h
Code:
#ifndef XBOOK_H
#define XBOOK_H
#include "xproduct.h"
class XBook : public XProduct
{
float CalcPrice(std::string Label);
protected:
int m_noPages;
std::string m_label;
public:
XBook();
//XBook(int BarCode, std::string const Title, std::string const Author,
// std::string const Publisher, int Year, float Price, int NoPages, std::string Label);
XBook(int BarCode, std::string const Title, std::string const Author,
std::string const Publisher, int Year, int NoPages, std::string Label);
const std::string & GetLabel() const;
int GetNoPages() const;
void SetPrice(std::string Label);
void SetLabel(std::string Label);
void SetNoPages(int NoPages);
};
#endif // XBOOK_H
Xbook.cpp
Code:
#include "xbook.h"
XBook::XBook()
{
}
XBook::XBook(int BarCode, const std::string Title, const std::string Author,
const std::string Publisher, int Year, int NoPages,
std::string Label)
:XProduct(BarCode, Title, Author, Publisher, Year, CalcPrice(Label)),
m_noPages(std::move(NoPages)),
m_label(std::move(Label))
{}
float XBook::CalcPrice(std::string Label){
if (Label == "Blue"){
m_price = 50.0;
}
else if (Label == "Green"){
m_price = 10.0;
}
else {
m_price = 0.0;
}
return m_price;
}
const std::string & XBook::GetLabel()const{
return m_label;
}
int XBook::GetNoPages() const {
return m_noPages;
}
void XBook::SetLabel(std::string Label){
m_label = std::move(Label);
}
void XBook::SetNoPages(int NoPages){
m_noPages = std::move(NoPages);
}
void XBook::SetPrice(std::string Label){
m_price = CalcPrice(Label);
}
Xcomicbook.h
Code:
#ifndef XCOMICBOOK_H
#define XCOMICBOOK_H
#include "xbook.h"
class XComicBook: public XBook
{
std::string m_creator;
std::string m_universe;
public:
XComicBook();
XComicBook(int BarCode, std::string const Title, std::string const Author,
std::string const Publisher, int Year, int NoPages, std::string Label,
std::string Creator, std::string Universe);
const std::string & GetCreator() const;
const std::string & GetUniverse() const;
void SetCreator(std::string Creator);
void SetUniverse(std::string Universe);
};
#endif // XCOMICBOOK_H
Xcomicbook.cpp
Code:
#include "xcomicbook.h"
XComicBook::XComicBook()
{
}
XComicBook::XComicBook(int BarCode, const std::string Title, const std::string Author, const std::string Publisher, int Year,
int NoPages, std::string Label,
std::string Creator, std::string Universe)
:XBook(BarCode, Title, Author, Publisher, Year, NoPages, Label),
m_creator(std::move(Creator)),
m_universe(std::move(Universe))
{}
const std::string & XComicBook::GetCreator() const{
return m_creator;
}
const std::string & XComicBook::GetUniverse() const{
return m_universe;
}
void XComicBook::SetCreator(std::string Creator){
m_creator = std::move(Creator);
}
void XComicBook::SetUniverse(std::string Universe){
m_universe = std::move(Universe);
}