兰州理工大学,研究生VC++课件7

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

THE STRUCT IN C++
• This chapter is about creating your own data types to suit your particular problem. • It’s also about creating objects, the building blocks of object-oriented programming. • An object can seem a bit mysterious to the uninitiated, but as you will see in this chapter, an object can be just an instance of one of your own data types.
Defining a struct
• Suppose that you just want to include the title, author, publisher, and year of publication within your definition of a book. • You could declare a structure to accommodate this as follows:
What Is a struct?
• Almost all the variables have been able to store a single type of entity — a number of some kind, a character, or an array of elements of the same type. • The real world is a bit more complicated than that, and just about any physical object you can think of needs several items of data to describe it even minimally. • Think about the information that might be needed to describe something as simple as a book. • You might consider title, author, publisher, date of publication, number of pages, price, topic or classification, and ISBN number, just for starters, and you can probably come up with a few more without too much difficulty. • You could specify separate variables to contain each of the parameters that you need to describe a book, but ideally, you would want to have a single data type, BOOK, say, which embodied all of these parameters.
• Every object of type BOOK contains its own set of the members Title, Author, Publisher, and Year.
• You can create variables of type BOOK in exactly the same way as you create variables of any other type:
• You may think this is a limitation, but note that you could include a pointer of type BOOK.
• The elements Title, Author, Publisher, and Year enclosed between the braces in the preceding definition are referred to as members or fields of the BOOK structure. • An object of type BOOK is called an instance of the class type.
BOOK Novel; // Declare variable Novel of type BOOK
• This declares a variable with the name Novel that you can use to store information about a book. • All you need is to understand how you get data into the various members that make up a variable of type BOOK.
• With this wealth of information, you can write the declaration for Novel as:
BOOK Novel = { "Paneless Programming", // Initial value for Title "I.C. Fingers", // Initial value for Author "Gutter Press", // Initial value for Publisher 1981 // Initial value for Year };
• A structure is a user-defined type that you define using the struct keyword so it is often referred to as a struct. • The struct originated back in the C language, and C++ incorporates and expands on the C struct. • A struct in C++ is functionally replaceable by a class insofar as anything you can do with a struct, you can also achieve by using a class; however, because Windows was written in C before C++ became widely used, the struct appears pervasively in Windows programming. • It is also widely used today, so you really need to know something about structs.
Accessing the Members of a struct
• To access individual members of a struct, you use the member selection operator, which is a period; this is sometimes referred to as the member access operator. • To refer to a member, you write the struct variable name, followed by a period, followed by the name of the member. • To change the Year member of the Novel structure, you could write:
struct BOOK { char Title[80]; char Author[80]; char Publisher[80]; int Year; };
• This doesn’t define any variables, but it does define a new type for variables, and the name of the type is BOOK. • The struct keyword defines BOOK as such, and the elements making up an object of this type are defined within the braces.
7.Defining Your Own Data Type
201405
WHAT YOU WILL LEARN IN THIS CHAPTER:
• How structures are used • How classes are used • The basic components of a class and how you define class types • How to create, and use, objects of a class • How to control access to members of a class • How to create constructors • The default constructor • References in the context of classes • How to implement the copy constructor
Initializing a struct
• The first way to get data into the members of a struct object is to define initial values for them in the declaration of the object. • Suppose you wanted to initialize Novel to contain the data for one of your favorite books, Paneless Programming, published in 1981 by the Gutter Press. • This is a story of a guy performing heroic code development while living in an igloo, and, as you probably know, inspired the famous Hollywood box office success, Gone with the Window. • It was written by I.C. Fingers, who is also the author of that seminal three-volume work, The Connoisseur’s Guide to the Paper Clip.
ቤተ መጻሕፍቲ ባይዱ
• Note that each line defining an element in the struct is terminated by a semicolon, and that a semicolon also appears after the closing brace. • The elements of a struct can be of any type, except the same type as the struct being defined. • You couldn’t have an element of type BOOK included in the structure definition for BOOK, for example.
相关文档
最新文档