polymorphism

Is it considered a good practice to define virtual get and set functions in C++?

佐手、 提交于 2021-01-27 06:28:56
问题 If I have a simple 2 level class hierarchy as, for instance, this one: // level 1 class Spare_Part{ private: string name; double price; public: Spare_Part(); string getName() { return name; } double getPrice() { return price; } virtual int getQuantity() { return -1; }; // may also define it as pure virtual }; //level 2 class On_hand : public Spare_Part{ private: int quantity; string location; public: On_hand(); int getQuantity(){ return quantity; } }; I want to have access to the member

Is it considered a good practice to define virtual get and set functions in C++?

拥有回忆 提交于 2021-01-27 06:27:07
问题 If I have a simple 2 level class hierarchy as, for instance, this one: // level 1 class Spare_Part{ private: string name; double price; public: Spare_Part(); string getName() { return name; } double getPrice() { return price; } virtual int getQuantity() { return -1; }; // may also define it as pure virtual }; //level 2 class On_hand : public Spare_Part{ private: int quantity; string location; public: On_hand(); int getQuantity(){ return quantity; } }; I want to have access to the member

Why and when use polymorphism?

孤人 提交于 2021-01-21 07:44:43
问题 I'm new to OOP and polymorphism has given me a hard time: class Animal { public virtual void eat() { Console.Write("Animal eating"); } } class Dog : Animal { public override void eat() { Console.Write("Dog eating"); } } class Program { public void Main() { Animal dog = new Dog(); Animal generic = new Animal(); dog.eat(); generic.eat(); } } So that prints Dog eating Animal eating But why not to just use the Dog type instead of animal, like Dog dog = new Dog()? I assume this comes handy when

Why and when use polymorphism?

本秂侑毒 提交于 2021-01-21 07:43:00
问题 I'm new to OOP and polymorphism has given me a hard time: class Animal { public virtual void eat() { Console.Write("Animal eating"); } } class Dog : Animal { public override void eat() { Console.Write("Dog eating"); } } class Program { public void Main() { Animal dog = new Dog(); Animal generic = new Animal(); dog.eat(); generic.eat(); } } So that prints Dog eating Animal eating But why not to just use the Dog type instead of animal, like Dog dog = new Dog()? I assume this comes handy when

Policy class design but without making the whole user class a template

痞子三分冷 提交于 2021-01-19 06:35:32
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Policy class design but without making the whole user class a template

ぃ、小莉子 提交于 2021-01-19 06:32:52
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Policy class design but without making the whole user class a template

梦想的初衷 提交于 2021-01-19 06:32:02
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Policy class design but without making the whole user class a template

試著忘記壹切 提交于 2021-01-19 06:28:33
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Polymorphic value types and interfaces

ⅰ亾dé卋堺 提交于 2020-12-30 02:33:25
问题 I have a polymorphic value type implemented like so: class ShapeValue { public: template<class T> ShapeValue(const T& value) { obj = make_unique<holder<T>>(value); } // ... appropriate copy constructors and such void draw() { obj->draw(); } private: struct base { virtual ~base() {} virtual void draw() = 0; }; template<class T> struct holder<T> : public base { T value; void draw() override { value.draw(); } } unique_ptr<base> obj; }; If you aren't familiar with this sort of thing, here's a

Polymorphic value types and interfaces

痞子三分冷 提交于 2020-12-30 02:33:13
问题 I have a polymorphic value type implemented like so: class ShapeValue { public: template<class T> ShapeValue(const T& value) { obj = make_unique<holder<T>>(value); } // ... appropriate copy constructors and such void draw() { obj->draw(); } private: struct base { virtual ~base() {} virtual void draw() = 0; }; template<class T> struct holder<T> : public base { T value; void draw() override { value.draw(); } } unique_ptr<base> obj; }; If you aren't familiar with this sort of thing, here's a