write a c++ program which create a shape class and derive circle and square class from shape class.define approriate constructor for all three classes.define find area method in shape class and override this method in circle and square classes to find thir area.
Answers
Answered by
0
ircle.java
public class Circle extends Shape { private final double radius; final double pi = Math.PI; public Circle() { this(1); } public Circle(double radius) { this.radius = radius; } @Override public double area() { // A = π r^2 return pi * Math.pow(radius, 2); } public double perimeter() { // P = 2πr return 2 * pi * radius; } }
public class Circle extends Shape { private final double radius; final double pi = Math.PI; public Circle() { this(1); } public Circle(double radius) { this.radius = radius; } @Override public double area() { // A = π r^2 return pi * Math.pow(radius, 2); } public double perimeter() { // P = 2πr return 2 * pi * radius; } }
Similar questions