Home

The Object Class

 

Introduction

To assist you with starting a program, the C++/CLI language, along with the .NET Framework, ship with many created classes. You can use most of these classes directly in your program, especially those classes that are part of the System namespace. Many other classes belong to namespaces that are part of, or nested in, the System namespace. We will study other classes in future lessons.

An Object as a Handle

The most fundamental class of the C++/CLI language and the .NET Framework is called Object. You can declare an Object handle using the formulas we have reviewed in this and the previous lesson. Here is an example:

using namespace System;

int main()
{	
    Object ^ home;

    return 0;
}

After creating an Object handle, you can initialize it with just about any value. Here are examples:

using namespace System;

int main()
{	
    Object ^ Integer   = 97834;
    Object ^ Real      = 274.55;
    Object ^ Character = L'G';

    return 0;
}

Notice that the first variable is initialized with a natural number, the second is initialized with a decimal number, and the third is initialized with a character. Because the value of an Object variable can be vague, use it only if you don't know or cannot find out what type of value you want to use. Since the other data types we reviewed in the previous lessons are more precise and we now know how to create a class, use those types and techniques instead.

Just as you can declare a member variable of a class as a regular data type, you can use the Object class to create a member of a class.

You can also declare a member variable that is of type Object.

Inheriting from the Object Class

All managed classes that you create using either the value of the ref keywords are derived from the Object class. If you want, you can reinforce this by explicitly inheriting from Object. Here is an example:

public ref class Classification : public Object
{
public:
	long PropertyNumber;
	int  Condition;
};

Practical LearningPractical Learning: Inheriting From the Object Class

  1. To inherit from the Object class, change the file as follows:
     
    using namespace System;
    
    namespace RealEstate
    {
        public ref class CProperty : public Object
        {
        public:
            long PropertyNumber;
    	String ^ Address;
    	String ^ City;
    	String ^ State;
    	String ^ ZIPCode;
    	int Bedrooms;
    	float Bathrooms;
    	int YearBuilt;
    	double MarketValue;
        };
    
        public ref class CHouse : public CProperty
        {
        public:
    	Byte Stories;
    	int  Condition;
    	__wchar_t Style;
        };
    }
    
    . . .
  2. Execute the application to see the result
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next