polymorphism

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

 ̄綄美尐妖づ 提交于 2021-02-07 05:52:25
问题 One of my friends asked me "How Runtime Polymorphism is achieved in C++?" I answered "By Inheritance" He said "No, it can be achieved only using virtual functions". So I gave him an example of the following code :- #include<iostream> using namespace std; class A { public: int i; A(){i=100;} }; class B : public A { public: int j; B(){i = -1; j = 99;} }; void func(A& myA) { cout<<myA.i << endl; } int main() { B b; A* a = new B(); func(*a); func(b); delete a; return 0; } Here, function func()

jaxb - how to create XML from polymorphic classes

限于喜欢 提交于 2021-02-07 03:19:23
问题 I've just started using JAXB to make XML output from java objects. A polymorphism exists in my java classes, which seems to not working in JAXB. Below is the way how I tried to deal with it, but in the output I haven't expected field: fieldA or fieldB. @XmlRootElement(name = "root") public class Root { @XmlElement(name = "fieldInRoot") private String fieldInRoot; @XmlElement(name = "child") private BodyResponse child; // + getters and setters } public abstract class BodyResponse { }

jaxb - how to create XML from polymorphic classes

瘦欲@ 提交于 2021-02-07 03:19:15
问题 I've just started using JAXB to make XML output from java objects. A polymorphism exists in my java classes, which seems to not working in JAXB. Below is the way how I tried to deal with it, but in the output I haven't expected field: fieldA or fieldB. @XmlRootElement(name = "root") public class Root { @XmlElement(name = "fieldInRoot") private String fieldInRoot; @XmlElement(name = "child") private BodyResponse child; // + getters and setters } public abstract class BodyResponse { }

ES6 Classes ability to perform polymorphism

走远了吗. 提交于 2021-02-07 03:18:46
问题 I am trying to emulate polymorphism through ES6 Classes, in order to be able to better understand this theory. Concept is clear ( designing objects to share behaviors and to be able to override shared behaviors with specific ones ) but I am afraid my code above is not a valid polymorphism example. Due to my lack of experience, I appreciate if you answer these questions in a comprehensive way: The fact that both Classes have an equally named method, and that instances made from each Class get

jaxb - how to create XML from polymorphic classes

两盒软妹~` 提交于 2021-02-07 03:18:01
问题 I've just started using JAXB to make XML output from java objects. A polymorphism exists in my java classes, which seems to not working in JAXB. Below is the way how I tried to deal with it, but in the output I haven't expected field: fieldA or fieldB. @XmlRootElement(name = "root") public class Root { @XmlElement(name = "fieldInRoot") private String fieldInRoot; @XmlElement(name = "child") private BodyResponse child; // + getters and setters } public abstract class BodyResponse { }

ES6 Classes ability to perform polymorphism

…衆ロ難τιáo~ 提交于 2021-02-07 03:17:59
问题 I am trying to emulate polymorphism through ES6 Classes, in order to be able to better understand this theory. Concept is clear ( designing objects to share behaviors and to be able to override shared behaviors with specific ones ) but I am afraid my code above is not a valid polymorphism example. Due to my lack of experience, I appreciate if you answer these questions in a comprehensive way: The fact that both Classes have an equally named method, and that instances made from each Class get

ES6 Classes ability to perform polymorphism

旧时模样 提交于 2021-02-07 03:15:45
问题 I am trying to emulate polymorphism through ES6 Classes, in order to be able to better understand this theory. Concept is clear ( designing objects to share behaviors and to be able to override shared behaviors with specific ones ) but I am afraid my code above is not a valid polymorphism example. Due to my lack of experience, I appreciate if you answer these questions in a comprehensive way: The fact that both Classes have an equally named method, and that instances made from each Class get

When to prefer templated policy based design over non-templated inheritance based design

无人久伴 提交于 2021-02-06 20:43:03
问题 I am trying to understand the real requirement of the usage of templates for policy based design. Going through the new templated designs in C++ I found that policy based class design is a highly suggested way of design which allows you to 'plug-in' different behaviors from policy classes. A minimal example is the following (a shortened version of the wiki): template <typename LanguagePolicy> class HelloWorld : private LanguagePolicy { using LanguagePolicy::message; public: // Behaviour

C++ Polymorphism: from parent class to child [duplicate]

丶灬走出姿态 提交于 2021-02-06 20:01:42
问题 This question already has answers here : When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? (9 answers) Closed 6 years ago . In C++ we can convert child class pointer to parent, but is there any way to convert it back: from parent, which was obtained from child, give child class back? I mean: class Parent { ... }; class Child : public Parent { ... }; int main(int argc, char const *argv[]) { Child* child = new Child(); Parent* parent = child; Child* old_child =

C++ Polymorphism: from parent class to child [duplicate]

假装没事ソ 提交于 2021-02-06 19:57:25
问题 This question already has answers here : When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? (9 answers) Closed 6 years ago . In C++ we can convert child class pointer to parent, but is there any way to convert it back: from parent, which was obtained from child, give child class back? I mean: class Parent { ... }; class Child : public Parent { ... }; int main(int argc, char const *argv[]) { Child* child = new Child(); Parent* parent = child; Child* old_child =