C++ structure membrers

STRUCTURE

  • A Structure is a container, it can hold a bunch of things.

–These things can be of any type.

  • Structures are used to organize related data (variables) into a nice neat package.

EXAPMES STUDENT RECORD

  • Student Record:

–Name   a string
–HW Grades  an array of 3 doubles
–Test Grades  an array of 2 doubles
–Final Average  a double

STRUCTURE MEMBRERS

  • Each thing in a structure is called member.
  • Each member has a name, a type and a value.
  • Names follow the rules for variable names.
  • Types can be any defined type.

EXAMPLE STRUCTURE DEFINITION

struct StudentRecord {

  char *name;  // student name
  double hw[3];  // homework grades
  double test[2];  // test grades
  double ave;  // final average

};

USING A STRUCT

  • By defining a structure you create a new data type.
  • Once a struct is defined, you can create variables of the new type.

StudentRecord stu;

ACCESSING MEMBERS

  • You can treat the members of a struct just like variables.
  • You need to use the member access operator ‘.’ (pronounced « dot »):

cout << stu.name << endl;

stu.hw[2] = 82.3;

stu.ave = total/100;   

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *