Child class reading parent class methods – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#4676    siteicon   siteicon  

Child class reading parent class methods

Understanding how a child class object can read the parent class methods/functions easily.

Learning Objectives

  • Learning how child class can read parent class methods or functions once it is inherited with public accessibility.

Source Code

TC++ #4676

 

Source Code

Run Output

Some birds can fly up to 11300 meters.
Some birds can have max wing span of 4 meters.

Code Understanding

class FlyingThing { public: int max_height(){return 11300;}};
A class which is parent class of class Bird in this case. It has a method to return max_height a bird can fly.

class Bird:public FlyingThing { public: int max_wing_spread(){return 4;}};
A derived class which is derived from its parent class FlyingThing. It has a method which returns max-wing spread a bird can spread.

int main() {
Bird b;
Declaring of an object of child class only. Parent class object is not declared.

cout<<“Some birds can fly up to ” <<b.max_height()<<” meters.”<<endl;
Here we are using method max_height of parent class with object of child class. This is possible due to inheritance only.

cout<<“Some birds can have max wing span of “<<b.max_wing_spread()<<” meters.”<<endl;
Here we are using method max_wing_spread of child class only. This is possible to use here being it to be a public access.

return 0;
}

Notes

  • public accessibility is important while inheriting the child class in this type of code. Other accessibility may pose certain limits.


Suggested Filename(s): smpl-inh2.cpp,simple-inheritance2.cpp,child-reading-parent-methods.cpp



Share

sunmitra| Created: 15-Sep-2018 | Updated: 15-Sep-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back