Questions tagged [derived-class]

In Object Oriented languages, derived class can inherit properties and/or member functions from a base class, also called super class. cf inheritance and polymorphism.

In Object Oriented languages, derived class can inherit properties and/or member functions from a base class, also called super class. cf inheritance and polymorphism.

1192 questions
153
votes
16 answers

Why does calling a method in my derived class call the base class method?

Consider this code: class Program { static void Main(string[] args) { Person person = new Teacher(); person.ShowInfo(); Console.ReadLine(); } } public class Person { public void ShowInfo() { …
user1968030
112
votes
33 answers

Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?. I have tried it and it creates a run-time error.
Maddy.Shik
  • 6,609
  • 18
  • 69
  • 98
74
votes
3 answers

How to dynamically create a derived type in the Python C-API

Assume we have the type Noddy as defined in the tutorial on writing C extension modules for Python. Now we want to create a derived type, overwriting only the __new__() method of Noddy. Currently I use the following approach (error checking…
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
67
votes
4 answers

Why we do create object instance from Interface instead of Class?

I have seen an Interface instance being generated from a class many times. Why do we use interface this way? An interface instance is created only itself with the help of the derived class and we can access only these interface members through this…
user2282567
  • 781
  • 1
  • 7
  • 14
61
votes
6 answers

How to define sealed class in C++?

How to stop the class to be inherited by other class.
Shashi
  • 2,860
  • 2
  • 34
  • 45
60
votes
9 answers

Cast base class to derived class python (or more pythonic way of extending classes)

I need to extend the Networkx python package and add a few methods to the Graph class for my particular need The way I thought about doing this is simplying deriving a new class say NewGraph, and adding the required methods. However there are…
zenna
  • 9,006
  • 12
  • 73
  • 101
59
votes
7 answers

how to get derived class name from base class

I have a base class Person and derived classes Manager and Employee. Now, what I would like to know is the object created is Manager or the Employee. The person is given as belows: from Project.CMFCore.utils import getToolByName schema =…
Sadiksha Gautam
  • 5,032
  • 6
  • 40
  • 71
49
votes
5 answers

C++ Access derived class member from base class pointer

If I allocate an object of a class Derived (with a base class of Base), and store a pointer to that object in a variable that points to the base class, how can I access the members of the Derived class? Here's an example: class Base { public: …
Peter
  • 493
  • 1
  • 5
  • 5
45
votes
5 answers

Error : base class constructor must explicitly initialize parent class constructor

I am new to c++. When I try to compile the code below , I get this error constructor for 'child' must explicitly initialize the base class 'parent' which does not have a default constructor child::child(int a) { here is my class…
charlotte
  • 1,031
  • 3
  • 12
  • 19
44
votes
4 answers

Why doesn't a derived template class have access to a base template class' identifiers?

Consider: template class Base { public: static const bool ZEROFILL = true; static const bool NO_ZEROFILL = false; } template class Derived : public Base { public: Derived( bool initZero…
cheshirekow
  • 4,797
  • 6
  • 43
  • 47
41
votes
4 answers

Calling the base class constructor from the derived class constructor

I have a question: Say I have originally these classes which I can't change (let's say because they're taken from a library which I'm using): class Animal_ { public: Animal_(); int getIdA() { return idA; }; string…
Joy
  • 1,707
  • 7
  • 29
  • 44
39
votes
6 answers

Delete virtual function from a derived class

I have a virtual base class function which should never be used in a particular derived class. Is there a way to 'delete' it? I can of course just give it an empty definition but I would rather make its attempted use throw a compile-time error. …
Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
36
votes
11 answers

Disabling inherited method on derived class

Is there any way to, in a Java derived class, "disable" a method and/or field that is otherwise inherited from a base class? For example, say you have a Shape base class that has a rotate() method. You also have various types derived from the Shape…
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
33
votes
4 answers

In C# 4.0, is it possible to derive a class from a generic type parameter?

I've been trying this, but I can't seem to figure this out. I want to do this... public abstract class SingletonType : TBaseClass where TSingleton : TBaseClass, new() where TBaseClass : class { static TSingleton…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
32
votes
1 answer

template and derived class definition : error: 'myClass' is not a class, namespace, or enumeration

I'm trying to learn templates in C++ and I have the following code : #include template class myClass : public std::stack{ public: myClass(void); myClass(myClass const & src); virtual ~myClass(void); myClass…
vmonteco
  • 14,136
  • 15
  • 55
  • 86
1
2 3
79 80