Monday, January 18, 2021
Home C / C++ C++ inheritance

C++ inheritance

lets have a look at the below example:

  1. // C++ inheritance
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. class Point
  6. {
  7. protected:
  8.     int x,y;
  9. public:
  10.     Point(int ,int);
  11.     void display(void);
  12. };
  13.  
  14. Point::Point(int a,int b)
  15. {
  16.     x=a;
  17.     y=b;
  18. }
  19.  
  20. void Point::display(void)
  21. {
  22.      cout<<“point = [“ <<x<<“,”<<y<<“]”;
  23. }
  24.  
  25. class Circle : public Point
  26. {
  27.     double radius;
  28. public:
  29.     Circle(int ,int ,double );
  30.     void display(void);
  31. };
  32.  
  33. Circle::Circle(int a,int b,double c):Point(a,b)
  34. {
  35.     radius = c;
  36. }
  37.  
  38. void Circle :: display(void)
  39. {
  40.     Point::display();
  41.     cout<<” and radius = “<<radius;
  42. }
  43.  
  44. int main(void)
  45. {
  46.     Circle c(3,4,2.5);
  47.     c.display();
  48.     cout<<endl;
  49.     return 0;
  50. }

In the above we took a class “point” in which we defined its attributes (its data members and member functions), then we inherited(derived) the class “circle” from it, Now in main() function we called an abject of circle class, now it will have the attributes of “circle” as well as “point”.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....