danzylrabago

inheritance.cs

Jun 7th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. using System;
  2.  
  3. // Base class
  4. class Shape {
  5. public Shape(){length = 0;} //default constructor
  6. public void setlength(int l) {length = l;}
  7. protected int length;
  8. }
  9.  
  10. // Derived class
  11. class Sqaure: Shape {
  12. public Sqaure() {length = 0;} //declaring and initializing derived class constructor
  13. public int get_Area(){ return (length * length); }
  14. }
  15.  
  16. class Program
  17. {
  18. static void Main(){
  19. Sqaure sq = new Sqaure(); //making object of child class Sqaure
  20. sq.setlength(5); //setting length equal to 5
  21. // Print the area of the object.
  22. Console.WriteLine("Total area of sqaure is: {0}",sq.get_Area());
  23. }
  24. }
Add Comment
Please, Sign In to add comment