Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

8.5.1 Introduction

So far, we have used the Console class' Write() and WriteLine() to display things on the screen. While Write() is used to display something on the screen, you can use Read() to get a value from the user. To use it, the name of a variable can be assigned to it. The formula used is:

VariableName = Console::Read();

This simply means that, when the user types something and presses Enter, what the user had typed would be given (the word is assigned) to the variable specified on the left side of the assignment operator.

Read() doesn't always have to assign its value to a variable. For example, it can be used on its own line, which simply means that the user is expected to type something but the value typed by the user would not be used for any significant purpose.

Besides Read(), you can use ReadLine(). Like WriteLine(), after performing its assignment, ReadLine() sends the caret to the next line. Otherwise, it plays the same role as Read().

8.5.2Practical 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 RealEstate6 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;

};

}

  1. To create another header file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, make sure Header File (.h) is selected. Set the Name to House and press Enter

  3. 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;

};

}

  1. To create one more header file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, make sure Header File (.h) is selected. Set the Name to SingleFamily and click Add

  3. 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;

};

}

  1. To create a source file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, click C++ File (.cpp)

  3. In the New box, type Exercise and click Add

  4. In the empty file, type:  

#include "SingleFamily.h"

using namespace RealEstate;