Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Inheritance

Provides a way to create a new class from an existing class.
New class is a specialized version of the existing class.

  • Base Class(or Parent): inherited by child class.
  • Derived Class(or child): inherits from base class.
class Student{ //base class
//body
};

class UnderGrad : public Student{ //derived class
//body
};

Inheritance establishes an “is a” relationship between classes.

An object of the derived class “is a” object of the base class. For example:

  • An UnderGrad is Student.

A derived object has all​ characteristics of the base class.

An object of child class has:

  • All members defined in the child class.
  • All ​members declared in the parent class.

An object of child class can use:

  • All public members defined in the child class.
  • All public members defined in the parent class.

Protected Members

  • protected member access specification: similar to private, but accessible by objects of derived class.

Class Access Specifiers

  • public: the object of the derived class can be treated as an object of the base class.
  • protected: more restrictive than public, but allows derived classes to know details of parents.
  • private: prevents objects of the derived class to be treated as objects of base class.
#include <iostream>
using namespace std;

// Base class
class Shape {
public:
Shape(){length = 0; width = 0;} //default constructor
void setlength(int l, int w) {length = l; width = w;}
protected:
int length, width;
};

// Derived class
class Square: public Shape {
public:
Square() : Shape() {} //declaring and initializing derived class constructor
int get_Area(){ return (length * width); }
};

int main(void) {
Square sq; //making object of child class Square
sq.setlength(5, 5); //setting length equal to 5
// Print the area of the object.
cout << "Total area of square is: " << sq.get_Area() << endl;

return 0;
}

As you can see in the example above,

  • The shape class is the parent class whereas the Square class is the child class derived from it.
  • In our child class Square, we use members from the parent class such as
    • the protected length and width variable which gets initialized to zero in the default constructor.
    • Length and width also gets used in child class function get_Area to compute the area of the square.
  • In main the setlength function which is a public member function of the parent class is accessible to the child class object sq
    • The dot operator is used to access setlength in the main.

Try the following example in the editor below.

With reference to above example, create a derived class Rectangle inherited from class Shape, your task is to create a public function in it named get_Perimeter which return the perimeter of rectange.

Start solving Inheritance on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test