From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/CPLUSPLUS/EXAMPLES/struct1.cc | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 reference/CPLUSPLUS/EXAMPLES/struct1.cc (limited to 'reference/CPLUSPLUS/EXAMPLES/struct1.cc') diff --git a/reference/CPLUSPLUS/EXAMPLES/struct1.cc b/reference/CPLUSPLUS/EXAMPLES/struct1.cc new file mode 100644 index 0000000..9fa884a --- /dev/null +++ b/reference/CPLUSPLUS/EXAMPLES/struct1.cc @@ -0,0 +1,53 @@ +/****************************************************************** + * + * Purpose: Program to demonstrate struct in C++. + * Date: 04-Aug-97 + * Author: M J Leslie. + * Descrip: + * + ******************************************************************/ + +#include // printf + +struct Person +{ + // ... Declare the variables in the 'Person' structure. + // ... This is normal C syntax. + + char Name[35]; + int Age; + + // ... Declare a function within the structure. This is + // ... new to C++ + + int YearsToRetire(void) // \ + { // --- Function in a structure. + return 65-Age; // --- + } // / +}; + +// ... Program to test the 'Person' structure. + +int main(int argc, char **argc ) +{ + // ... Create a person called Mr Leslie. + + Person Mr_Leslie; + + // ... Put some data into the structure. + + strcpy(Mr_Leslie.Name, "Martin"); + + Mr_Leslie.Age=36; + + // ... Extract data from the structure. + // ... Use the 'YearsToRetire' function to + // ... Calculate the number of years this poor + // ... soul has to work before retirement. + + printf("%s will retire in %d years\n", + Mr_Leslie.Name, + Mr_Leslie.YearsToRetire()); // <-- Call the function. + + return (0); +} -- cgit v1.2.3-54-g00ecf