C++ inheritance

0
52
Creating a Timer in C++
4 Big Mistakes to Avoid When Learning Programming

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”.

Creating a Timer in C++
4 Big Mistakes to Avoid When Learning Programming

Leave a Reply