inheritance
Title
Question
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 12.6px; white-space-collapse: preserve; background-color: rgb(249, 249, 249);">Create a class Shape. Create two functions of the class as Area and Perimeter. Find the area and perimeter of various shapes like square, rectangle and circle.</span>
Advanced-Cpp Inheritance 08-09 min 10-20 sec
Answers:
Please specify the difficulty you are facing
#include <iostream>
using namespace std;
class Shape {
public:
void Area(double side) {
cout << "Square Area: " << side * side << endl;
}
void Area(double length, double width) {
cout << "Rectangle Area: " << length * width << endl;
}
void AreaCircle(double radius) {
cout << "Circle Area: " << 3.14 * radius * radius << endl;
}
void Perimeter(double side) {
cout << "Square Perimeter: " << 4 * side << endl;
}
void Perimeter(double length, double width) {
cout << "Rectangle Perimeter: " << 2 * (length + width) << endl;
}
void PerimeterCircle(double radius) {
cout << "Circle Perimeter: " << 2 * 3.14 * radius << endl;
}
};
int main() {
Shape s;
s.Area(5); // Square
s.Perimeter(5);
s.Area(4, 6); // Rectangle
s.Perimeter(4, 6);
s.AreaCircle(3); // Circle
s.PerimeterCircle(3);
return 0;
}
Login to add comment