Contents of this section:
/ 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.
Output
First name is: John
Last name is: Doe
Birthday is: 12/30/1978
Phone number is: 5855555
- A simple program demonstrating structure
- Nested structure
- Using functions in structure
// 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; } Output 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 2. Nested structure // 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; } Output First name is: John Last name is: Doe Birthday is: 12/30/1978 Phone number is: 5855555 3. Using functions in structure // 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; }
Output
First name is: John
Last name is: Doe
Birthday is: 12/30/1978
Phone number is: 5855555
Post A Comment:
0 comments: