Basics of OOP using c++
Rate this article
0 out of 5
In this tutorial we will show you how to use Basics of OOP using c++







#include <iostream>
using namespace std;

class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
};

class Rectangle: public Polygon {
  public:
    int area()
      { return width*height; }
};

class Triangle: public Polygon {
  public:
    int area()
      { return width*height/2; }
};

int main () {
  Rectangle rect;
  Triangle trgl;
  Polygon * ppoly1 = &rect;
  Polygon * ppoly2 = &trgl;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  cout << rect.area() << ' ';
  cout << trgl.area() << ' ';
  return 0;
}


source code

   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

Copyright © KTS Infotech 2000-2023. Site Developed Using KTS WebCloud
Reach Us on facebookWatch Video Tutorials on YouTube Channel TekTipsNavigate to KTS Technology Blog