Inheritance

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.

 

Questions by desirocks   answers by desirocks

Showing Answers 1 - 37 of 37 Answers

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.

  Was this answer useful?  Yes

yzesong

  • Jul 29th, 2009
 

C++ struct can not be inherited is FALSE. Here is what I coded:

struct A{int a;
void print(void) {
cout << "Hello from struct A: a = " << a << endl;
};
};

class B: public A{
public:
B(int arg1, int arg2) { b = arg1; a = arg2;};
int b;
void printA(void) {
cout << "Hello from Class B: b = " << b << " ;; a = " << a << endl;
};
};

int main(int argc, char* argv[]) {
   A a;
  a.a = 10;
  a.print();

  B* b = new B(20, 30);
  b->printA();
  return 0;
}

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.


arunv111285

  • Feb 10th, 2010
 

Inheritance actually promotes polymorphism. Reuse of code should be done using aggregation/composition.

thus a) is the wrong about inheritance.

  Was this answer useful?  Yes

abhimanipal

  • Feb 21st, 2010
 

The above answers are wrong. structs can be inherited in C++ . The only point to note is that for structs the defualt mode of inheritance for structs is public but for classes its private

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions