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
8
votes
1 answer

C++ copy of two derived class

I have a base class and two derived class, and I need to copy a pointer to an object of the derived class to one of the other class, like the example. class Base { public: Base(const Base& other); } class Derived1 :public Base { public: …
Pablosproject
  • 1,374
  • 3
  • 13
  • 33
8
votes
2 answers

C++ Initialize base class' const int in derived class?

I have a constant int variable in my base class, and I would like to initialize responding one in my derived class, with different value (taken as a parameter), is this possible? Here's what I did: // Base.h (methods implemented in Base.cpp in the…
user1632861
7
votes
5 answers

How to detect template parameters of base classes at compile time (for errors)?

I've been using the Curiously recurring template pattern The general code looks like this: template void genericFunction(T &); template struct Functionality { void genericMethod() { genericFunction(*((T *)this))…
Gearoid Murphy
  • 11,834
  • 17
  • 68
  • 86
7
votes
1 answer

C++ calling template functions of Base class

Below are two cases. Case 1) Base->BaseIndirect->DerivedIndirect Case 2) Base->Derived In Case 2), I am able to call a template function of Base class using 3 notations. In Case 1), I am able to call template function of Base class using only 1…
anon
  • 73
  • 1
  • 3
7
votes
2 answers

Calling an overridden function from a base class?

My question will probably be best explained by an example. For example, I have 2 classes: A base class and a derived class: class baseClass { public: baseClass() { foo(); } virtual bool foo() { printf("baseClass"); return…
Brad
  • 10,015
  • 17
  • 54
  • 77
7
votes
3 answers

Optional Parameters in Abstract method? Is it possible?

I have a abstract base class. I have 2 derived classes from this base class. Is there anyway that one of my classes can ignore the string parameter in the abstract overide usage? Or do I have to just send in a blank one and ignore it? (making…
jordan
  • 3,436
  • 11
  • 44
  • 75
7
votes
3 answers

Can I pass arguments to a base constructor from a derived class's default constructor?

Suppose I have an abstract base class Deck: public abstract class Deck { public List cards; public Deck(string[] values, string[] suits) {...} ... } and a derived class EuchreDeck: public class EuchreDeck : Deck { string[]…
Zach
  • 437
  • 3
  • 10
  • 27
7
votes
3 answers

How to implement a boost::variant derived-class?

I have tried for hours to code a class deriving from boost::variant. But I do not understand what is the problem (I do not understand what the compilation error means). What are the rules to implement a clean boost::variant derived-class? #include…
oHo
  • 51,447
  • 27
  • 165
  • 200
7
votes
5 answers

Why derived class function argument takes value of base class function argument?

I'm working on C++. Following is my code: #include using namespace std; class base { public: virtual void display(int a = 4) { cout << "base :: "<< a*a << endl; } }; class derived :…
BSalunke
  • 11,499
  • 8
  • 34
  • 68
6
votes
1 answer

QWidget derived class is not visible

I can create and see a QWidget in one of the functions of main window class: .. // ok QWidget *w = new QWidget(this); w->setGeometry(400,300,400,300); w->setStyleSheet("background-color:white;"); w->show(); .. but when I try to do something similar…
sithereal
  • 1,656
  • 9
  • 16
6
votes
2 answers

Move semantics in derived-to-base class conversions

Consider the following class Buffer, which contains an std::vector object: #include #include class Buffer { std::vector buf_; protected: Buffer(std::byte val): buf_(1024, val) {} }; Now, consider the function…
JFMR
  • 23,265
  • 4
  • 52
  • 76
6
votes
1 answer

About pointer to member function of derived class

Here's my code, and the IDE is DEV C++11 #include using namespace std; class A{ public: int a=15; }; class B:public A { }; int main(){ int A::*ptr=&B::a; //OK int B::*ptr1=&A::a; //why? int…
BooAA
  • 173
  • 3
  • 11
6
votes
8 answers

C# derived classes, overload resolution

Ok, I have an some different objects that are derived from a base class and I've put a bunch of them in a list. I want to loop through the list and push each to a method. I have separate methods with each one's type signature, but the compiler is…
end-user
  • 2,845
  • 6
  • 30
  • 56
6
votes
3 answers

Virtual function performance when called by derived classes?

Is there a performance penalty when a virtual method is called from a class that's known to be the derived class at compile time? Below I explicitly call force_speak with a derived class. Code: #include #include #include…
Agrim Pathak
  • 3,047
  • 4
  • 27
  • 43
6
votes
2 answers

copy & swap in base and derived class

I recently read about copy & swap and am now trying to implement the ctors in a base and derived class. I have the four constructors in both my base and derived class, however I am unsure how to implement the assignment operator of the derived…
gartenriese
  • 4,131
  • 6
  • 36
  • 60