Home

Classes and Functions

 

Returning a Tracking Reference

As done for regular types, you can return a tracking reference to a class. To indicate this, type the % operator between the class type and the name of the function. In the body of the function, you can create a class as complete as possible, get a tracking reference to that class, and return that reference. Here is an example:

Car
using namespace System;

public value class CCar
{
public:
    String ^ Make;
    String ^ Model;
    int      Doors;
	int      Year;
    double   Price;
};

CCar % Build();

int main()
{
    CCar ^ vehicle = Build();

    Console::WriteLine(L"Car Characteristics");
    Console::WriteLine(L"Make:  {0}", vehicle->Make);
    Console::WriteLine(L"Model: {0}", vehicle->Model);
    Console::WriteLine(L"Doors: {0}", vehicle->Doors);
    Console::WriteLine(L"Year:  {0}", vehicle->Year);
    Console::WriteLine(L"Value: {0:C}", vehicle->Price);

    Console::WriteLine();
    return 0;
}

CCar % Build()
{
    CCar car;

    Console::Write(L"Enter Make:  ");
    car.Make = Console::ReadLine();
    Console::Write(L"Enter Model: ");
    car.Model = Console::ReadLine();
    Console::Write(L"# of Doors:  ");
    car.Doors = int::Parse(Console::ReadLine());
    Console::Write(L"Year Made:   ");
    car.Year = int::Parse(Console::ReadLine());
    Console::Write(L"Enter Price: ");
    car.Price = double::Parse(Console::ReadLine());

    CCar % veh = car;
    return veh;
}

This would produce:

Enter Make:  Honda
Enter Model: Accord
# of Doors:  4
Year Made:   1998
Enter Price: 14500
Car Characteristics
Make:  Honda
Model: Accord
Doors: 4
Year:  1998
Value: $14,500.00

Press any key to continue . . .

Practical LearningPractical Learning: Introducing Data Reading

  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 RealEstate10 and click OK
  5. On the main menu, click Project -> Add New Item...
  6. To create a header file, in the Templates list, click Header File (.h)
  7. Set the Name to Property and click Add
  8. Complete the file as follows:
     
    #pragma once
    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;
        };
    }
  9. To create another header file, on the main menu, click Project -> Add New Item...
  10. In the Templates list, make sure Header File (.h) is selected.
    Set the Name to House and press Enter
  11. Complete the file as follows:
     
    #pragma once
    
    #include "Property.h"
    
    using namespace System;
    
    namespace RealEstate
    {
        public ref class CHouse : public CProperty
        {
        public:
            Byte Stories;
            int  Condition;
            unsigned NumberOfGarageSpaces;
        };
    }
  12. To create one more header file, on the main menu, click Project -> Add New Item...
  13. In the Templates list, make sure Header File (.h) is selected.
    Set the Name to SingleFamily and click Add
  14. Complete the file as follows:
     
    #pragma once
    #include "House.h"
    
    using namespace System;
    
    namespace RealEstate
    {
        public ref class CSingleFamily : public CHouse
        {
        public:
            __wchar_t Style;
            double LotSizeInSqFt;
        };
    }
  15. To create a source file, on the main menu, click Project -> Add New Item...
  16. In the Templates list, click C++ File (.cpp)
  17. In the New box, type Exercise and click Add
  18. In the empty file, type:
     
    #include <iostream>
    #include "SingleFamily.h"
    
    using namespace std;
    using namespace RealEstate;
    
    int main()
    {
        String ^ strTitle1 = L"=//= Altair Realty =//=";
        String ^ strTitle2 = L"-=- Properties Inventory -=-";
        CSingleFamily ^ home = gcnew CSingleFamily;    
    
        Console::WriteLine(strTitle1);
        Console::WriteLine(strTitle2);
    
        Console::WriteLine();
        return 0;
    }
  19. Execute the application to test it
  20. Close the DOS window

Returning a Handle

If you are defining a function and you want it to return a handle to a class, you can. To indicate this, between the name of the class and the name of the function, type the ^ operator.

Practical LearningPractical Learning: Returning a Handle

  1. To return a handle, change the Exercise.cpp file as follows:
     
    #include <iostream>
    #include "SingleFamily.h"
    
    using namespace std;
    using namespace RealEstate;
    
    CSingleFamily ^ CreateListing()
    {
        CSingleFamily ^ home = gcnew CSingleFamily;    
    
    Console::WriteLine("To create a listing, enter the following information");
        Console::Write("Property #:    ");
        home->PropertyNumber = long::Parse(Console::ReadLine());
        Console::WriteLine("Property Condition");
        Console::WriteLine("0. Unknown");
        Console::WriteLine("1. Excellent");
        Console::WriteLine("2. Good (may need minor repair");
        Console::WriteLine("3. Acceptable (needs major repair)");
        Console::WriteLine("4. Even (land is more important)");
        Console::Write("Your Choice:    ");
        home->Condition = int::Parse(Console::ReadLine());
        Console::Write("Street Address: ");
        home->Address = Console::ReadLine();
        Console::Write("City:           ");
        home->City = Console::ReadLine();
        Console::Write("State:          ");
        home->State = Console::ReadLine();
        Console::Write("ZIP Code:       ");
        home->ZIPCode = Console::ReadLine();
        Console::Write("Bedrooms:       ");
        home->Bedrooms = int::Parse(Console::ReadLine());
        Console::Write("Bathrooms:      ");
        home->Bathrooms = float::Parse(Console::ReadLine());
        Console::Write("Stories:        ");
        home->Stories = int::Parse(Console::ReadLine());
        Console::Write("Year Built:     ");
        home->YearBuilt = int::Parse(Console::ReadLine());
        Console::WriteLine("Style");
        Console::WriteLine("U. Unknown");
        Console::WriteLine("S. Split Level");
        Console::WriteLine("C. Colonial");
        Console::WriteLine("G. Georgial");
        Console::Write("Your Choice:    ");
        home->Style = __wchar_t::Parse(Console::ReadLine());
        Console::Write("Garage Spaces:  ");
        home->NumberOfGarageSpaces = int::Parse(Console::ReadLine());
        Console::Write("Total Lot Size: ");
        home->LotSizeInSqFt = double::Parse(Console::ReadLine());
        Console::Write("Market Value:   ");
        home->MarketValue = double::Parse(Console::ReadLine());
    
        return home;
    }
    
    int main()
    {
        String ^ strTitle1 = L"=//= Altair Realty =//=";
        String ^ strTitle2 = L"-=- Properties Inventory -=-";
      
        CSingleFamily ^ house = CreateListing();
    
        system("cls");
    
        Console::WriteLine(strTitle1);
        Console::WriteLine(strTitle2);
        Console::WriteLine("=//= Property Listing =//=");
        Console::WriteLine("Property #:    {0}", house->PropertyNumber);
        Console::WriteLine("Address:       {0}\n              {1}, {2} {3}",
    	                house->Address, house->City,
    			house->State, house->ZIPCode);
        Console::WriteLine("Condition:     {0}", house->Condition);
        Console::WriteLine("Style:         {0}", house->Style);
        Console::WriteLine("Bedrooms:      {0}", house->Bedrooms);
        Console::WriteLine("Bathrooms:     {0}", house->Bathrooms);
        Console::WriteLine("Stories:       {0}", house->Stories);
        Console::WriteLine("Year Built:    {0}", house->YearBuilt);
        Console::WriteLine("Garage Spaces: {0} SqFt",
    			house->NumberOfGarageSpaces);
        Console::WriteLine("Lot Size:      {0:F}", house->LotSizeInSqFt);
        Console::WriteLine("Market Value:  {0:C}", house->MarketValue);
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application to test it
  3. Close the DOS window
 

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