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
13
votes
5 answers

Pointer to array of base class, populate with derived class

If I have a base class, with only virtual methods and 2 derived classes from the base class, with those virtual methods implemented. How do I: // causes C2259 BaseClass* base = new BaseClass[2]; BaseClass[0] = new FirstDerivedClass; …
Deukalion
  • 2,516
  • 9
  • 32
  • 50
12
votes
5 answers

static abstract class

I need a way to create a static class where some constants can be case specific, but hard-coded. What I really want to do is have a class where several constants are provided when the class is extended - I want the 'constants' hard-coded. I figured…
David Božjak
  • 16,887
  • 18
  • 67
  • 98
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
3 answers

Why does my C++ subclass need an explicit constructor?

I have a base class that declares and defines a constructor, but for some reason my publicly derived class is not seeing that constructor, and I therefore have to explicitly declare a forwarding constructor in the derived class: class WireCount0…
Neil Steiner
  • 729
  • 1
  • 7
  • 17
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
5 answers

How could an instance of the base class hold an instance of the derived class?

I have been a .Net coder (can not say I am a programmer) for 2 years. There is one question that I can not understand for years, that is how could an instance of the base class hold an instance of the derived class? Suppose we have two…
NextStep
  • 1,035
  • 3
  • 14
  • 18
11
votes
2 answers

Deriving Class from Generic T

I have a parameterized hibernate dao that performs basic crud operations, and when parameterized is used as a delegate to fulfil basic crud operations for a given dao. public class HibernateDao implements GenericDao
bowsie
  • 1,275
  • 3
  • 15
  • 22
11
votes
1 answer

Why can’t protected members be used by friends of derived classes?

The C++ standard states in [class.access/1] (emphasis mine): A member of a class can be private; that is, its name can be used only by members and friends of the class in which it is declared. protected; that is, its name can be used only by…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
11
votes
2 answers

Accessing public static members of a base class specified as private

I'm learning C++. The documentation learn.microsoft.com/en-us/cpp/cpp/member-access-control-cpp says: When you specify a base class as private, it affects only nonstatic members. Public static members are still accessible in the derived…
KarlEL
  • 131
  • 6
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
11
votes
3 answers

Unable to cast Base class (data contract) to derived class

[DataContract] public class SearchCriteria { [DataMember] public string CountryID { get; set; } } [DataContract] public class CitySearchCriteria: SearchCriteria { [DataMember] public string CityID { get; set; } } I am creating…
Nirman
  • 6,715
  • 19
  • 72
  • 139
11
votes
4 answers

C# accessing protected member in derived class

I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); …
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
10
votes
2 answers

Serialization of derived objects without the xsi:type

I'm having a problem when serializing a Dictionary containing a list of derived objects. The serialized output contains where I would like the BaseAttributes to be…
dennis_ler
  • 659
  • 1
  • 9
  • 36
10
votes
6 answers

How to access protected method in base class from derived class?

Here is a sample of code that annoys me: class Base { protected: virtual void foo() = 0; }; class Derived : public Base { private: Base *b; /* Initialized by constructor, not shown here Intended to store a pointer on an…
Bernard Rosset
  • 4,523
  • 6
  • 27
  • 29
10
votes
5 answers

Why can't a Base class object be assigned to a Derived class object?

A derived class object can be assigned to a base class object in C++. Derived d; Base b = d; // It's Ok But why can't a base class object be assigned to a derived class object? Base b; Derived d = b; //Not Ok. Compiler give an error Edit: Sorry,…
msc
  • 33,420
  • 29
  • 119
  • 214
1 2
3
79 80