Last active
April 5, 2018 08:41
-
-
Save DipanshKhandelwal/f7fd190be3cd901d620c2b3aed5bab54 to your computer and use it in GitHub Desktop.
Object Oriented Programming with Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Created by Dipansh Khandelwal | |
// Github @DipanshKhandelwal | |
import 'dart:math'; | |
abstract class Shape { | |
List<double> sides; | |
double perimeter; | |
Shape(this.sides) { | |
this.perimeter = this.sides.reduce((a, b) => a + b); | |
} | |
double area(); | |
} | |
class Triangle extends Shape { | |
Triangle(double a, double b, double c) : super([a,b,c]); | |
double area() { | |
double s = this.perimeter/2; | |
return sqrt(s * (s - sides[0]) * (s - sides[1]) * (s - sides[2])); | |
} | |
} | |
class Rectangle extends Shape { | |
Rectangle(double a, double b) : super([a,b]); | |
double area() { | |
return sides[0]*sides[1]; | |
} | |
} | |
void main() { | |
Triangle triangle = new Triangle(3.0, 4.0, 5.0); | |
Rectangle rectangle = new Rectangle(4.0, 5.0); | |
print('Area of triangle is ${triangle.area()}, its perimeter is ${triangle.perimeter}'); | |
print('Area of rectangle is ${rectangle.area()}, its perimeter is ${rectangle.perimeter}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment