Shows initialization of structure variables - C++ Data Type

C++ examples for Data Type:struct

Description

Shows initialization of structure variables

Demo Code

#include <iostream>
using namespace std;
struct Product                   //specify a structure
{
   int modelnumber;           //ID number of widget
   int partnumber;            //ID number of widget part
   float cost;                //cost of part
};
int main()//from   www  .  j av  a  2s  .com
{                          //initialize variable
    Product part1 = { 6, 3, 2.55F };
    Product part2;                //define variable

    cout << "Model "  << part1.modelnumber;
    cout << ", part "   << part1.partnumber;
    cout << ", costs $" << part1.cost << endl;
    part2 = part1;             //assign first variable to second

    cout << "Model "  << part2.modelnumber;
    cout << ", part "   << part2.partnumber;
    cout << ", costs $" << part2.cost << endl;
    return 0;
}

Result


Related Tutorials