Contents
- 1 Can you assign a base class object to a derived class reference?
- 2 What happens when a derived class is assigned to a reference to its base class?
- 3 Which members can be accessed in derived class from the base class?
- 4 Can super class reference variable can hold an object of sub class?
- 5 What is base class for convert?
Can you assign a base class object to a derived class reference?
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 derived class is assigned to a reference to its base class?
Object slicing happens when a derived class object is assigned to a base class object, additional attributes of a derived class object are sliced off to form the base class object. For example, if we change the global method myfunc() in the above program to following, object slicing doesn’t happen.
What is base class reference?
then a base class reference refers to any variable defined as Parent pObj (here the name pObj doesn’t matter), a base class object refers to an object created as new Parent() and a derived class object refers to one created as new Child() .
Which members can be accessed in derived class from the base class?
A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Constructors, destructors and copy constructors of the base class.
Can super class reference variable can hold an object of sub class?
Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).
What is the syntax of inheritance of class?
Which is the correct syntax of inheritance? Explanation: Firstly, keyword class should come, followed by the derived class name. Colon is must followed by access in which base class has to be derived, followed by the base class name. And finally the body of class.
What is base class for convert?
Convert class provides different methods to convert a base data type to another base data type. The base types supported by the Convert class are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime, and String.