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
9
votes
3 answers

C++ member-function chaining return types and derived classes

Given this contrived example: struct point_2d { point_2d& x( int n ) { x_ = n; return *this; } point_2d& y( int n ) { y_ = n; return *this; } int x_, y_; }; struct point_3d : point_2d { point_3d& z( int n ) { z_ =…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
9
votes
1 answer

Using declaration (Derived class)

struct B1{ int d; void fb(){}; }; struct B2 : B1{ using B1::d; using B1::fb; int d; // why this gives error? void fb(){} // and this does not? }; int main(){} Is it because, B1::fb() is treated as B1::fb(B1*)…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
9
votes
3 answers

Pass this to base constructor

I am trying to implement good design patterns for a program I am writing. I have a class structure like this. abstract class SomeBase { public SomeObject obj { get; protected set; } protected SomeBase(SomeObject x) { obj…
guitar80
  • 716
  • 6
  • 19
8
votes
3 answers

Adding virtual removes the error : type 'base' is not a direct base of derived class

Conside the following sample code below: #include using namespace std; class base { public: base() { cout << "ctor in base class\n"; } }; class derived1 : public base { public: derived1() { …
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
8
votes
6 answers

Can you override private functions defined in a base class?

I believe, a derived class can override only those functions which it inherited from the base class. Is my understanding correct.? That is if base class has a public member function say, func, then the derived class can override the member function…
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
8
votes
5 answers

Constructor and Destructor Inheritance

I believe Constructors and Destructors in base class cannot be inherited by derived classes of the base class. Is my understanding correct.
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
8
votes
2 answers

Simpler "Preventing derived classes" in C++

Going under the assumption that there is a legitimate reason for preventing derivation from some class, Bjarne gives a solution here for the answer to "Can I stop people deriving from my class?" However, I thought of: class final { protected: …
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
8
votes
5 answers

Why in C++ 'virtual' and '=0' is both needed to describe a method is abstract?

As it is explained in The C++ programming language: virtual void push(char c) = 0; virtual void pop() = 0; The word virtual means 'may be redefined later in a class derived from this one' The =0 syntax says that some class derived from Stack must…
hwding
  • 840
  • 10
  • 22
8
votes
2 answers

C++ class template parameter must have a specific parent class

Given is a class MyClass with one template parameter template class MyClass { //... }; and another class MySecondClass with two template parameters. template class MySecondClass { //... }; What I would…
user1056903
  • 921
  • 9
  • 26
8
votes
1 answer

In Python, how to avoid calling __init__ twice in a class derived from a class with super() in its __new__:

I am new to python. Somehow __init__ is called twice for a class that is derived from another class using super() My question is how to avoid this because I have a very expensive computation there. class A(object): def __new__(cls, *args,…
Ahsan Zeb
  • 91
  • 1
  • 5
8
votes
2 answers

ES6 constructor returns instance of base class?

The constructor of a derived class returns an instance of the base class. The following code explains my problem: // Vector is defined by an external module (Unreal.js) class TestB extends Vector { constructor() { super(); } …
arthur.sw
  • 11,052
  • 9
  • 47
  • 104
8
votes
7 answers

How to force a derived class to include certain properties with default value

I have a class structure for a role playing game which looks like this... public abstract class Item { public abstract string Name { get; set; } } public abstract class Armor : Item { public override string Name { get; set; } } public class…
mikedev
  • 733
  • 9
  • 16
8
votes
3 answers

Why does List implement so many interfaces?

List derives from the following interfaces: public class List : IList, ICollection, IEnumerable, IList, ICollection, IEnumerable I just wonder, why it needs all these interfaces (in the class declaration)? IList itself already…
Andy
  • 3,997
  • 2
  • 19
  • 39
8
votes
2 answers

Cast lambda expression to derived type

I need a little piece of magic. I believe what I am trying to do makes sense, but if it I've not seen a problem with the plan the reasons why would be just as welcome. I have an expression Expression> and I want to cast/convert…
MJM
  • 431
  • 4
  • 14
8
votes
5 answers

is it bad programming practice to place abstract classes inside the same package containing their derived classes?

Assuming that A, B, C derive from AbstractBaseClass and they are all part of the same java project, are package structures of the form... package com.whatever.### AbstractBaseClass.java package com.whatever.###.### A.java B.java C.java ...…
John Smith
  • 4,416
  • 7
  • 41
  • 56