Can we create a base class object from derived class?

Can we create a base class object from derived class?

No, that’s not possible since assigning it to a derived class reference would be like saying “Base class is a fully capable substitute for derived class, it can do everything the derived class can do”, which is not true since derived classes in general offer more functionality than their base class (at least, that’s …

What happens when a reference to a derived class object is assigned to a base class reference variable?

In C++, a derived class object can be assigned to a base class object, but the other way is not possible. Object slicing doesn’t occur when pointers or references to objects are passed as function arguments since a pointer or reference of any type takes same amount of memory.

In which case parameterized constructor of the base class Cannot be called?

The parameterized constructor of base class cannot be called in default constructor of sub class, it should be called in the parameterized constructor of sub class.

Which of the following statement is correct base class pointer Cannot point to derived class pointer Cannot point to base class?

d)Pointer to base class cannot be created. Correct answer is option ‘B’.

Can a derived class be initialized in a base class?

No. you can’t initialize the derived instance in base class object. One of the options is to serialize the base class object and then deserialize it to derived class.

How can I initialize base class member variables in..?

The base class members are already initialized by the time your derived-class constructor gets to run. You can assign them, if you have access, or call setters for them, or you can supply values for them to the base class constructor, if there is one suitable. The one thing you cannot do in the devised class is initialize them.

Do you declare B New with an initializer?

B new has the fields of both classes. However, you should declare them with an initializer unless you like nulls: I hope I’m not the only one who thinks swapping out your base class is a bad design pattern. Another approach is to replace inheritance with composition:

How to initialize a base class in C + +?

Since class B inherits from A, there must be a way to just assign A to the B.base In C++ this would be a trivial thing to do, but I can’t seem to grasp how to do this in .NET Unfortunately base is readonly. Well perhaps not so unfortunate. The relationship between a base class and a child class is IS-A not HAS-A.