Return an object : Class Definition « Class Interface « C# / C Sharp






Return an object

Return an object
 
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Return an object. 
 
using System; 
 
class Rect { 
  int width; 
  int height; 
 
  public Rect(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  public int area() { 
    return width * height; 
  } 
 
  public void show() { 
    Console.WriteLine(width + " " + height); 
  } 
 
  /* Return a rectangle that is a specified 
     factor larger than the invoking rectangle. */ 
  public Rect enlarge(int factor) { 
    return new Rect(width * factor, height * factor); 
  } 
} 
  
public class RetObj { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
 
    Console.Write("Dimensions of r1: "); 
    r1.show(); 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine(); 
 
    // create a rectange that is twice as big as r1 
    Rect r2 = r1.enlarge(2); 
 
    Console.Write("Dimensions of r2: "); 
    r2.show(); 
    Console.WriteLine("Area of r2 " + r2.area()); 
  } 
}


           
         
  








Related examples in the same category

1.simulate a bank account
2.Using Initializers
3.A simple inventory exampleA simple inventory example
4.Declaring and Defining Classes
5.Declaring Class Instances
6.Assign value to classAssign value to class
7.Declare class and use itDeclare class and use it
8.Illustrates how to declare classes, object references, and create objectsIllustrates how to declare classes, object references, and create objects
9.Illustrates how to assign default values to fields using initializersIllustrates how to assign default values to fields using initializers
10.illustrates how to use a 'has a' relationshipillustrates how to use a 'has a' relationship
11.Illustrates nested classesIllustrates nested classes
12.Multiple constructors in a class definitionMultiple constructors in a class definition
13.Demonstrate the use of a nested class to contain dataDemonstrate the use of a nested class to contain data
14.Show name hiding in a derived classShow name hiding in a derived class
15.Illustrates hidingIllustrates hiding
16.Variable in and out a classVariable in and out a class
17.Create classCreate class
18.A program that uses the Building classA program that uses the Building class
19.This program creates two Building objectsThis program creates two Building objects
20.Uses a class from Example16_3a.cs
21.A Simple C# ClassA Simple C# Class
22.Persons Info class
23.Message class
24.Point class