danzylrabago

INHERITANCE2.CS

Jun 7th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. using System;
  2.  
  3. class Shape {
  4. protected int length;
  5. public Shape() { //default constructor
  6. Console.WriteLine("Shape's default constructor");
  7. }
  8. public Shape(int length) { //constructor with parameters
  9. this.length = length;
  10. Console.WriteLine("Shape's constructor with 1 parameter");
  11. Console.WriteLine(this.length);
  12. }
  13. }
  14.  
  15. class Square: Shape {
  16. public Square(): base() { //calling Shape class default constructor using base
  17. Console.WriteLine("Square's default constructor");
  18. }
  19. public Square(int length): base(length) { //calling Shape class constructor with parameters using base
  20. Console.WriteLine("Sqaure's constructor with 1 parameter");
  21. Console.WriteLine(this.length);
  22. }
  23. public int get_Area(){ return (length * length); } //method computing area of square
  24. }
  25.  
  26. class Program {
  27. static void Main() {
  28. Square sq1 = new Square(); //making object of child class Sqaure
  29. Square sq2 = new Square(5); //setting length equal to 5
  30. Console.WriteLine("Area of sq1 is: {0}",sq1.get_Area());
  31. Console.WriteLine("Area of sq2 is: {0}",sq2.get_Area());
  32. }
  33. }
Add Comment
Please, Sign In to add comment