Answers to Chapter 18 Review Questions

   


Chapter 18

1:

List three kinds of data abstractions where structs should be considered instead of classes.

A:
  1. To represent fractions (with a numerator and a denominator

  2. To represent points on a map (2D) or points in space (3D)

  3. To represent a time span

2:

Mention the important similarities and differences between classes and structs.

A:

Similarities: structs and classes can both contain methods, properties, indexers, constructors, data members, operator methods, nested types; both can implement interfaces; and they both implicitly inherit from the System.Object class.

Differences: Structs cannot contain destructors; their constructors work in a slightly different way. Structs form value types, whereas classes form reference types; structs don't support inheritance, and structs are stored more efficiently in memory.

3:

If myStruct is a value type, how is the following call possible if WriteLine only accepts reference types?

 Console.WriteLine("Details of myStruct" + myStruct); 
A:

As part of the process of being passed as an argument to WriteLine, myStruct is boxed into a reference value, which then can be processed by WriteLine.

4:

Can a struct have an explicit default constructor?

A:

No.

5:

Why is boxing called boxing?

A:

One can imagine boxing as the process of putting a struct value into a box that is of a reference type.

6:

Suppose the struct Fraction contains the two public data members numerator and denominator. Fraction does not contain any explicitly defined constructors. You now write the following code:

 Fraction myFraction; myFraction.ToString(); 

Is the second line valid? Why or why not?

A:

No, the second line is not valid. Fraction does not contain any explicitly defined constructors, so the numerator and denominator values have not been initialized by the time ToString is called in the second line. This is invalid because no methods, properties, or indexers can be called for a struct value that contains uninitialized data members.

Answers to Chapter 18 Programming Exercises

1:

Implement a struct called Fraction that contains two private data members of type int called numerator and denominator. Equip Fraction with the following elements:

  • A constructor that takes two arguments to initialize numerator and denominator

  • Properties to get and set numerator and denominator

  • A property called Value that simply returns the value of the fraction, which is calculated as (numerator / denominator)

  • Override the ToString method (implicitly inherited from System.Object) to return the following string: "Fraction value: xxx" where xxx represents the fraction value as a string.

Write suitable code to test the Fraction struct.

A:

Exercise 1:

 using System; struct Fraction {     private int numerator;     private int denominator;     public Fraction(int initNumerator, int initDenominator)     {         numerator = initNumerator;         denominator = initDenominator;     }     public int Numerator     {         get         {             return numerator;         }         set         {             numerator = value;         }     }     public int Denominator     {         get         {             return denominator;         }         set         {             denominator = value;         }     }     public double Value     {         get         {             return (double)numerator / (double)denominator;         }     }     public override string ToString()     {         string returnString;         returnString = "Fraction value: " + Value;         return returnString;     } } class Tester {     public static void Main()     {         Fraction myFraction = new Fraction(1, 3);         Console.WriteLine(myFraction);     } } 


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

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