[C ++] C ++ class basic syntax

Source: Internet
Author: User
Tags define local

[C ++] C ++ class basic syntax

The test code includes the following:

(1) How to Use constructors;
(2) default constructor;
(3) assignment between objects;
(4) use const syntax;
(5) Defining class constants: One method is to use enum, And the other method is to use static.

# Include <iostream> using namespace std; enum sexType {MAN, WOMAN}; class Human {// the default is private: string name; sexType sex; int age; // (5) define a class constant: One method is to use enum, And the other method is to use static enum {LEN = 1}; static const int LEN2 = 3; public: // if no constructor is provided in the class definition, the compiler provides the default constructor. However, if constructors are defined in the class, the writer must provide a default constructor at the same time. // There are two ways to provide the default constructor: // (1) define a constructor without parameters: Human (); // (2) the default value is Human (string m_name = "no name", int m_age = 0, sexType m_sex = MAN) for non-default constructor parameters ); // You can select only one of the two definitions: Human (); Human (string m_name, int m_age, sexType m_sex); Human (int m_age );~ Human (); // the method defined in the class declaration is an inline method. You can also use the inline keyword to define a function outside the class declaration. Void show () const // after adding const to the function name, this function will not modify the data members of this class. {Cout <"This is" <name <", sex:" <sex <"," <age <"Years old. "<endl ;}; Human: Human () {cout <" default construct function "<endl ;}human: Human (string m_name, int m_age, sexType m_sex) {cout <"construct function:" <m_name <endl; name = m_name; age = m_age; sex = m_sex;} Human: Human (int m_age) {age = m_age;} Human ::~ Human () {cout <"destroy function:" <name <endl;} int main () {cout <"This is test code of C ++ class: "<endl; {// (1) use of construct function Human jack = Human (" Jack ", 30, MAN); // display the call of Human jerry (" Jerry ", 26, MAN); // call Human * pTom = new Human ("Tom", 10, MAN) implicitly; // call New // when the constructor has only one parameter, you can directly assign values using the value assignment statement. Only one constructor will automatically call Human marry = 11; // assign a value call // (2) defaults construct function Human Lucy; // (3) value assignment object: Human James; James = Human ("James", 28, MAN); // create a temporary object, James. copy this object and assign it to the James variable. Then the temporary object will be destroyed. // (4) const Human Thomas ("Thomas", 29, MAN); Thomas. show (); // The show method must define with 'const'} return 0 ;}

The running result is:


What is the difference between c syntax and c ++ syntax?

It is not supported in c because the syntax format is not in Standard c, so the gcc compiler cannot recognize it normally, and C ++ is a superset of c language, in this way, you can define local variables in. In fact, you can try this as follows:
Int I = 0;
For (; I <10; I ++)

Basic Syntax of C Language

A keyword is a word that has been used by Turbo C2.0 and cannot be used for other purposes. For example, a key cannot be used as a variable name or function name. Turbo C2.0 has the following keywords: Turbo C2.0 extends a total of 11 asm _ cs, _ ds, _ es, _ ss, cdecl, far, near, huge, interrupt, pascal, defined by ANSI, there are 32 auto, double, int, struct, break, long, switch, case, enum, register, typedef, char, extern, return, union, const, float, short, unsigned, continue, for, signed, void, default, goto, sizeof, volatile, do, if, while, static,

Related Article

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.