Home

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
{
};
 

Previous Copyright © 2006-2007 FunctionX, Inc. Next