-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtual_methods.cs
157 lines (125 loc) · 3.55 KB
/
Virtual_methods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
class Figura2D {
double pri_width;
double pri_height;
public Figura2D() {
Width = Height = 0.0;
name = "null";
}
public Figura2D(double w, double h, string n) {
Width = w;
Height = h;
name = n;
}
public Figura2D(double x, string n) {
Width = Height = x;
name = n;
}
public Figura2D(Figura2D ob) {
Width = ob.Width;
Height = ob.Height;
name = ob.name;
}
public double Width {
get { return pri_width; }
set { pri_width = value < 0 ? -value : value; }
}
public double Height {
get { return pri_height; }
set { pri_height = value < 0 ? -value : value; }
}
public string name { get; set; }
public void ShowDim() {
Console.WriteLine("La anchura y la altura son " +
Width + " y " + Height);
}
public virtual double Area() {
Console.WriteLine("Se debe hacer override al método Area()");
return 0.0;
}
}
class Triangle : Figura2D {
string Style;
public Triangle() {
Style = "null";
}
public Triangle(string s, double w, double h) :
base(w, h, "triángulo") {
Style = s;
}
public Triangle(double w, double h, string s) :
base(w, h, "triángulo equilatero") {
Style = s;
}
public Triangle(double w, string s, double h) :
base(w, h, "triángulo rectangulo") {
Style = s;
}
public Triangle(double x) : base(x, "triángulo isosceles") {
Style = "isosceles";
}
public Triangle(Triangle ob) : base(ob) {
Style = ob.Style;
}
public override double Area() {
return Width * Height / 2;
}
public void ShowStyle() {
Console.WriteLine("Triángulo es " + Style);
}
}
class Circulo : Figura2D {
string Style;
public Circulo() {
Style = "null";
}
public Circulo(string s, double w, double h) :
base(w, h, "circulo") {
Style = s;
}
public Circulo(Circulo ob) : base(ob) {
Style = ob.Style;
}
public override double Area() {
return Width * (Height*Height);
}
public void ShowStyle() {
Console.WriteLine("Circulo es " + Style);
}
}
class Rectangle : Figura2D {
public Rectangle(double w, double h) :
base(w, h, "rectángulo"){ }
public Rectangle(double x) :
base(x, "cuadrado") { }
public Rectangle(Rectangle ob) : base(ob) { }
public bool IsSquare() {
if(Width == Height) return true;
return false;
}
public override double Area() {
return Width * Height;
}
}
class Cuadrado : Rectangle {
public Cuadrado(double x) :
base(x) { }
}
class Program {
static void Main() {
Figura2D[] shapes = new Figura2D[8];
shapes[0] = new Triangle("derecho", 7.0, 12.0);
shapes[1] = new Triangle(7.0, 12.0,"buenas");
shapes[2] = new Triangle(7.0, "hmm", 12.0);
shapes[3] = new Triangle(7.0);
shapes[4] = new Cuadrado(10);
shapes[5] = new Rectangle(10, 4);
shapes[6] = new Circulo("hola",3.1416, 4);
shapes[7] = new Figura2D(10, 20, "genérica");
for(int i=0; i < shapes.Length; i++) {
Console.WriteLine("El objeto es " + shapes[i].name);
Console.WriteLine("El área es " + shapes[i].Area());
Console.WriteLine();
}
}
}