proffreda

C++ Initialize Structs

Feb 23rd, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct product {
  6.   string name;  int weight;  double price;
  7. } ;
  8.  
  9. void use (product*);
  10.  
  11. int main() {
  12. product apple = {"McIntosh Apple", 12, 1.50};
  13. cout << "Name: " << apple.name << endl;
  14. cout << "Weight: " << apple.weight << endl;
  15. cout << "Price: $" <<apple.price << endl << endl;
  16.  
  17. cout << sizeof(apple) << " >= " <<
  18. sizeof(apple.name) <<  " + " <<
  19. sizeof(apple.weight) << " + " <<
  20. sizeof(apple.price) << endl <<endl;
  21.  
  22. product*  aptr = &apple;
  23. cout << "Product Name: " << aptr->name << endl;
  24. cout << "Weight: "<<  aptr->weight << endl;
  25. cout << "Price: $" << aptr->price << endl << endl;
  26.  
  27. use(aptr);
  28. cout << "After Using Product: " << aptr->name << endl;
  29. cout << "Weight: "<<  aptr->weight << endl;
  30. cout << "Price: $" << aptr->price << endl << endl;
  31. }
  32.  
  33. void use (product* a) {
  34.     a->weight = a->weight / 2;
  35.     a->price = a->price / 4;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment