C++ struct initialization of structure variables

Description

C++ struct initialization of structure variables

#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()/*  ww w.j  ava2 s.  c o m*/
{                          //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;
}



PreviousNext

Related