CSharp examples for Custom Type:Constructor
Circle class with overloaded constructors
using System;//from w ww.ja v a 2 s.co m public class Circle { public int x; public int y; public int radius; private const float PI = 3.14159F; public double area() { double theArea; theArea = PI * radius * radius; return theArea; } public double circumference() { double Circ; Circ = 2 * PI * radius; return Circ; } public Circle() { x = 0; y = 0; radius = 0; } public Circle( int r ) { x = 0; y = 0; radius = r; } public Circle ( int new_x, int new_y ) { x = new_x; y = new_y; radius = 0; } public Circle ( int new_x, int new_y, int r ) { x = new_x; y = new_y; radius = r; } public void print_circle_info() { Console.WriteLine("Circle: Center = ({0},{1})", x, y); Console.WriteLine(" Radius = {0}", radius); Console.WriteLine(" Area = {0}", area()); Console.WriteLine(" Circum = {0}", circumference()); } } class MainClass { public static void Main() { Circle first = new Circle(); Circle second = new Circle(4); Circle third = new Circle(3,4); Circle fourth = new Circle(1, 2, 5); Console.WriteLine("\nFirst Circle:"); first.print_circle_info(); Console.WriteLine("\nSecond Circle:"); second.print_circle_info(); Console.WriteLine("\nThird Circle:"); third.print_circle_info(); Console.WriteLine("\nFourth Circle:"); fourth.print_circle_info(); } }