Home

Classes Combinations and Inheritance

 

Class Combinations

 

A Class as a Member Variable 

Like a regular data type, you can use a class to create a member variable of another class. The primary rules to follow are the same as those of a primitive type. Here is an example:

public value class Classification
{
public:
	long PropertyNumber;
	int  Condition;
	__wchar_t Style;
};

public value class CHouse
{
public:
	Classification Class;
	__wchar_t TypeOfHome;
	int Bedrooms;
	double Bathrooms;
	Byte Stories;
	int YearBuilt;
	double Value;
};

After declaring the variable, to access its members, if you had declared it like a regular type, you can access a member using the period operator. This allows you either retrieve the value of the member or to modify it. Here are examples:

using namespace System;

public value class Classification
{
public:
	long PropertyNumber;
	int  Condition;
	__wchar_t Style;
};

public value class CHouse
{
public:
	Classification Class;
	__wchar_t TypeOfHome;
	int Bedrooms;
	double Bathrooms;
	Byte Stories;
	int YearBuilt;
	double Value;
};

int main()
{
	CHouse ^ condo = gcnew CHouse;

	condo->Class.PropertyNumber = 697234;
	condo->Class.Condition = 2;
	condo->Class.Style = L'H';

	condo->YearBuilt = 2002;
	condo->Bathrooms = 1;
	condo->Stories = 18;
	condo->Value = 155825;
	condo->Bedrooms = 2;
	condo->TypeOfHome = L'C';

	Console::Write("Property #:          ");
	Console::WriteLine(condo->Class.PropertyNumber);
	Console::Write("Condition:           ");
	Console::WriteLine(condo->Class.Condition);
	Console::Write("Style:               ");
	Console::WriteLine(condo->Class.Style);
	Console::Write("Type of Home:        ");
	Console::WriteLine(condo->TypeOfHome);
	Console::Write("Number of Bedrooms:  ");
	Console::WriteLine(condo->Bedrooms);
	Console::Write("Number of Bathrooms: ");
	Console::WriteLine(condo->Bathrooms);
	Console::Write("Number of Stories:   ");
	Console::WriteLine(condo->Stories);
	Console::Write("Year Built:          ");
	Console::WriteLine(condo->YearBuilt);
	Console::Write("Monetary Value:      ");
	Console::WriteLine(condo->Value);

	Console::WriteLine();
	return 0;
}

This would produce:

Property #:          697234
Condition:           2
Style:               H
Type of Home:        C
Number of Bedrooms:  2
Number of Bathrooms: 1
Number of Stories:   18
Year Built:          2002
Monetary Value:      155825

Press any key to continue . . .

Most of the time, you will declare the member variable as a handle type. To do this, type the ^ operator between the class type and the name of the member variable. Here is an example:

public value class CHouse
{
public:
	Classification ^ Class;
	__wchar_t TypeOfHome;
	int Bedrooms;
	double Bathrooms;
	Byte Stories;
	int YearBuilt;
	double Value;
};

This time, to access a member of that class, you can use the -> operator. Because the member variable is a handle, you must appropriately initialize it on the managed heap. Otherwise the program will not compile. There are various ways you can initialize the variable. From what we have learned so far, you can first create an object of that type, allocated it on the managed heap, and initialize it as you see fit. Here is an example:

int main()
{
	CHouse ^condo = gcnew CHouse;

	Classification ^ type = gcnew Classification;

	type->PropertyNumber = 697234; // Unique Number
	type->Condition      = 2;      // Good, may need minor repair
	type->Style          = L'H';   // High-rise

	return 0;
}

Once the object is ready, you can assign it to the member variable that needs the values:

int main()
{
	CHouse ^condo = gcnew CHouse;

	Classification ^ type = gcnew Classification;

	type->PropertyNumber = 697234; // Unique Number
	type->Condition      = 2;      // Good, may need minor repair
	type->Style          = L'H';   // High-rise
	condo->Class         = type;

	return 0;
}

When accessing the member variables of that object, remember to use the -> opera tor. Here are examples:

using namespace System;

public value class Classification
{
public:
	long PropertyNumber;
	int  Condition;
	__wchar_t Style;
};

public value class CHouse
{
public:
	Classification ^ Class;
	__wchar_t TypeOfHome;
	int Bedrooms;
	double Bathrooms;
	Byte Stories;
	int YearBuilt;
	double Value;
};

int main()
{
	CHouse ^condo = gcnew CHouse;

	Classification ^ type = gcnew Classification;

	type->PropertyNumber = 697234; // Unique Number
	type->Condition      = 2;      // Good, may need minor repair
	type->Style          = L'H';   // Highrise
	condo->Class         = type;

	condo->YearBuilt = 2002;
	condo->Bathrooms = 1;
	condo->Stories = 18;
	condo->Value = 155825;
	condo->Bedrooms = 2;
	condo->TypeOfHome = L'C';

	Console::Write("Property #:          ");
	Console::WriteLine(condo->Class->PropertyNumber);
	Console::Write("Condition:           ");
	Console::WriteLine(condo->Class->Condition);
	Console::Write("Style:               ");
	Console::WriteLine(condo->Class->Style);
	Console::Write("Type of Home:        ");
	Console::WriteLine(condo->TypeOfHome);
	Console::Write("Number of Bedrooms:  ");
	Console::WriteLine(condo->Bedrooms);
	Console::Write("Number of Bathrooms: ");
	Console::WriteLine(condo->Bathrooms);
	Console::Write("Number of Stories:   ");
	Console::WriteLine(condo->Stories);
	Console::Write("Year Built:          ");
	Console::WriteLine(condo->YearBuilt);
	Console::Write("Monetary Value:      ");
	Console::WriteLine(condo->Value);

	Console::WriteLine();
	return 0;
}

When we study methods and constructor, we will see that there are other ways to initi alize a member variable declared as a handle.

Practical LearningPractical Learning: Using an Object as a Field

  1. To start a new program, launch Microsoft Visual C++ 2005
  2. On the main menu, click File -> New -> Project...
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
  4. In the Name box, replace the string with RealEstate7 and click OK
  5. On the main menu, click Project -> Add New Item...
  6. In the Templates list, click File (.cpp)
  7. In the New box, type Exercise and click Add
  8. In the empty file, type:
     
    using namespace System;
    
    namespace RealEstate
    {
        public value class CClassification
        {
        public:
    	long      PropertyNumber;
    	int       Condition;
    	__wchar_t Style;
        };
    	
        public value class CProperty
        {
        public:
    	CClassification ^ Class;
    	__wchar_t TypeOfHome;
    	int Bedrooms;
    	float Bathrooms;
    	Byte Stories;
    	int YearBuilt;
    	double MarketValue;
        };
    }
    
    int main()
    {
        RealEstate::CProperty ^ condo = gcnew RealEstate::CProperty;
        RealEstate::CClassification ^ type = gcnew RealEstate::CClassification;
    
        type->PropertyNumber = 697234; // Unique Number
        type->Condition      = 2;      // Good, may need minor repair
        type->Style          = L'H';   // Highrise
        condo->Class         = type;
    
        condo->YearBuilt  = 2002;
        condo->Bathrooms  = 1;
        condo->Stories    = 18;
        condo->MarketValue      = 155825;
        condo->Bedrooms   = 2;
        condo->TypeOfHome = L'C';
    
        Console::Write("Property #:   ");
        Console::WriteLine(condo->Class->PropertyNumber);
        Console::Write("Condition:    ");
        Console::WriteLine(condo->Class->Condition);
        Console::Write("Style:        ");
        Console::WriteLine(condo->Class->Style);
        Console::Write("Type of Home: ");
        Console::WriteLine(condo->TypeOfHome);
        Console::Write("Bedrooms:     ");
        Console::WriteLine(condo->Bedrooms);
        Console::Write("Bathrooms:    ");
        Console::WriteLine(condo->Bathrooms);
        Console::Write("Stories:      ");
        Console::WriteLine(condo->Stories);
        Console::Write("Year Built:   ");
        Console::WriteLine(condo->YearBuilt);
        Console::Write("Market Value: ");
        Console::WriteLine(condo->MarketValue);
    
        Console::WriteLine();
        return 0;
    }
  9. To execute the application, on the main menu, click Debug -> Start Without Debugging
  10. Click Yes
     
    Property #:   697234
    Condition:    2
    Style:        H
    Type of Home: C
    Bedrooms:     2
    Bathrooms:    1
    Stories:      18
    Year Built:   2002
    Market Value: 155825
    
    Press any key to continue . . .
  11. Close the DOS window

Introduction to Strings

The data types we introduced in Lesson 2 each supports a precise type of symbols to make up its value. For example, an integer must contain only digits or characters that can be evaluated to a number. A decimal number can combine only digits and a period for its value. A character can be only one symbol. We also saw that there were precise ways to initialize variables of these types. A string is one or more characters or symbols considered as an entity. The symbols can be digits only. It can be a combination of digits and letters. It can also combine digits, letters, and non-readable characters. This type of definition doesn't fit any of the data types we have seen so far, not even the Object. To be able to consider such a combination as a (whole) value, there are some techniques you must use. Overall, such a combination is considered a string.

In the C++/CLI and the .NET Framework, a string is created using the String class. You can declare it as a handle. Here is an example:

using namespace System;

int main()
{	
    String ^ Whatever;

    return 0;
}

After declaring the variable, you can initialize it. Because a string can be used to represent any type significant or insignificant value, there are techniques you must use to initialize it. The fundamental technique of initializing a string is to include its value in double-quotes. Here is an example:

using namespace System;

int main()
{	
    String ^ Whatever = "KhjK526Hh6";

    return 0;
}

To display the value of a String object, you can pass it to Write() or WriteLine(). Here is an example:

using namespace System;

int main()
{	
	
    String ^ Whatever = "KhjK526Hh6";

    Console::WriteLine(Whatever);

    return 0;
}

This would produce:

KhjK526Hh6
Press any key to continue . . .

You can also include one or more escape sequences in a String object. Here is an example:

using namespace System;

int main()
{
    String ^ WhiteHouse = "The White House\n";

    Console::WriteLine(WhiteHouse);

    return 0;
}

A string can be of almost any length, at least you are allowed to create long strings. When initializing a long string, you can span its value on various lines. Each line must have its pair of quotes. Here is an example:

using namespace System;

int main()
{	
    String ^ WhiteHouse = "The White House"
	                  "1600 Pennsylvania Avenue"
			  "Washington, DC";

    return 0;
}

As mentioned already, you can put escape sequences anywhere inside of the string. The compiler would find and interpret them appropriately. Here are examples:

using namespace System;

int main()
{
    String ^ WhiteHouse = "The White House\n"
	                  "1600 Pennsylvania Avenue\n"
			  "Washington, DC";

    Console::WriteLine(WhiteHouse);

    return 0;
}

This would produce:

The White House
1600 Pennsylvania Avenue
Washington, DC
Press any key to continue . . .

Another technique you can use to span a string on various lines with one pair of double-quotes is to end each line with a backslash. Here is an example:

int main()
{
	String ^ str = "A Soho, \
dans Londres";

	Console::WriteLine(str);
	return 0;
}

A string as those we have used so far is considered a regular string. Notice that our strings use only regular characters of a Latin-based language. The C++ language also supports wide strings of the Unicode. To specify that your String value supports Unicode, you can precede its initialization with L. Here is an example:

using namespace System;

int main()
{	
    String ^ Whatever = L"The White House";

    Console::WriteLine(WhiteHouse);
    return 0;
}

If your initialization spans many lines, start each with its own L. Here are examples:

using namespace System;

int main()
{	
    String ^ Whatever = L"The White House"
			L"1600 Pennsylvania Avenue"
	                L"Washington, DC";

    Console::WriteLine(WhiteHouse);
    return 0;
}

The strings displayed in Write() or WriteLine() also support all aspects of the String class. For this reason, you can also start a string of Write() or WriteLine() with L.

Just as you can declare a String variable in a function such as main(), you can also create a member of class as a string. To do this, simple declare the String variable in the body of the class.

Practical LearningPractical Learning: Using String Variables

  1. To use a string, access the Exercise.cpp source file
  2. Declare and use the following variables:
     
    using namespace System;
    
    namespace RealEstate
    {
        public value class CClassification
        {
        public:
    	long      PropertyNumber;
    	int       Condition;
    	__wchar_t Style;
        };
    	
        public value class CProperty
        {
        public:
    	CClassification ^ Class;
    	__wchar_t TypeOfHome;
    	int Bedrooms;
    	float Bathrooms;
    	Byte Stories;
    	int YearBuilt;
    	double MarketValue;
        };
    }
    int main()
    {
        String ^ strTitle1 = L"=//= Altair Realty =//=";
        String ^ strTitle2 = L"-=- Properties Inventory -=-";
    
        RealEstate::CProperty ^ condo = gcnew RealEstate::CProperty;
        RealEstate::CClassification ^ type = gcnew RealEstate::CClassification;
    
        type->PropertyNumber = 697234; // Unique Number
        type->Condition      = 2;      // Good, may need minor repair
        type->Style          = L'H';   // Highrise
        condo->Class         = type;
    
        condo->YearBuilt  = 2002;
        condo->Bathrooms  = 1;
        condo->Stories    = 18;
        condo->MarketValue      = 155825;
        condo->Bedrooms   = 2;
        condo->TypeOfHome = L'C';
    
        Console::WriteLine(strTitle1);
        Console::WriteLine(strTitle2);
        Console::Write("Property #:   ");
        Console::WriteLine(condo->Class->PropertyNumber);
        Console::Write("Condition:    ");
        Console::WriteLine(condo->Class->Condition);
        Console::Write("Style:        ");
        Console::WriteLine(condo->Class->Style);
        Console::Write("Type of Home: ");
        Console::WriteLine(condo->TypeOfHome);
        Console::Write("Bedrooms:     ");
        Console::WriteLine(condo->Bedrooms);
        Console::Write("Bathrooms:    ");
        Console::WriteLine(condo->Bathrooms);
        Console::Write("Stories:      ");
        Console::WriteLine(condo->Stories);
        Console::Write("Year Built:   ");
        Console::WriteLine(condo->YearBuilt);
        Console::Write("Market Value: ");
        Console::WriteLine(condo->MarketValue);
    
        Console::WriteLine();
        return 0;
    }
  3. Execute the application to see the result
     
    =//= Altair Realty =//=
    -=- Properties Inventory -=-
    Property #:   697234
    Condition:    2
    Style:        H
    Type of Home: C
    Bedrooms:     2
    Bathrooms:    1
    Stories:      18
    Year Built:   2002
    Market Value: 155825
    
    Press any key to continue . . .
  4. Close the DOS window

A String as a Member Variable of a Class

A string is one of the most sought members of a class. It allows the member variable to carry any type of value, like any combination of characters or symbols included in double-quotes. As stated already, in C++/CLI, a string is mostly identified with the String class. Because the String class is a managed type, it must be declared on the managed heap as a handle. This is done using the ^ operator. Here are examples:

public value class CHouse
{
public:
	String ^ PropertyNumber;
	String ^ Address;
	String ^ City;
	String ^ State;
	String ^ ZIPCode;
	__wchar_t TypeOfHome;
	int Bedrooms;
	double Bathrooms;
	Byte Stories;
	int YearBuilt;
	int  Condition;
	__wchar_t Style;
	double Value;
};

To access a String member variable, use the -> operator and you don't have to allocate its memory. Here are example:

using namespace System;

public value class CHouse
{
public:
	String ^ PropertyNumber;
	String ^ Address;
	String ^ City;
	String ^ State;
	String ^ ZIPCode;
	__wchar_t TypeOfHome;
	int Bedrooms;
	double Bathrooms;
	Byte Stories;
	int YearBuilt;
	int  Condition;
	__wchar_t Style;
	double Value;
};

int main()
{
	CHouse ^ home = gcnew CHouse;

	home->PropertyNumber    = "288635";
	home->Address           = "6808 Lilas Drive";
	home->City              = "Silver Spring";
	home->State             = "MD";
	home->ZIPCode           = "20904";
	home->TypeOfHome        = L'S';
	home->Bedrooms  = 5;
	home->Bathrooms = 1;
	home->Stories           = 3;
	home->YearBuilt         = 1992;
	home->Condition         = 2;      // Good, may need minor repair
	home->Style             = L'M';   // Highrise
	home->Value             = 555825;

	Console::Write("Property #:          ");
	Console::WriteLine(home->PropertyNumber);
	Console::Write("Address:             ");
	Console::WriteLine(home->Address);
	Console::Write("                     ");
	Console::Write(home->City);
	Console::Write(", ");
	Console::Write(home->State);
	Console::Write(" ");
	Console::WriteLine(home->ZIPCode);
	Console::Write("Type of Home:        ");
	Console::WriteLine(home->TypeOfHome);
	Console::Write("Number of Bedrooms:  ");
	Console::WriteLine(home->Bedrooms);
	Console::Write("Number of Bathrooms: ");
	Console::WriteLine(home->Bathrooms);
	Console::Write("Number of Stories:   ");
	Console::WriteLine(home->Stories);
	Console::Write("Year Built:          ");
	Console::WriteLine(home->YearBuilt);
	Console::Write("Condition:           ");
	Console::WriteLine(home->Condition);
	Console::Write("Style:               ");
	Console::WriteLine(home->Style);
	Console::Write("Property Value:      ");
	Console::WriteLine(home->Value);

	Console::WriteLine();
	return 0;
}

This would produce:

Property #:          288635
Address:             6808 Lilas Drive
                     Silver Spring, MD 20904
Type of Home:        S
Number of Bedrooms:  5
Number of Bathrooms: 1
Number of Stories:   3
Year Built:          1992
Condition:           2
Style:               M
Property Value:      555825

Press any key to continue . . .

Practical LearningPractical Learning: Using Strings as Fields

  1. Change the Exercise.cpp source file as follows:
     
    using namespace System;
    
    namespace RealEstate
    {
        public value class CClassification
        {
        public:
    	long      PropertyNumber;
    	int       Condition;
    	__wchar_t Style;
        };
    	
        public value class CProperty
        {
        public:
    	CClassification ^ Class;
    	__wchar_t TypeOfHome;
    	String ^ Address;
    	String ^ City;
    	String ^ State;
    	String ^ ZIPCode;
    	int Bedrooms;
    	float Bathrooms;
    	Byte Stories;
    	int YearBuilt;
    	double MarketValue;
        };
    }
    int main()
    {
        String ^ strTitle1 = L"=//= Altair Realty =//=";
        String ^ strTitle2 = L"-=- Properties Inventory -=-";
    
        RealEstate::CProperty ^ condo = gcnew RealEstate::CProperty;
        RealEstate::CClassification ^ type = gcnew RealEstate::CClassification;
    
        type->PropertyNumber = 697234; // Unique Number
        condo->Address       = L"6808 Lilas Drive";
        condo->City          = L"Silver Spring";
        condo->State         = L"MD";
        condo->ZIPCode       = L"20904";
        type->Condition      = 2;      // Good, may need minor repair
        type->Style          = L'H';   // Highrise
        condo->Class         = type;
    
        condo->YearBuilt  = 2002;
        condo->Bathrooms  = 1;
        condo->Stories    = 18;
        condo->MarketValue      = 155825;
        condo->Bedrooms   = 2;
        condo->TypeOfHome = L'C';
    
        Console::WriteLine(strTitle1);
        Console::WriteLine(strTitle2);
        Console::Write("Property #:   ");
        Console::WriteLine(condo->Class->PropertyNumber);
        Console::Write("Address:      ");
        Console::WriteLine(condo->Address);
        Console::Write("              ");
        Console::Write(condo->City);
        Console::Write(", ");
        Console::Write(condo->State);
        Console::Write(" ");
        Console::WriteLine(condo->ZIPCode);
        Console::Write("Condition:    ");
        Console::WriteLine(condo->Class->Condition);
        Console::Write("Style:        ");
        Console::WriteLine(condo->Class->Style);
        Console::Write("Type of Home: ");
        Console::WriteLine(condo->TypeOfHome);
        Console::Write("Bedrooms:     ");
        Console::WriteLine(condo->Bedrooms);
        Console::Write("Bathrooms:    ");
        Console::WriteLine(condo->Bathrooms);
        Console::Write("Stories:      ");
        Console::WriteLine(condo->Stories);
        Console::Write("Year Built:   ");
        Console::WriteLine(condo->YearBuilt);
        Console::Write("Market Value: ");
        Console::WriteLine(condo->MarketValue);
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application to see the result
     
    =//= Altair Realty =//=
    -=- Properties Inventory -=-
    Property #:   697234
    Address:      6808 Lilas Drive
                  Silver Spring, MD 20904
    Condition:    2
    Style:        H
    Type of Home: C
    Bedrooms:     2
    Bathrooms:    1
    Stories:      18
    Year Built:   2002
    Market Value: 155825
    
    Press any key to continue . . .
  3. Close the DOS window

Inheritance

 

Introduction

The ability to use already created classes is one of the strengths of C++. If you have a ready made class, you can construct a new one using the existing characteristics. Inheritance is the ability to create a class using, or based on, another. The new or inherited class is also said to derive, or be derived, from the other class.

To start with inheritance, you should define a class. This means that it should have functionality, characteristics, and behaviors that other classes can use with little concerns as to how the class was built, but trusting that it can handle the desired assignment of a parent. To When creating the class, it must be made with ref instead of value. Here is an example:

public ref class CProperty
{
public:
	int Bedrooms;
	double Bathrooms;
	int YearBuilt;
	double Value;
};

Practical LearningPractical Learning: Introducing Inheritance

  1. Change the file as follows:
     
    . . .
    
    namespace RealEstate
    {
        public value class CProperty
        {
        public:
    		long PropertyNumber;
    	    String ^ Address;
    	    String ^ City;
    	    String ^ State;
    	    String ^ ZIPCode;
    	    int Bedrooms;
    	    float Bathrooms;
    	    int YearBuilt;
    	    double MarketValue;
        };
    }
    
    . . .
  2. Save the file

Inheriting From a Class

Once you have a class, you can apply its behavior as the starting point of another object. The basic syntax on inheriting from a class is:

AccessType ref structORclass ClassName : AccessLevel ParentClass

The optional AccessType can be public or private as we saw in Lesson 1.

The ref keyword is required (if you are creating a managed class).

The word structORclass will be replaced by struct or class depending on the class type you are trying to create.

The ClassName element represents the name of the class you are creating.

The colon (:) is read "is based on". It lets the compiler know that the new class gets its foundation from another class.

The word AccessLevel specifies whether the class will use the public or private level of access. If you are creating a managed class, this must be specified as public.

The ParentClass is the name of the class that the new class is based on or is inheriting from.

When inheriting from another class, the new class is considered a child. It has access to the public member(s) of the parent class. The inheriting class will not have access to the private members of the parent class. Here is an example:

public ref class CProperty
{
public:
	int Bedrooms;
	double Bathrooms;
	int YearBuilt;
	double Value;
};

public ref class CHouse : public CProperty
{
};

In the body of the child class, you can define the new members as you see fit. You can create the class' own public and private members. Each member of the child class would have access to all the members of the child class and the public members of the parent class.

To use the class in a function such as main(), first declare a handle to the class, access its public members and the public members of its base class. Here is an example:

using namespace System;

public ref class CProperty
{
public:
	int Bedrooms;
	double Bathrooms;
	int YearBuilt;
	double Value;
};

public value class Classification
{
public:
	long PropertyNumber;
	int  Condition;
};

public ref class CHouse : public CProperty
{
public:
	Classification Class;
	Byte Stories;
	int  GarageCanAccomodate;
};

int main()
{
	CHouse ^ Townhouse = gcnew CHouse;

	Townhouse->Class.PropertyNumber = 697234;
	Townhouse->Class.Condition = 3;
	Townhouse->Stories = 2;
	Townhouse->Bedrooms = 2;
	Townhouse->Bathrooms = 1.5;
	Townhouse->YearBuilt = 1977;
	Townhouse->Value = 388450;

	Console::WriteLine(L"=//= Real Estate - Catalog =//=");
	Console::WriteLine(L"-- Property Type: Townhouse --");
	Console::Write(L"Property #:          ");
	Console::WriteLine(Townhouse->Class.PropertyNumber);
	Console::Write(L"Condition:           ");
	Console::WriteLine(Townhouse->Class.Condition);
	Console::Write(L"Number of Bedrooms:  ");
	Console::WriteLine(Townhouse->Bedrooms);
	Console::Write(L"Number of Levels:    ");
	Console::WriteLine(Townhouse->Stories);
	Console::Write(L"Number of Bathrooms: ");
	Console::WriteLine(Townhouse->Bathrooms);
	Console::Write(L"Year Built:          ");
	Console::WriteLine(Townhouse->YearBuilt);
	Console::Write(L"Property Value:      ");
	Console::WriteLine(Townhouse->Value);

	Console::WriteLine();
	return 0;
}

This would produce:

=//= Real Estate - Catalog =//=
-- Property Type: Townhouse --
Property #:          697234
Condition:           3
Number of Bedrooms:  2
Number of Levels:    2
Number of Bathrooms: 1.5
Year Built:          1977
Property Value:      388450

Press any key to continue . . .

In the same way, you can inherit one class from another class that itself was inherited from another class. Also, you can inherit different classes from a common class. Here are examples:

public ref class CProperty
{
public:
	int Bedrooms;
	double Bathrooms;
	int YearBuilt;
	double Value;
};

public value class Classification
{
public:
	long PropertyNumber;
	int  Condition;
};

public ref class CHouse : public CProperty
{
};

public ref class CSingleFamily : public CHouse
{
};

public ref class CTownhouse : public CHouse
{
};

public ref class CCondominium : public CProperty
{
};

Practical LearningPractical Learning: Inheriting From a Class

  1. Change the Exercise.cpp source file as follows:
     
    using namespace System;
    
    namespace RealEstate
    {
        public ref class CProperty
        {
        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;
        };
    }
    
    int main()
    {
        using namespace RealEstate;
    
        String ^ strTitle1 = L"=//= Altair Realty =//=";
        String ^ strTitle2 = L"-=- Properties Inventory -=-";
    
        CHouse ^ home = gcnew CHouse;
    
        home->PropertyNumber = 697234; // Unique Number
        home->Condition      = 1;      // Excellent
        home->Address        = L"12446 Green Castle Avenue";
        home->City           = L"Silver Spring";
        home->State          = L"MD";
        home->ZIPCode        = L"20906";
        home->Bedrooms       = 4;
        home->Bathrooms      = 2.5F;
        home->Stories        = 3;
        home->YearBuilt      = 2004;
        home->Style          = L'S';
        home->MarketValue    = 750855;
    
        Console::WriteLine(strTitle1);
        Console::WriteLine(strTitle2);
        Console::Write("Property #:   ");
        Console::WriteLine(home->PropertyNumber);
        Console::Write("Address:      ");
        Console::WriteLine(home->Address);
        Console::Write("              ");
        Console::Write(home->City);
        Console::Write(", ");
        Console::Write(home->State);
        Console::Write(" ");
        Console::WriteLine(home->ZIPCode);
        Console::Write("Condition:    ");
        Console::WriteLine(home->Condition);
        Console::Write("Style:        ");
        Console::WriteLine(home->Style);
        Console::Write("Bedrooms:     ");
        Console::WriteLine(home->Bedrooms);
        Console::Write("Bathrooms:    ");
        Console::WriteLine(home->Bathrooms);
        Console::Write("Stories:      ");
        Console::WriteLine(home->Stories);
        Console::Write("Year Built:   ");
        Console::WriteLine(home->YearBuilt);
        Console::Write("Market Value: ");
        Console::WriteLine(home->MarketValue);
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application to see the result
  3. Close the DOS window

The protected Access Level

In previous sections, we learned that the public level allows the client of a class to access any member of the public section of the class. We also learned to hide other members by declaring them as private, which prevents the clients of class from accessing such variables. You can create a special access level that allows only the children of a class to have access to certain members of the parent class. This new access level is called protected and created with that keyword.

To allow the children of a class to have a special permission in accessing some members of the parent class, you can place those members in a protected section. Here is an example:

public ref class CProperty
{
protected:
	int Bedrooms;
	double Bathrooms;
	int YearBuilt;
	double Value;
};

Only the classes derived can access the protected members of a class.

Namespaces and Inheritance

You can inherit a class that belongs to a namespace. To do this, type the name of the namespace, followed by the :: operator, and followed by the name of the base namespace. Here is an example:

using namespace System;

namespace RealEstate
{
	public ref class CProperty
	{
	};
}

public ref class CHouse : public RealEstate::CProperty
{

};

public ref class CCondominium : public RealEstate::CProperty
{
};

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