[C + +] C + + class basic syntax

Source: Internet
Author: User

This test code includes the following:

(1) How to use the constructor function;
(2) default constructor;
(3) Assigning values between objects;
(4) const use syntax;
(5) Defining a Class constant: One method is to use an enum, and the other is to use static.

#include <iostream>using namespacestd;enumsextype{man, WOMAN};classhuman{//The default is private    Private:        stringname;        Sextype sex; intAge ; //(5) Defining a Class constant: One method is to use an enum, and the other is to use the static        enum{len=1}; Static Const intLEN2 =3;  Public:       //If no constructors are provided in the class definition, the compiler provides a default constructor.       However, if a constructor is defined in a class, the writer must provide a default constructor at the same time. //There are two ways of providing a default constructor://(1) Define a constructor with no parameters: Human (); //(2) Provide default values for parameters of non-default constructors: Human (String m_name= "No Name", int m_age=0, Sextype m_sex=man); //two ways to define it can only two select oneHuman (); Human (stringM_name,intm_age, Sextype m_sex); Human (intm_age); ~Human (); //The method defined in the class declaration is inline. You can also use the inline keyword to define a function outside the class declaration.        voidShow ()Const  //a const appended to the function name indicates that the function does not modify the data members of the class. {cout<<" This is"<<name<<", Sex:"<<sex<<", "<<age<<"years old."<<Endl; }}; Human::human () {cout<<"default construct function"<<Endl;} Human::human (stringM_name,intm_age, Sextype m_sex) {cout<<"construct function:"<<m_name<<Endl; Name=M_name; Age=M_age; Sex=M_sex;} Human::human (intm_age) { Age=M_age;} Human::~Human () {cout<<"Destroy function:"<<name<<Endl;}intMain () {cout<<"This is test code of C + + class:"<<Endl; {        //(1) Use of construct functionHuman Jack = Human ("Jack", -, man);//Show CallHuman Jerry ("Jerry", -, man);//Implicit invocationHuman *ptom =NewHuman ("Tom",Ten, man);//New Call//You can assign a value directly with an assignment statement when the constructor has only one argument. Constructors with only one argument will be automatically calledHuman marry = One;//Assignment Invocation//(2) Defaults construct functionHuman Lucy; //(3) Assignment ObjectHuman James; James= Human ("James", -, man);//Create a temporary object james,copy a portion of the object assigned to the James variable.        The temporary object is immediately destroyed. //(4) const        ConstHuman Thomas ("Thomas", in, man);  Thomas.show (); //The Show method must define with ' const '    }    return 0;}

The result of the operation is:

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.