The syntax for composition is obvious, but to perform inheritance there’s a new and different form.When you inherit, you are saying, “This new class is like that old class.” You state this in code by giving the name of the class as usual, but before the opening brace of the class body, you put a colon and the name of the base class (or base classes, separated by commas, for multiple inheritance). When you do this, you automatically get all the data members and member functions in the base class. Here’s an example: //: C14:Inheritance.cpp // Simple inheritance #include "Useful.h" #include <iostream> using namespace std; class Y : public X { int i; // Different from X's i public: Y() { i = 0; } int change() { i = permute(); // Different name call return i; } void set(int ii) { i = ii; X::set(ii); // Same-name function call } }; int main() { cout << "sizeof(X) = " << sizeof(X) << endl; cout << "sizeof(Y) = " << sizeof(Y) << endl; Y D; D.change(); // X function interface comes through: D.read(); D.permute(); // Redefined functions hide base versions: D.set(12); } ///:~You can see Y being inherited from X, which means that Y will contain all the data elements in X and all the member functions in X. In fact, Y contains a subobject of X just as if you had created a member object of X inside Y instead of inheriting from X. Both member objects and base class storage are referred to as subobjects.All the private elements of X are still private in Y; that is, just because Y inherits from X doesn’t mean Y can break the protection mechanism. The private elements of X are still there, they take up space – you just can’t access them directly.In main( ) you can see that Y’s data elements are combined with X’s because the sizeof(Y) is twice as big as sizeof(X).You’ll notice that the base class is preceded by public. During inheritance, everything defaults to private. If the base class were not preceded by public, it would mean that all of the public members of the base class would be private in the derived class. This is almost never what you want[51]; the desired result is to keep all the public members of the base class public in the derived class. You do this by using the public keyword during inheritance.In change( ), the base-class permute( ) function is called. The derived class has direct access to all the public base-class functions.The set( ) function in the derived class redefines the set( ) function in the base class. That is, if you call the functions read( ) and permute( ) for an object of type Y, you’ll get the base-class versions of those functions (you can see this happen inside main( )). But if you call set( ) for a Y object, you get the redefined version. This means that if you don’t like the version of a function you get during inheritance, you can change what it does. (You can also add completely new functions like change( ).)However, when you’re redefining a function, you may still want to call the base-class version. If, inside set( ), you simply call set( ) you’ll get the local version of the function – a recursive function call. To call the base-class version, you must explicitly name the base class using the scope resolution operator.
C++
Topic: Inheritance
Inheritance syntax
Browse random answers:
Explain the ISA and HASA class relationships. How would you implement each in a class design?
Does c++ support multilevel and multiple inheritance?
What is RTTI?
What is the difference between composition and inheritance?
What is an accessor?
what is the use of volatile keyword? Give me one example?
Why Call by reference technique is used in case of Copy constructor & not call by value?
Is there any way to write a class such that no class can be inherited from it. Please include code
What do you mean by inheritance?
What does inheritance means in C++? What are the different forms of inheritance? Give an example for each.
How to create object for singleton class?
it possible to inherit the private member in drived class?
Can you prevent your class from being inherited and becoming a base class for some other classes?
what are the advantages and disadvantages of inheritance?
What is the difference between superclass and subclass?
Whether there will be a default constructor provided by the compiler in above case ?class A(){};int main(){A a;}
When should you use multiple inheritance?
What is a modifier?
What is a mutable member?
If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?
Can you allow class to be inherited, but prevent the method from being over-ridden?
how to select access modifier & what's need of access modifier?
Can you inherit multiple interfaces?
how many types of Inheritence ?
explain the real word example of multilevel inheritance?
Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. Does c++ support multilevel and multiple inheritance?
When would I use inheritance?
How do you express inheritance in C++?
Is it OK to convert a pointer from a derived class to its base class?
What's the difference between public, private, and protected?
Why can't my derived class access private things from my base class?
How can I protect derived classes from breaking when I change the internal parts of the base class?
I've been told to never use protected data, and instead to always use private data with protected access functions. Is that a good rule?
What is inherited from the base class?
Explain about Inheritance between classes ?
Explain about Base & Derived Classes ?
Explain about the Access Control and Inheritance ?
How many Type of Inheritance ?
What are the FORMS OF INHERITANCE ?
Explain about execution of base class constructor ?
What are the Overriding of method(function) in inheritance ?
Explain about Virtual Base Class ?
Why java does not support Multiple Inheritance?
What are Encapsulation, Inheritance and Polymorphism?
What is the object-oriented design,inheritance and dynamic polymorphism in c++?
When should you use multiple inheritance?
What is public, protected, private in C++?
Should I hide member functions that were public in my base class?
Converting Derived* → Base* works OK; why doesn't Derived** → Base** work?
What would be printed out – and in what order? execution order of the constructor and destructor in inheritance.
Inheritance syntax
Explain about Inheritance Overview ?
Explain about Key Properties of C++ Inheritance