Child class reading parent class data member – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#4673    siteicon   siteicon  

Child class reading parent class data member

Understanding how a child class object read the parent class data member easily.

Learning Objectives

  • Learning how child class can read parent class data member once it is inherited with public accessiblity.

Source Code

TC++ #4673

 

Source Code

Run Output

No. of Walls = 4
Bed in Room = 1

Code Understanding

class Room
{
public:
int walls;
Room(){walls=4;}
};
This class is parent class and it defines a data member walls that is initialised with value 4 using a constructor.

class BedRoom:public Room
{
public:
int beds;
BedRoom(){beds=1;}
};
This class is child class inherited from class Room and it defines a data member beds that is initialised with value 1 using a constructor.

int main() {
BedRoom b;
Here we are defining the object of child class only.

cout<<“No. of Walls = “<<b.walls<<endl;
Here we are printing the member of parent class using the data member of parent class. This is possible due to inheritance only.

cout<<“Bed in Room = “<<b.beds<<endl;
Here we are printing the member of child class itself using its own data member which is possible as it is a public member.

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-inh.cpp,simple-inheritance.cpp,child-reading-parent.cpp



Share

sunmitra| Created: 9-Sep-2018 | Updated: 9-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 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 Mult-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