Which one of the following statements regarding C++ class inheritance is FALSE?
a. Inheritance promotes generic design and code reuse. b. Struct cannot be inherited in C++.
c. C++ supports multiple inheritance. d. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass.
Total Answers and Comments: 9
Last Update: September 25, 2009 Asked by: desirocks
Structures can be inherited. They are almost similar to classes in C++. The only difference is that the default access specifier in struct is public while it is private in a class.
Now I wanted to mention what I observed. You can inherit a class in a structure but you cannot inherit a structure inside a class. You may want to try this out. Because I tried it out.
class A { public: A() { cout<<"IN A"; } int a; };
struct B : public A //This will work {
};
struct C { //int c; }
class D: public C //This won't work {
};
int main() {
D d; A abc; B bad; B.a 1; D.c 2; //This would not work system("PAUSE"); return 0; }
B. Structures cannot be inherited in C++ is False There are some major difference between Class and Structures in C++. 1. Class members are private by default but in Stuctures they are Public. 2. In case of Structures we cannot initialize variables during declaration while in Class we can. 3. Structures does not support polymorphism while Classes do.
The only difference between a class and a struct is a class has private access and private inheritance by default and a struct has public access and public inheritance by default. The only place I'm aware of that you can't replace class with struct is in a template definition. For example template where you can also use template but template is incorrect.
struct is a class keyword and anything you can do with a class you can do with struct. Virtual functions pure virtual functions inheritance data member initialization all of it there aren't any restrictions. You could literally go through a program and replace every instance of class except as noted previously with the equivalent struct definition.