A very simple C++ program example demonstrating the struct type usage

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Displaying some data hold by struct data type in C++ programming

To show: How to use struct data type and a normal variable in C++ programming

 

// the struct C++ program example

#include <iostream>

using namespace std;

 

// struct data type with one element

struct item

{

int keep_data;

};

 

void main(void)

{

// struct type variable

item John_cat, Joe_cat, Big_cat;

// normal variable

int garfield;

 

cout<<"The struct size = "<<sizeof(struct item)<<endl;

 

// assigning values

John_cat.keep_data = 10;

Joe_cat.keep_data = 11;

Big_cat.keep_data = 12;

garfield = 13;

 

// printing data

cout<<"Data value for John_cat is "<<John_cat.keep_data<<endl;

cout<<"Data value for Joe_cat is "<<Joe_cat.keep_data<<endl;

cout<<"Data value for Big_cat is "<<Big_cat.keep_data<<endl;

cout<<"Data value for garfield is "<<garfield<<endl;

 

return;

}

 

Output example:

 

The struct size = 4

Data value for John_cat is 10

Data value for Joe_cat is 11

Data value for Big_cat is 12

Data value for garfield is 13

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index