Thread: Learning C++ by Example

  1. #151
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I've started digging into example, however I've figured it out that it didn't compile.
    Separate the definition from the declaration of the virtual destructor:
    Code:
    #include <iostream>
    
    class Base
    {
    public:
    	Base(int foo, int bar): m_foo(foo), m_bar(bar) {}
    	void SetFoo(int foo) { m_foo = foo; }
    	void SetBar(int bar) { m_bar = bar; }
    	int GetFoo() const { return m_foo; }
    	virtual ~Base() = 0; // Make Base an abstract class - we don't want clients to create instances of Base directly
    
    protected:
    	int m_foo, m_bar;
    };
    
    Base::~Base() {}
    
    class Derived: public Base
    {
    public:
    	Derived(int bar): Base(-1, bar) {}
    	Derived(int bar, int SomethingElse): Base(CalcFoo(SomethingElse), bar) {}
    	void SetSomethingElse(int SomethingElse) { m_foo = CalcFoo(SomethingElse); }
    	void SetFoo(int foo) = delete;
    
    private:
    	int CalcFoo(int SomethingElse) { return (SomethingElse == 100 ? 1 : 0); }
    };
    
    int main()
    {
    	Derived d(10); // Not initializing SomethingElse, so foo will have its default value
    	std::cout << "d.foo: " << d.GetFoo() << "\n";
    	d.SetSomethingElse(100); // Setting SomethingElse, so foo will get value 1.
    	std::cout << "d.foo: " << d.GetFoo() << "\n";
    	d.SetSomethingElse(0); // Setting SomethingElse to something other than 100, so foo will get value 0.
    	std::cout << "d.foo: " << d.GetFoo() << "\n";
    	
    	Derived d2(10, 100); // Initializing SomethingElse, so foo will get value 1.
    	std::cout << "d.foo: " << d2.GetFoo() << "\n";
    	d2.SetSomethingElse(-1); // Setting SomethingElse to something other than 100, so foo will get value 0.
    	std::cout << "d.foo: " << d2.GetFoo() << "\n";
    
    	//d2.SetFoo(0); // Won't compile because SetFoo is a deleted function in Derived
    	//Base b(0, 0); // Won't compile because Base is abstract
    	return 0;
    }
    Apparently GCC doesn't like it (nor does the standard), but MSVC happily accepts it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #152
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    you wrote:
    Code:
    Base(int foo, int bar): m_foo(foo), m_bar(bar) {}
    Why not?

    Code:
    Base(int foo, int bar): m_foo(std::move(foo)), m_bar(std::move(bar)) {}

  3. #153
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because you have nothing to gain from doing it on integral types. They're too small and they aren't class types.
    Learn more about rvalue references: Rvalue References and Move Semantics in C++11 - Cprogramming.com
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #154
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Thanks!

    I updated my code, however I've got an error in other classes:

    "undefined reference to `XProduct::~XProduct()'"

    Do I need to declare the virtual class in all?

    Code:
    #ifndef XBOOK_H
    #define XBOOK_H
    
    #include "xproduct.h"
    
    
    class XBook : public XProduct
    {
    
    float CalcPrice(float Price);
    
    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);
    
    
        const std::string & GetLabel() const;
        int GetNoPages() const;
    
        void SetLabel(std::string Label);
        void SetNoPages(int NoPages);
    
    };
    #endif // XBOOK_H
    Code:
    #include "xbook.h"
    
    XBook::XBook()
    {
    }
    
    XBook::XBook(int BarCode, const std::string Title, const std::string Author,
                 const std::string Publisher, int Year, float Price, int NoPages,
                 std::string Label)
        :XProduct(BarCode, Title, Author, Publisher, Year, CalcPrice(Price)),
          m_noPages(std::move(NoPages)),
          m_label(std::move(Label))
    {}
    
    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);
    }
    Updated Git:
    https://github.com/CreativeSoftware/LearnCppByExample/

  5. #155
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I updated my code, however I've got an error in other classes:

    "undefined reference to `XProduct::~XProduct()'"
    You declared your destructor in XProduct, but you never defined it (or rather, implemented it).

    Do I need to declare the virtual class in all?
    You should ask yourself if it makes sense if the user of the class can instantiate a product. If not, then it should be abstract.
    If it should be abstract, you should ask yourself, what functions do we require classes that derive from this class to implement? If there are any, they should be marked as pure virtual.
    If there are no such functions and yet you want the class to be abstract, you can declare the destructor pure virtual. It is also a good idea to mark the destructor of any base class as virtual to avoid problems when using pointers to base classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #156
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    At this case I need an abstract class.
    I updated the code. I'm not sure if I created in correct way the virtual class.

    I don't get how can I ride off "Price" from constructor. Do I need create another constructor?

    Product.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();
    
        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
    Product.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;
    
    }
    Book.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);
    
    
        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
    Book.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, float Price, 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;
        }
        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);
    }

  7. #157
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    At this case I need an abstract class.
    I updated the code. I'm not sure if I created in correct way the virtual class.
    You did it correctly as far as I can see.

    I don't get how can I ride off "Price" from constructor. Do I need create another constructor?
    What is stopping you from simply removing that argument from your current constructor?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #158
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    You did it correctly as far as I can see.
    My only doubt is the initilialization.
    I see in your examples and at web People using virtual class = 0.
    And I didn't do it.


    Quote Originally Posted by Elysia View Post
    What is stopping you from simply removing that argument from your current constructor?
    The compiler ! It won't compile.

  9. #159
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    My only doubt is the initilialization.
    I see in your examples and at web People using virtual class = 0.
    And I didn't do it.
    You're right. I missed that. You should append "= 0" to the virtual destructor to make the function pure virtual and the class abstract.

    The compiler ! It won't compile.
    Can't help you if you don't show your code that does not compile and list your compile errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #160
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    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);
    }

  11. #161
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    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.
    First off:
    If a book has a price set by label, then why would a comic book be different? If a comic book does not set a price depending on label, then is it really a book? Have you considered that your book might be a special case of a book?

    Now, let's think for a second what CalcPrice is and what it's supposed to do. What was it made for? Does it make sense for derived classes to call it?
    As for the constructor, you could add another protected constructor, but that leads back to the first question. Does it make sense to do so? Inheritance is usually supposed to add functionality to the existing hierarchy, not change it. First you say that a book has a price set depending on its label, and then you override that and say it depends on something else. That rings a bell to me saying that maybe this isn't right...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #162
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    First off:
    If a book has a price set by label, then why would a comic book be different? If a comic book does not set a price depending on label, then is it really a book? Have you considered that your book might be a special case of a book?

    Now, let's think for a second what CalcPrice is and what it's supposed to do. What was it made for? Does it make sense for derived classes to call it?
    As for the constructor, you could add another protected constructor, but that leads back to the first question. Does it make sense to do so? Inheritance is usually supposed to add functionality to the existing hierarchy, not change it. First you say that a book has a price set depending on its label, and then you override that and say it depends on something else. That rings a bell to me saying that maybe this isn't right...
    I'd like ComicBook's price set by label. ComicBook is a book with 2 more attributes. The Universe and the Creator.
    At Book Class I'm able to calc price inherite the Product class.
    ComicBook is a Book which is a Product.
    In order to set a price to the comicBook do I need to inherite the Product?
    Last edited by marcoesteves; 09-30-2014 at 05:03 AM.

  13. #163
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I'd like ComicBook's price set by label. ComicBook is a book with 2 more attributes. The Universe and the Creator.
    Then it's an extension of a book, so inherit from book.

    ComicBook is a Book which is a Product.
    A comic book may be a product, but that's irrelevant modelling in this system. A Comic book is a book, and thus you should inherit only from Book. Comic book should not care what Book inherits from or does differently. This lowers coupling and protects Comic book from changes.

    In order to set a price to the comicBook do I need to inherite the Product?
    No. It's a book, so inherit from book. Though we don't actually need to care, because Book inherits from Product, a Comic Book is also a Product (and indeed, contains all the stuff from Product).
    Since a comic book is a book, we let the book set the pricing. Because we are saying that a comic book is a book and we've said previously that a book handles pricing, you should let book set the price, i.e. comic book should call the appropriate book constructor and let it do the pricing.
    Again, if comic book requires a different pricing scheme, it may be wrong to inherit from book in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #164
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Then it's an extension of a book, so inherit from book.


    A comic book may be a product, but that's irrelevant modelling in this system. A Comic book is a book, and thus you should inherit only from Book. Comic book should not care what Book inherits from or does differently. This lowers coupling and protects Comic book from changes.


    No. It's a book, so inherit from book. Though we don't actually need to care, because Book inherits from Product, a Comic Book is also a Product (and indeed, contains all the stuff from Product).
    Since a comic book is a book, we let the book set the pricing. Because we are saying that a comic book is a book and we've said previously that a book handles pricing, you should let book set the price, i.e. comic book should call the appropriate book constructor and let it do the pricing.
    Again, if comic book requires a different pricing scheme, it may be wrong to inherit from book in the first place.
    It makes all sense.
    However, If the Book Constructor doesn't have the price, how I can set the ComicBook Price as a Label function?

  15. #165
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did I not say that comic book should let the book constructor take care of that? Comic book is an extension of a book - hence it shouldn't care about its price.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning C++
    By sLIVER in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2007, 08:18 PM
  2. Learning
    By nojoegohome in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2006, 04:33 AM
  3. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM
  4. Learning?
    By bob20 in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-11-2002, 10:17 AM

Tags for this Thread