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

1.10The Class View

 

1.10.1Introduction

The Class View displays the various classes used by your project, including there ancestry. The items of the Class View an organized as a tree list with the name of the project on top.

 

1.10.2Using the Class View

The Class View shares some of its functionality with the Solution Explorer. This means that you can use it to build a project, to add new class or a new resource. You can also use it to change the default project if you have more than one.

While the Solution Explorer displays the items that are currently being used by your project, the Class View allows you to explorer the classes used in your applications, including their dependencies. For example, some times you will be using a control of the of the .NET Framework and you may wonder from what class that control is derived. The Class View, rather than the Solution Explorer, can quickly provide this information. To find it out, expand the class by clicking its + button.

1.11Namespaces and Code Directives

1.11.1Introduction

When creating your classes, one of the most important rules you must observe is that two classes in the same program must not have the same name. For example, the following program will not compile:

struct CHouse

{

int protect()

{

return 0;

}

};

struct CCar

{

int drive()

{

return 0;

}

};

struct CHouse

{

int showroof()

{

return 0;

}

};

int main()

{

return 0;

}

Notice that there are two classes named house in this program. If you have different people working on the same project, it would not be unusual to have two classes with the same name. The consequence is that, if these classes are used in the same program, the program would not compile.

A namespace is a technique of grouping some functions and/or classes to create an entity. This has the main goal of eliminating the probability of having two classes of the same name. For example, if different people work on the same project, each one can create his or her own classes but put them in only his or her namespace. This way, even if they happen to have a class each with the same name, there is no risk of having a name conflict.

To create a namespace, type namespace followed by a name. Like a class, a namespace must have a body delimited by an opening curly bracket "{" and ending with a closing curly bracket "}". Here is an example:

namespace family

{

}

For the rest of the ebook, we will start the name of a namespace with an uppercase letter.

In the body of the namespace, you can create the classes (and/or functions) as you want. Here is an example:

namespace Family

{

struct CHouse

{

int protect()

{ return 0; }

};

struct CCar

{

int drive()

{ return 0; }

};

}

Remember that the struct or class keyword of a class can be preceded with private or public to control its "visibility" outside of its assembly. Therefore, the above classes can be preceded with the public keyword as follows:

namespace Family

{

public struct CHouse

{

int protect()

{ return 0; }

};

public struct CCar

{

int drive()

{ return 0; }

};

}

In the same way, you can have as many namespaces as you want. Each namespace can have any valid content. Inside of a namespace, two classes must not have the same name. Inside of a program, two namespaces must not have the same name. Consider this:

namespace Family

{

public struct CHouse

{

int protect()

{ return 0; }

};

public struct CCar

{

int drive()

{ return 0; }

};

}

int main()

{

return 0;

}

As done previously, you can create static methods as members of your classes. This is also valid even if a class is part of a namespace. Here is an example:

namespace Family

{

public struct CHouse

{

static int protect()

{ return 0; }

};

public struct CCar

{

int drive()

{ return 0; }

};

}

int main()

{

return 0;

}

Before accessing a non-static method of a class, you must first create an object from it. Before the type of declaration we saw earlier, you must precede it with the name of the namespace followed by ::. Here is an example:

namespace City

{

public struct CHouse

{

int showroof()

{ return 0; }

};

}

int main()

{

City::CHouse town;

town.showroof();

return 0;

}

As mentioned earlier, if a method is static, you don't have to create an object from it before using it. You can access it directly by typing the name of the namespace followed by ::, followed by the name of the class. Here is an example:

namespace Family

{

public struct CHouse

{

static int protect()

{

return 0;

}

};

public struct CCar

{

int drive()

{

return 0;

}

};

}

int main()

{

Family::CHouse::protect();

return 0;

}