In C + + the class of declaring classes and the struct keyword of the declaration structure _c language

Source: Internet
Author: User

Class
The class keyword declares the type or object that defines the class type.
Grammar

   [Template-spec]
    class [Ms-decl-spec] [tag [: Base-list]]]
{
  member-list
} [declarators];
[Class] Tag declarators;

Parameters
Template-spec
Optional Template description.
Ms-decl-spec
Optional storage class description for more information
Tag
The type name given to the class. tags within the scope of the class become reserved words. Flags are optional. If omitted, defines an anonymous class.
Base-list
An optional list of classes or structs from which this class derives its members.
Member-list
A list of class members.
Declarators
Specifies the list of declarations for one or more instance names of the class type. If all data members of a class are public, the declaration can contain an initializer list.

Use examples

Class.cpp//compile with:/EHSC//Example of the class keyword//exhibits polymorphism/virtual.

#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 a variable for a struct type and/or struct type.

[Template-spec] struct[ms-decl-spec] [tag [: Base-list]]]
{ 
  member-list 
} [declarators];
[struct] tag declarators;

Parameters
The same as the class parameter, you can refer to the above.
Note
A struct type is a user-defined composite type. It consists of fields or members that can have different types.
In C + +, structs are the same as classes, except that their members are public by default.
Use structure
In C, you must explicitly use the struct keyword to declare the structure. In C + +, you do not need to use the struct keyword after defining the type.
You can choose to declare a variable by placing one or more comma-delimited variable names between the closing brace and the semicolon when defining the struct type.
Structure variables can be initialized. The initialization of each variable must be enclosed in curly braces.
For related information, see Class, Union, and enum.
Example

 #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 the 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 =//Brother.age = 7//My_cell.character = 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.