The class of the declaration class in C++ and the struct keyword of the declaration structure

  • 2020-05-07 20:13:52
  • OfStack

class
The
class keyword declares a class type or defines an object of a class type.
grammar


   [template-spec]
    class [ms-decl-spec] [tag [: base-list ]]
{
  member-list
} [declarators];
[ class ] tag declarators;

parameter
template-spec
Optional template description.
ms-decl-spec
Optional storage class description for more information
tag
Give the type name to the class. Tags in the class scope become reserved words. Flags are optional. If omitted, define an anonymous class.
base-list
An optional list of classes or structures from which this class derives its members.
member-list
List of class members.
declarators
Specifies a list of declarators for one or more instance names of a class type. If all data members of the class are public, the declarator can contain a list of initializers.

Using an example


// class.cpp
// compile with: /EHsc
// Example of the class keyword
// Exhibits polymorphism/virtual functions.

#include <iostream>
#include <string>
#define TRUE = 1
using namespace std;

class dog
{
public:
  dog()
  {
   _legs = 4;
   _bark = true;
  }

  void setDogSize(string dogSize)
  {
   _dogSize = dogSize;
  }
  virtual void setEars(string type)   // virtual function
  {
   _earType = type;
  }

private:
  string _dogSize, _earType;
  int _legs;
  bool _bark;

};

class breed : public dog
{
public:
  breed( string color, string size)
  {
   _color = color;
   setDogSize(size);
  }

  string getColor()
  {
   return _color;
  }

  // virtual function redefined
  void setEars(string length, string type)
  {
   _earLength = length;
   _earType = type;
  }

protected:
  string _color, _earLength, _earType;
};

int main()
{
  dog mongrel;
  breed labrador("yellow", "large");
  mongrel.setEars("pointy");
  labrador.setEars("long", "floppy");
  cout << "Cody is a " << labrador.getColor() << " labrador" << endl;
}


struct
The
struct keyword defines variables for the structure type and/or structure type.


[template-spec] struct[ms-decl-spec] [tag [: base-list ]]
{ 
  member-list 
} [declarators];
[struct] tag declarators;

parameter
As for the parameters of class, you can refer to the above.
note
A structure type is a user-defined composite type. It consists of fields or members that can have different types.
In C++, the structure is the same as the class, except that its members default to public.
Using the structure
In C, you must explicitly declare the structure using the struct keyword. In C++, you do not need to use the struct keyword after defining the type.
You can optionally declare a variable by placing one or more comma-separated variable names between the close curly brace and the semicolon when defining a structure type.
Structure variables can be initialized. Initialization of each variable must be enclosed in braces.
For information, see class, union, and enum.
The sample


 #include <iostream>
using namespace std;

struct PERSON {  // Declare PERSON struct type
  int age;  // Declare member types
  long ss;
  float weight;
  char name[25];
} family_member;  // Define object of type PERSON

struct CELL {  // Declare CELL bit field
  unsigned short character : 8; // 00000000 ????????
  unsigned short foreground : 3; // 00000??? 00000000
  unsigned short intensity : 1; // 0000?000 00000000
  unsigned short background : 3; // 0???0000 00000000
  unsigned short blink   : 1; // ?0000000 00000000
} screen[25][80];    // Array of bit fields 

int main() {
  struct PERSON sister;  // C style structure declaration
  PERSON brother;  // C++ style structure declaration
  sister.age = 13;  // assign values to members
  brother.age = 7;
  cout << "sister.age = " << sister.age << '\n';
  cout << "brother.age = " << brother.age << '\n';

  CELL my_cell;
  my_cell.character = 1;
  cout << "my_cell.character = " << my_cell.character;
}
// Output:
// sister.age = 13
// brother.age = 7
// my_cell.character = 1


Related articles: