Questions tagged [base-class]

In Object Oriented Programming, a base class is one from which other classes inherit. For example, a child-class `Male` and another child-class `Female` may both inherit from the base-class `Human`.

In Object Oriented Programming, a base class is one from which other classes inherit. For example, a child-class Male and another child-class Female may both inherit from the base-class Human. As a result of such an inheritance, each of them will have at least all the attributes and methods of the class Human and perhaps a few others.

678 questions
1621
votes
37 answers

Creating a singleton in Python

This question is not for the discussion of whether or not the singleton design pattern is desirable, is an anti-pattern, or for any religious wars, but to discuss how this pattern is best implemented in Python in such a way that is most pythonic. In…
theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
835
votes
38 answers

Interface vs Base class

When should I use an interface and when should I use a base class? Should it always be an interface if I don't want to actually define a base implementation of the methods? If I have a Dog and Cat class. Why would I want to implement IPet instead…
Howler
  • 2,252
  • 6
  • 22
  • 27
169
votes
11 answers

Does delete on a pointer to a subclass call the base class destructor?

I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class (class B. When I'm done with an object of class B, I call delete, which I assume calls the…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
136
votes
8 answers

C# Class naming convention: Is it BaseClass or ClassBase or AbstractClass

What is the recommended approach to naming base classes? Is it prefixing the type name with "Base" or "Abstract" or would we just suffix it with "Base"? Consider the following: type: ViewModel e.g. MainViewModel, ReportViewModel base class:…
Soni Ali
  • 18,464
  • 16
  • 44
  • 53
133
votes
7 answers

Will the base class constructor be automatically called?

class Person { public int age; public Person() { age = 1; } } class Customer : Person { public Customer() { age += 1; } } Customer customer = new Customer(); Would the age of customer be 2? It seems…
Ivan Li
  • 1,850
  • 4
  • 19
  • 23
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
10 answers

How to hide an inherited property in a class without modifying the inherited class (base class)?

If i have the following code example: public class ClassBase { public int ID { get; set; } public string Name { get; set; } } public class ClassA : ClassBase { public int JustNumber { get; set; } public ClassA() { …
Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75
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
37
votes
5 answers

GCC issue: using a member of a base class that depends on a template argument

The following code doesn't compile with gcc, but does with Visual Studio: template class A { public: T foo; }; template class B: public A { public: void bar() { cout << foo << endl; } }; I get the…
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
35
votes
2 answers

How to avoid error "Constructor on type 'MyType' not found" when inheriting a base class

I have a Visual Studio 2010 Windows Forms app which includes a Form base class that other classes will inherit. The base class' constructor takes a parameter that the child classes will pass to the base class. Example: public partial class BaseForm…
Jed
  • 10,649
  • 19
  • 81
  • 125
32
votes
9 answers

.NET: Unable to cast object to interface it implements

I have a class (TabControlH60) that both inherits from a base class (UserControl) and implements an interface (IFrameworkClient). I instantiate the object using the .NET Activator class. With the returned instance, I can cast to the UserControl base…
user193327
  • 341
  • 1
  • 3
  • 4
31
votes
8 answers

How to call an explicitly implemented interface-method on the base class

I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly: interface I { int M(); } class A : I { int I.M() { return 1; } } class B : A, I { int I.M() { return 2; } } From the derived…
M4N
  • 94,805
  • 45
  • 217
  • 260
31
votes
1 answer

conversion to inaccessible base class is not allowed

I define a class B1 and a derived class D1 at first. Then I want to define a reference to B1 and initialize that to the D1 object I just defined. Here comes the error, saying that "conversion to inaccessible base class 'B1' is not allowed", which I…
Cheng Lu
  • 429
  • 1
  • 4
  • 8
31
votes
3 answers

How to resolve "pure virtual method called"

I understand why this is happening, but I'm stuck trying to resolve it...here is what my code is doing when the error is generated (thus, leading to a crash) when my program exits... pure virtual method called SomeClass::~SomeClass() { …
user869525
  • 769
  • 2
  • 12
  • 21
1
2 3
45 46