inheritance

Multiple Inheritance

末鹿安然 提交于 2020-01-11 08:01:10
问题 #include<iostream> using namespace std; class A { int a; int b; public: void eat() { cout<<"A::eat()"<<endl; } }; class B: public A { public: void eat() { cout<<"B::eat()"<<endl; } }; class C: public A { public: void eat() { cout<<"C::eat()"<<endl; } }; class D: public B, C { }; int foo(A *ptr) { ptr->eat(); } main() { D obj; foo(&(obj.B)); //error. How do i call with D's B part. } The above foo call is a compile time error. I want to call foo with obj's B part without using virtual

Object.create changes prototype.constructor to parent constructor, but upon child instantiation, child constructor runs

烂漫一生 提交于 2020-01-11 06:49:15
问题 I've created an example to illustrate: // this is the parent class function animal() { console.log('animal constructor') } // allow animals to walk animal.prototype.walk = function() { console.log('animal walking') } // create child class function cat() { console.log('cat constructor') } // cat inherits from animal cat.prototype = Object.create(animal.prototype); // let cats meow cat.prototype.meow = function() { console.log('meow') } // create a cat object var myCat = new cat(); /* output:

Should I extend an ArrayList (is-a) or should I include it as a member (has-a)?

痴心易碎 提交于 2020-01-11 05:13:41
问题 I'm making a simple program that maintains a list of numbers, and I want this list to also have a name. Which is the best approach: have my list class extend ArrayList or have it include an ArrayList member? In both cases, there would of course be a "name" String member. The first approach means I only have to implement a getter & setter for the name, but I think this would tie my class too closely to a particular implementation? For example, if I wanted to later use a Vector, than I would

Inheritance threading.Thread class does not work

元气小坏坏 提交于 2020-01-11 04:00:09
问题 I'm new in multithreading, so the answer is probably very simple. I'm trying to make two instances of one class and run them parallel. I've read that I can use class inheritance to do that. class hello(threading.Thread): def __init__(self,min,max): threading.Thread.__init__(self) time.sleep(max) for i in range(1000): print random.choice(range(min,max)) h = hello(3,5) k = hello(0,3) I've noticed that this does not work (the first outputs are numbers between 3 and 5) Could you explain what am I

Java inheritance

ぃ、小莉子 提交于 2020-01-11 00:57:16
问题 Why does is print last "I'm a Child Class." ? public class Parent { String parentString; public Parent() { System.out.println("Parent Constructor."); } public Parent(String myString) { parentString = myString; System.out.println(parentString); } public void print() { System.out.println("I'm a Parent Class."); } } public class Child extends Parent { public Child() { super("From Derived"); System.out.println("Child Constructor."); } public void print() { super.print(); System.out.println("I'm a

Exception and Inheritance in Java

风格不统一 提交于 2020-01-10 23:24:11
问题 Suppose we have this problem public class Father{ public void method1(){...} } public class Child1 extends Father{ public void method1() throws Exception{ super.method1(); ... } } Child1 extends Father and overrides method1 but given the implementation Child1.method1 now throws a exception. This won't compile as the overriding method can't throw new exceptions. What is the best solution? Propagate the required exception to the Father . To me this is against encapsulation, inheritance and

If derived class inherits the private members of a base class, then why not constructors?

对着背影说爱祢 提交于 2020-01-10 20:12:47
问题 I want to clear my understanding of this basic OOPS concept in c#. On most of the internet sites, I read that a derived class inherits the private members of a base class, but it cannot access those members. A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived

Counting With Template Metaprogramming?

梦想的初衷 提交于 2020-01-10 19:13:23
问题 I've been trying to think up a creative solution to this problem (on and off) for some time, but I have not as of yet been able to. I recently considered that it might be solvable with template metaprogramming, though I am not sure due to my relative lack of experience with the technique. Is it possible to use template metaprogramming (or any other mechanism with the C++ language) to count the number of classes which are derived from some base class such that each derived class is given a

Counting With Template Metaprogramming?

让人想犯罪 __ 提交于 2020-01-10 19:11:47
问题 I've been trying to think up a creative solution to this problem (on and off) for some time, but I have not as of yet been able to. I recently considered that it might be solvable with template metaprogramming, though I am not sure due to my relative lack of experience with the technique. Is it possible to use template metaprogramming (or any other mechanism with the C++ language) to count the number of classes which are derived from some base class such that each derived class is given a

How does inheritance of instance fields work in this particular code?

拈花ヽ惹草 提交于 2020-01-10 07:32:08
问题 class A { int a = 2, b = 3; public void display() { int c = a + b; System.out.println(c); } } class B extends A { int a = 5, b = 6; } class Tester { public static void main(String arr[]) { A x = new A(); B y = new B(); x.display(); y.display(); } } Why does the output come out as 5,5? And not 5,11?.How would the y.display() method work? 回答1: why does the output comes 5,5? Because A.display() only knows about the fields A.a and A.b . Those are the only fields that any code in A knows about. It