Section A.7. Chapter 7: Classes and Objects


A.7. Chapter 7: Classes and Objects

A.7.1. Quiz



Solution to Question 71 .

A class defines a new type; an object is a single instance of that type.



Solution to Question 72 .

Instances of classes are reference types and are created on the heap.



Solution to Question 73 .

Intrinsic types (such as integers) and structs are value types and are created on the stack.



Solution to Question 74 .

Access is limited to methods of the defining class.



Solution to Question 75 .

Access is available to methods in any class.



Solution to Question 76 .

The class's constructor is called.



Solution to Question 77 .

A default constructor is a constructor that takes no parameters. If you do not create any constructor at all for your class, a default constructor is implicitly created.



Solution to Question 78 .

None. A constructor is not defined to return a type, and is not marked void.



Solution to Question 79 .

Either in the constructor, using assignment, or when the member variable is created:

 private int myVariable = 88; 

Technically, only the latter is truly initialization; assigning it in the constructor is not as efficient.



Solution to Question 710 .

this refers to the object itselfthe current instance of the class.



Solution to Question 711 .

A static method has no this reference. It does not belong to an instance; it belongs to the class and can only call other static methods.

You access a static method through the name of the class:

 Dog myDog = new Dog(  ); myDog.InstanceMethod(  ); Dog.StaticMethod(  ); 

Of course, from within any method (including static methods), you can instantiate a class, and then call methods on that instance.

You can even instantiate an instance of your own class, and then call any non-static method of that object, as we did with [static] Main( ) calling [non-static] Test( ) .



Solution to Question 712 .

The using statement automatically calls the dispose method on the object once the statement completes.

A.7.2. Exercises



Solution to Exercise 7-1 .

Write a program with a Math class that has four methods: Add , Subtract , Multiply , and Divide , each of which takes two parameters. Call each method from Main( ) .

 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 {    class Math    {       public int Add( int left, int right )       {          return left + right;       }       public int Subtract( int left, int right )       {          return left - right;       }       public int Multiply( int left, int right )       {          return left * right;       }       public float Divide( float left, float right )       {          return left / right;       }    }     // end class Math    class Program    {       static void Main( string[] args )       {          Math m = new Math(  );          int sum =         m.Add(3,5);          int difference =  m.Subtract(3,5);          int product =     m.Multiply(3,5);          float quotient =  m.Divide(3.0f, 5.0f);          Console.WriteLine(             "sum: {0}, difference: {1}, product: {2}, quotient: {3}",             sum, difference, product, quotient);       }    } } 



Solution to Exercise 7-2 .

Modify the program from Exercise 7-1 so that you do not have to create an instance of Math to call the four methods:

 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 {    class Math    {       static public int Add( int left, int right )       {          return left + right;       }       static public int Subtract( int left, int right )       {          return left - right;       }       static public int Multiply( int left, int right )       {          return left * right;       }       static public float Divide( float left, float right )       {          return left / right;       }    }     // end class Math    class Program    {       static void Main( string[] args )       {          int sum =         Math.Add( 3, 5 );          int difference =  Math.Subtract(3,5);          int product =     Math.Multiply(3,5);          float quotient =  Math.Divide(3.0f, 5.0f);          Console.WriteLine(             "sum: {0}, difference: {1}, product: {2}, quotient: {3}",             sum, difference, product, quotient);       }    } } 



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net