Does the following program contain errors?

class A {};
class B: A {};
struct C {};
struct D: C {};

int main(void) {
  A *clA = new B();  //1
  B *clB = new B();  //2
  C *stC = new D();  //3
  D *stD = new D();  //4
  delete clA; 
  delete clB;
  delete stC;
  delete stD;
  return 0;
}
Explanation
The base (parent) data type, from which the new one should be inherited, has a private access modifier by default for the class, and a public one for the struct (section 11.2 on p. 181 of the "International standard ISO/IEC 14882" document). Therefore, the A data type is used within the B data type, it is impossible to cast B data type to a private A data type.
C++
C++ Quiz
Start Quiz

or Read more about C++ Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback