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
18
votes
4 answers

C# private (hidden) base class

Is it possible to make a C# base class accessible only within the library assembly it's compiled into, while making other subclasses that inherit from it public? For example: using System.IO; class BaseOutput: Stream // Hidden base…
David R Tribble
  • 11,918
  • 5
  • 42
  • 52
17
votes
7 answers

How to Get Base Class Instance from a Derived Class

I don't know if this is possible, but I am trying to get the Base Class instance from a Derived Class. In C#, I can use the base keyword to access properties and methods of the Base Class (of course), but I want to use base itself. Attempting to do…
OneSource
  • 519
  • 3
  • 8
  • 23
16
votes
2 answers

Understanding virtual base classes and constructor calls

I'm a bit confused about how virtual base classes work. In particular, I was wondering how the constructor of the base class gets called. I wrote an example to understand it: #include #include using std::string; struct A{ …
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
16
votes
2 answers

Python inheritance - calling base class methods inside child class?

It baffles me how I can't find a clear explanation of this anywhere. Why and when do you need to call the method of the base class inside the same-name method of the child class? class Child(Base): def __init__(self): …
user1369281
  • 161
  • 1
  • 1
  • 3
14
votes
1 answer

Scala: How can I make my immutable classes easier to subclass?

I've recently created an immutable class supporting operations like +, -, etc. that returns a new instance of that class when it is changed. I wanted to make a subclass of that class to add a bit of state and functionality but now I'm running into a…
Dobes Vandermeer
  • 8,463
  • 5
  • 43
  • 46
14
votes
5 answers

C++ template duck-typing vs pure virtual base class inheritance

Which are the guidelines for choosing between template duck-typing and pure virtual base class inheritance? Examples: // templates class duck { void sing() { std::cout << "quack\n"; } }; template void somefunc(const bird& b) { …
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
14
votes
7 answers

Compiler warning at C++ template base class

I get a compiler warning, that I don't understand in that context. When I compile the "Child.cpp" from the following code. (Don't wonder: I stripped off my class declarations to the bare minimum, so the content will not make much sense, but you will…
eike
  • 141
  • 1
  • 5
13
votes
1 answer

Why does the compiler select the base class constructor inside the template argument list?

Follow-up question to this one. Basically, in the following code, why does the compiler think that the B inside A in Cs constructor refer to the (inaccessible) constructor of the B base class? struct B{}; template struct A : private…
Xeo
  • 129,499
  • 52
  • 291
  • 397
13
votes
3 answers

C++: Accessing parent methods and variables

In which way should I access this parent method and parent variable? class Base { public: std::string mWords; Base() { mWords = "blahblahblah" } }; class Foundation { public: Write( std::string text ) { std::cout << text; …
user542687
12
votes
2 answers

Call derived class method from base class reference

class Material { public: void foo() { cout << "Class Material"; } }; class Unusual_Material : public Material { public: void foo() { cout << "Class Unusual_Material"; } }; int main() { Material strange = Unusual_Material(); …
user487100
  • 918
  • 1
  • 11
  • 22
12
votes
5 answers

Call a C++ base class method automatically

I'm trying to implement the command design pattern, but I'm stumbling accross a conceptual problem. Let's say you have a base class and a few subclasses like in the example below: class Command : public boost::noncopyable { virtual ResultType…
Dinaiz
  • 2,213
  • 4
  • 22
  • 32
12
votes
5 answers

C# protected members accessed via base class variable

It may seems rather newbie question, but can you explain why method Der.B() cannot access protected Foo via Base class variable? This looks weird to me: public class Base { protected int Foo; } public class Der : Base { private void B(Base…
Roman
  • 4,531
  • 10
  • 40
  • 69
12
votes
5 answers

C# "Rename" Property in Derived Class

When you read this you'll be awfully tempted to give advice like "this is a bad idea for the following reason..." Bear with me. I know there are other ways to approach this. This question should be considered trivia. Lets say you have a class…
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76
11
votes
3 answers

Order of calling base class constructor from derived class initialization list

struct B { int b1, b2; B(int, int); }; struct D : B { int d1, d2; // which is technically better ? D (int i, int j, int k, int l) : B(i,j), d1(k), d2(l) {} // 1st Base // or D (int i, int j, int k, int l) : d1(k), d2(l), B(i,j) {} //…
iammilind
  • 68,093
  • 33
  • 169
  • 336
11
votes
4 answers

c++ casting base class to derived class mess

If I were to create a base class called base and derived classes called derived_1, derived_2 etc... I use a collection of instances of the base class, then when I retrieved an element and tried to use it I would find that C++ thinks it's type is…
alan2here
  • 3,223
  • 6
  • 37
  • 62
1 2
3
45 46