C + + class Basic Syntax example Analysis _c language

Source: Internet
Author: User

Class is a very important concept of C + + programming, and this article illustrates the common usage of classes in instance form. Specifically as follows:

This test code mainly includes the following content:

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

The instance code is as follows:

#include <iostream> using namespace std;

Enum Sextype {man, WOMAN};
    Class Human {//the default is private private:string name;
    Sextype sex;

    int age;
    (5) Defining class constants: One method is to use an enum, and the other is to use static enum{len=1};

  static const int LEN2 = 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 to provide a default constructor://(1) to define a constructor with no parameters: Human ();
    (2) Provide a default value for a parameter that is not a default constructor: Human (String m_name= "No Name", int m_age=0, Sextype m_sex=man);
    Two definitions can only be two selected 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 of a class declaration.
    The Void Show () const//const is added after the function name to indicate 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 (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");//Show Call Human Jerry ("Jerry", Num, man); Implicit invocation of Human *ptom = new Human ("Tom", man); The new call//When the constructor has only one argument, you can assign the value directly with the assignment statement. The constructor of only one parameter will be automatically invoked Human marry = 11;

    Assignment Call//(2) Defaults construct function Human Lucy;
    (3) Assignment Object Human James; James = Human ("James", man); Create a temporary object james,copy a portion of this object is assigned to the James variable.

    The temporary object will be destroyed immediately thereafter.
    (4) Const const Human Thomas ("Thomas", man); Thomas.show ();
The Show method must define with ' const '} return 0;
 }

The result of the program running 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.