Review An object is a special variable that has data as well as member functions � that act on that data. CSI 201 - Introduction to A class is a type definition that gives us a blueprint for creating objects of � that type. Example classes we've seen: � Computer Science ostream � ofstream � istream � ifstream � An object is a variable whose type is a class . � We say an object is an instance of a class. � Example objects we've seen: Chapter 6 � cin � Defining classes cout � Goal of Chapter 6 - Learn how to define our own class � The complete class definition will define: Brian R. King � What kind of data the object can hold � Instructor What functions should exist to operate on that data. � 3/8/2006 2 CSI 201 -- Chapter 06 Structures Struct definition syntax � A structure (or struct ) is a simplified class, and � struct Identifier { makes a good introduction to exploring classes. variable_declaration_1; variable_declaration_2; � Structs allow us to define new data types that are … am amalgam of other data types. variable_declaration_n; }; � A good analogy of a struct is a record of a database. � Identifier names the structure type. We call this the structure � A student record may include fields such as: tag . Name � � It is an industry standard to have the first letter of a class or struct name be a CAPITAL letter (This is not a C++ syntax Student ID � requirement.) GPA � � Each one of the variable declarations between the braces are Status (1=freshman, … , 4 = senior, 5 graduate) � referred to as the members of the struct, also called member names or member fields. � Structs are sometimes referred to as records in � The structure definition ends with a semi-colon. some texts. � The structure tag becomes a new data type for your program. 3/8/2006 CSI 201 -- Chapter 06 3 3/8/2006 CSI 201 -- Chapter 06 4
The student record as a struct Declaring a struct variable #include <iostream> struct Student using namespace std; { struct Student { char name[30]; char name[30]; // Student name int id; // Student id int id; double GPA; // Current GPA (0.0 - 4.0) int status; // Current status double GPA; }; int status; int main() { }; Student st; � Student is the structure tag above, and therefore return 0; names a new type available in our program. } Our struct definition goes outside of any function, in the global declaration space. Why? � � Student has 4 members. They are name , id , GPA The struct definition defines a new type called Student . � The main program has a variable declaration of type Student called st . and status . � The variable declaration for st allocates within it memory to store each of the members of � st . 3/8/2006 5 3/8/2006 6 CSI 201 -- Chapter 06 CSI 201 -- Chapter 06 Accessing members – the "dot" operator Ex1.cpp #include <iostream> The dot operator is used to access members of a struct variable. � #include <iomanip> cout << "Enter the student id: "; #include <fstream> cin >> st.id ; using namespace std; cout << "Enter the student's GPA: "; struct Student cin >> st.GPA ; { cout << "Enter current standing (1 = freshman ... 5 = graduate): "; int id; // Student id cin >> st.status ; double GPA; // Current GPA (0.0 - 4.0) int status; // Current status (1 = freshman .. 4 = senior, 5 = graduate) To the left of the dot operator is a struct variable. � }; To the right of the dot operator is a member name. � int main() Struct members are treated just like any other variables, as long as { � Student st; they are accessed through the dot operator. cout << "Enter the student id: "; Think about the type of the expressions in bold above. What type � cin >> st.id; cout << "Enter the student's GPA: "; are they? cin >> st.GPA; cout << "Enter their current standing (1 = freshman ... 4 = senior, 5 = graduate): "; cin >> st.status; Why wouldn't the following work? (Ask yourself, does an istream � cout.setf(ios::right); object know how to extract a variable of type Student ?) cout << setw(15) << "Student id: " << setw(10) << st.id << endl; cout << setw(15) << "GPA: " << setw(10) << st.GPA << endl; cin >> st; � cout << setw(15) << "Status: " << setw(10) << st.status << endl; return 0; } 3/8/2006 CSI 201 -- Chapter 06 7 3/8/2006 CSI 201 -- Chapter 06 8
More on structs… Struct assignments Multiple struct definitions can have the same member names: � You can assign one struct variable to another struct variable, as long struct Employee � as both variables are of the same type: { int id; Student s1, s2; int years; s1.id = 8675309; }; s1.GPA = 3.98; s2 = s1; // This is legal! struct Student { It's illegal to assign struct variables of different types: � int id; Student st; double GPA; Employee emp; int status; }; st.id = 1234567; � Both structs have members called id . � This is legal because variable declarations inside of different emp = st; // This is illegal! struct definitions are independent of each other. They are tied to the struct in which they are defined. � 3/8/2006 9 3/8/2006 10 CSI 201 -- Chapter 06 CSI 201 -- Chapter 06 Structures as function arguments ex2.cpp – Structures within structures struct Date � You can pass structures as parameters into { You can also define a functions the same way you pass simple int month; hierarchy of structures. types as parameters. int day; int year; � Example: }; bool isHonorStudent(Student& s); struct Student � It is not necessary for a struct parameter to be a { call-by-reference parameter. Do what makes int id; // Student id sense for your program. // Student birthday Date bday; double GPA; // Current GPA (0.0 - 4.0) int status; // Current status }; 3/8/2006 CSI 201 -- Chapter 06 11 3/8/2006 CSI 201 -- Chapter 06 12
ex2.cpp continued… ex2.cpp continued… void print_student( Student &s , ostream &out); void print_student(Student &s, ostream &out) { int main() out.setf(ios::right); { out << setw(15) << "Student id: " << setw(10) << s.id << endl; Student st; out << setw(15) << "Birthday: " << setw(2) << s.bday.month << "/" << setw(2) << s.bday.day << "/" << setw(4) << s.bday.year << endl; cout << "Enter the student id: "; out << setw(15) << "GPA: " << setw(10) << s.GPA << endl; cin >> st.id; out << setw(15) << "Status: " << setw(10); cout << "Enter student's birthday as month day year: "; cin >> st.bday.month >> st.bday.day >> st.bday.year ; if ( s.status == 1) out << "Freshman"; cout << "Enter the student's GPA: "; else if ( s.status == 2) cin >> st.GPA; out << "Sophomore"; cout << "Enter their current standing (1 = freshman ... 4 = senior, 5 = grad else if ( s.status == 3) uate): "; out << "Junior"; cin >> st.status; else if ( s.status == 4) What type is st ? out << "Senior"; print_student( st ,cout); else if ( s.status == 5) out << "Graduate"; What type is st.bday ? return 0; else } out << "UNKNOWN"; What type is st.bday.month ? out << endl; } 3/8/2006 13 3/8/2006 14 CSI 201 -- Chapter 06 CSI 201 -- Chapter 06 Initializing structures Questions… You can initialize structures at the time they are declared. SEE EX3.cpp � Simple structures: � What happens if you don't have enough initializers for your struct � struct Date variable? { Student s = { "Fred Jones", 123043243, { 12, 25, 2004 }}; int month; int day; int year; }; Date d = { 12, 25, 2004 }; What happens if you have too many initializers for your structure � variable? Hierarchical structures: � Student s = { "Fred Jones", 123043243, { 12, 25, 2004 }, 3.25, struct Student 3, 2}; { char name[30];// Student name int id; // Student id Date bday; // Student birthday double GPA; // Current GPA (0.0 - 4.0) int status; // Current status What is the value of your structure variable if you don't initialize it? � }; Student s; Student s = { "Fred Jones", 123043243, { 12, 25, 2004 }, 3.23, 3 }; 3/8/2006 CSI 201 -- Chapter 06 15 3/8/2006 CSI 201 -- Chapter 06 16
Recommend
More recommend