Single Level Inheritance
#include <iostream>
using namespace std;
/*
* Types:
* 1. Single-Level Inheritance
* 2. Multi-level
* 3. Multiple
* 4. Hybrid
*/
class Shape
{
private:
float width;
float height;
public:
Shape()
{
this->width = this->height = 0.0;
}
Shape(float width, float height)
{
this->width = width;
this->height = height;
}
float get_width()
{
return this->width;
}
float get_height()
{
return this->height;
}
float getArea()
{
return this->width * this->height;
}
};
class Square : public Shape
{
public:
Square() { }
Square(float side) : Shape(side, side)
{
}
float getPerimeter()
{
return 2 * this->get_width() + 2 * this->get_height();
}
};
class Rectangle : public Shape
{
public:
Rectangle() { }
Rectangle(float width, float height) : Shape(width, height)
{
}
float getPerimeter()
{
return 2 * (this->get_width() + this->get_height());
}
};
int main(int argc, char const *argv[])
{
Square s(10.0);
cout << "Area of square is " << s.getArea() << endl;
cout << "Perimeter of square is " << s.getPerimeter() << endl;
Rectangle r(10, 20);
cout << "Area of rectangle is " << r.getArea() << endl;
cout << "Perimeter of rectangle is " << r.getPerimeter() << endl;
return 0;
}
using namespace std;
/*
* Types:
* 1. Single-Level Inheritance
* 2. Multi-level
* 3. Multiple
* 4. Hybrid
*/
class Shape
{
private:
float width;
float height;
public:
Shape()
{
this->width = this->height = 0.0;
}
Shape(float width, float height)
{
this->width = width;
this->height = height;
}
float get_width()
{
return this->width;
}
float get_height()
{
return this->height;
}
float getArea()
{
return this->width * this->height;
}
};
class Square : public Shape
{
public:
Square() { }
Square(float side) : Shape(side, side)
{
}
float getPerimeter()
{
return 2 * this->get_width() + 2 * this->get_height();
}
};
class Rectangle : public Shape
{
public:
Rectangle() { }
Rectangle(float width, float height) : Shape(width, height)
{
}
float getPerimeter()
{
return 2 * (this->get_width() + this->get_height());
}
};
int main(int argc, char const *argv[])
{
Square s(10.0);
cout << "Area of square is " << s.getArea() << endl;
cout << "Perimeter of square is " << s.getPerimeter() << endl;
Rectangle r(10, 20);
cout << "Area of rectangle is " << r.getArea() << endl;
cout << "Perimeter of rectangle is " << r.getPerimeter() << endl;
return 0;
}
Comments
Post a Comment
Post Your Valuable Comments