You are creating a class named Age.

You are creating a class named Age.
You need to ensure that the Age class is written such that collections of Age objects can be
sorted. Which code segment should you use?

A. public ref class Age {
    public : Int32 Value;
    public : virtual Object CompareTo(Object^ obj) {
    if (obj->GetType() == Age::GetType()) {
    Age^ _age = (Age^) obj;
    return Value.CompareTo(obj);
    }
    throw gcnew ArgumentException(“object not an Age”);
    }};
B. public ref class Age {
    public : Int32 Value;
    public : virtual Object CompareTo(Int32^ iValue) {
    try {
    return Value.CompareTo(iValue);
    } catch (Exception^ ex) {
    throw gcnew ArgumentException(“object not an Age”);
    }
    }};
C. public ref class Age : public IComparable {
    public : Int32 Value;
    public : virtual Int32 CompareTo(Object^ obj) {
    if (obj->GetType() == Age::GetType()) {
    Age^ _age = (Age^) obj;
    return Value.CompareTo(_age->Value);
    }
    throw gcnew ArgumentException(“object not an Age”);
    }};
D. public ref class Age : public IComparable {
    public : Int32 Value;
    public : virtual Int32 CompareTo(Object^ obj) {
    try {
    return Value.CompareTo(((Age^) obj)->Value);
    } catch (Exception^ ex) {
    return -1;
    }
    }};


 Answer: C 

No comments:

Post a Comment