structure help

This is a discussion on structure help within the C++ Programming forums, part of the General Programming Boards category; hi im really new to structures and i just wanted to know any real programming applications to them, like any ...

  1. #1
    tetra
    Guest

    structure help

    hi im really new to structures and i just wanted to know any real programming applications to them, like any use for them

    any examples would help as well


    thanks.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    A struct is a conglomeration of other types. In c++ this structure becomes a new type itself. For instance a structure that defines fractional numbers may look like this...
    Code:
    struct Fraction
    {
       int denominator;
       int numerator;
    };
    Now we have a type called Fraction that holds 2 int members. These members can be accessed by the dot operator . or with the arrow operator -> if you have a pointer to a Fraction instead.
    Code:
    Fraction fraction;
    fraction.numerator = 1;
    fraction.denominator = 2; // fraction now represents a half
    Fraction* pfraction = &fraction;
    pfraction->denominator = 4; // use a pointer to change fraction to a quarter
    All real applications use structures in one way or another. The common struct is more important in c than c++ because c++ offers the class keyword. In c++ the only thing you have to remember about structs is that their default access specifier is public whereas class default access specifier is private. i.e
    Code:
    struct A
    {
       int x; // this is public
    };
    
    class B
    {
       int x; // this is private
    };
    
    struct C : A  // Yes we can inherit structs. this is public inheritance
    {
       int y;
    };
    
    class D : B // this is private inheritance
    {
       int y;
    };
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 06:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21