// File Name: ~ftp/pub/class/cplusplus/Structure/StrucSimple.cpp // Purpose: Demonstrates the use of structure in C++. // Stores some personal data in a structure, then prints // the info out. #include <iostream> using namespace std; int main() { // Defining a structure struct PersonalData { char *FirstName; char *LastName; char *Birthday; // in the format of 12/30/1978 int PhoneNum; }; // don't forget the ending ";" // Declaring a variable of type PersonalData PersonalData PersonOne; // Populate PersonOne with data PersonOne.FirstName = "John"; PersonOne.LastName = "Doe"; PersonOne.Birthday = "12/30/1978"; PersonOne.PhoneNum = 5855555; // Print the data out cout << "PersonOne's First name is: " << PersonOne.FirstName << endl; cout << "PersonOne's Last name is: " << PersonOne.LastName<< endl; cout << "PersonOne's Birthday is: " << PersonOne.Birthday<< endl; cout << "PersonOne's Phone number is: " << PersonOne.PhoneNum<< endl; return 0; }
@9:51am merc-hu[34]% CC -LANG:std StrucSimple.cpp -o StrucSimple @9:52am merc-hu[35]% StrucSimple PersonOne's First name is: John PersonOne's Last name is: Doe PersonOne's Birthday is: 12/30/1978 PersonOne's Phone number is: 5855555 @9:52am merc-hu[36]%
// File Name: ~ftp/pub/class/cplusplus/Structure/StrucSimple.cpp // Purpose: Demonstrates the use of nested structure in C++. // Stores some personal data in a structure, then prints // the info out. #include <iostream> using namespace std; int main() { // Defining a structure for name struct Name { char *FirstName; char *LastName; }; struct PersonalData { Name NameField; // struct as a memeber char *Birthday; // in the format of 12/30/1978 int PhoneNum; }; // don't forget the ending ";" // Declaring a variable of type PersonalData PersonalData PersonOne; PersonOne.NameField.FirstName = "John"; PersonOne.NameField.LastName = "Doe"; // Populate PersonOne with data PersonOne.Birthday = "12/30/1978"; PersonOne.PhoneNum = 5855555; // Print the data out cout << "First name is: " << PersonOne.NameField.FirstName << endl; cout << "Last name is: " << PersonOne.NameField.LastName<< endl; cout << "Birthday is: " << PersonOne.Birthday<< endl; cout << "Phone number is: " << PersonOne.PhoneNum<< endl; return 0; }
@9:59am merc-hu[45]% CC -LANG:std StructNested.cpp -o StructNest @10:00am merc-hu[47]% StructNest First name is: John Last name is: Doe Birthday is: 12/30/1978 Phone number is: 5855555 @10:00am merc-hu[48]%
// File Name: ~ftp/pub/class/cplusplus/Structure/StrucSimple.cpp // Purpose: Demonstrates the use of funciton in C++ struct. // Stores some personal data in a structure, then prints // the info out. #include <iostream> using namespace std; int main() { struct PersonalData { char *FirstName; char *LastName; char *Birthday; // in the format of 12/30/1978 int PhoneNum; // struc can also have member functions void PrintDat() { // Print the data out cout << "First name is: " << FirstName << endl; cout << "Last name is: " << LastName << endl; cout << "Birthday is: " << Birthday << endl; cout << "Phone number is: " << PhoneNum << endl; } }; // don't forget the ending ";" // Declaring a variable of type PersonalData PersonalData PersonOne; // Populate PersonOne with data PersonOne.FirstName = "John"; PersonOne.LastName = "Doe"; PersonOne.Birthday = "12/30/1978"; PersonOne.PhoneNum = 5855555; PersonOne.PrintDat(); return 0; }
@10:07am merc-hu[51]% CC -LANG:std StrucFunc.cpp -o StrucFunc @10:08am merc-hu[52]% StrucFunc First name is: John Last name is: Doe Birthday is: 12/30/1978 Phone number is: 5855555 @10:08am merc-hu[53]%
กก