Q:

C++ Program to find Area of Rectangle using inheritance

belongs to collection: C++ Inheritance Solved Programs

0

Write a C++ Program to find Area of Rectangle using inheritance. Here’s a Simple C++ Program to find Area of Rectangle using inheritance in C++ Programming Language.

What are Inheritance in C++ ?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

  • Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
  • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
  • The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Types of Inheritance : :

There are different types of inheritance : :

 
 
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid (Virtual) Inheritance

Below is the source code for C++ Program to find Area of Rectangle using inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C++ Program to find Area of Rectangle using inheritance  */

#include<iostream>
using namespace std;
class Rectangle
{
     private:
        float length ; // This can't be inherited
     public:
        float breadth ; // The data and member functions are inheritable
        void Enter_lb(void)
        {
               cout << "\n Enter the length of the rectangle : "; 
                     cin >> length ;
               cout << "\n Enter the breadth of the rectangle : ";            
               cin >> breadth ;
        }
        float get_l(void)
        {      return length ; } 
};  // End of the class definition

class Rectangle1 : public Rectangle   
{      
     private:
        float area ;
      public:
        void Rec_area(void)
        {  area = get_l( ) * breadth ;  } 
        // area = length * breadth ;  can't be used here
void Display(void)
     {
       cout << "\n Length = " << get_l( ) ; // Object of the derived class can't
              //    inherit the private member of the base class. Thus the member
              //   function is used here to get the value of data member 'length'.
       cout << "\n Breadth = " << breadth ;
       cout << "\n Area = " << area  ;
     }
};  // End of the derived class definition D
int main()
{
        Rectangle1 r1 ;
        r1.Enter_lb( );
        r1.Rec_area( );
        r1.Display( );
return 0;
}

OUTPUT : :


/*  C++ Program to find Area of Rectangle using inheritance  */

 Enter the length of the rectangle : 5

 Enter the breadth of the rectangle : 8

 Length = 5
 Breadth = 8
 Area = 40

Process returned 0

Above is the source code and output for C++ Program to find Area of Rectangle using inheritance which is successfully compiled and run on Windows System to produce desired output.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to show access to Private Public and P... >>
<< C++ Program display Student Marksheet using Multip...