Vernacular C + + series (9)

Source: Internet
Author: User
Tags class definition

Wonderful out-of-class definition in-class definition

Question: What is in-class definition?

The way in which the function body of a member function is written inside the class is defined within the class. For example, the following class of student, we can see that when defining member functions, including the function body that each member function uses to implement, is inside the class.

Defining relationships within a class with inline functions

A member function defined within a class, the compiler will first compile it as an inline function, but for a complex member function that cannot be compiled into an inline function, it is compiled into a normal function.

Out-of-class definitions

The so-called out-of-class definition refers to the function body of a member function written outside the class. Specifically, the out-of-class definition is divided into the following two forms:

The so-called external definition of the same file, refers to the member function, although defined outside the class, but its definition and the definition of the class in the same file. As in the following example:

We create a new file, the file name is Car.cpp, and then define a car class in this file, the definition of the class car to write the definition (class car), and declare the corresponding member functions, but the implementation of the member function or the definition of member function body is also written in this file. So since the member function is written out of the class, we have to indicate that this member function is not a normal function, but rather belongs to the car's function, we need to precede this function with this class name plus:: Mark this function belongs to this class.

The following is a description of the sub-file definition, if the same as the definition of the file is a guerrilla, the sub-document definition can be regarded as the regular army. Almost all C + + projects, but any professional C + + programmers will be the definition of the class to complete the file, so there are many benefits, we will be in the follow-up course to give you a step-by-stage explanation. Let's look at an example to illustrate the definition of a sub-file.

When defining a file, we need to define a. h file (header file), and the class name recommendation is consistent with the file name. The following is a Car.h header file, in the header file we declare all the data members and member functions in the class, in another file (Car.cpp), we define all the member functions, the definition of the same way as before, the most important thing is to include its header file (Car.h) into the Car.cpp, if not With this inclusion, Car.cpp will not be able to find its corresponding declaration.

Code practices

Title Description:

The definition of a teacher class requires that the same file-class definition and sub-file definition be used, respectively, in such a way that the following are required:

Data members:

Name

Age

Gender

member functions:

Encapsulating functions for data members

Lecture teach

How to define in-class:

#include <iostream>#include<stdlib.h>#include<string>usingnamespacestd;/*  ************************************************************************//*define a teacher class that requires the same file-class definition and sub-file-class definition, respectively, as follows:/* data member:/* Name///age//gender/* member function:/* Package for data member function/* Lessons teach/* ************************************************************************/classteacher{ Public: Voidsetname (string_name);    Stringgetname (); Voidsetgender (string_gender);    Stringgetgender (); Voidsetage (int_age);    Intgetage (); voidteach ();Private: Stringm_strname;    Stringm_strgender; Intm_iage;}; Voidteacher::setname (string_name) {m_strname=_name;} Stringteacher::getname () {returnm_strname;} Voidteacher::setgender (string_gender) {M_strgender=_gender;} Stringteacher::getgender () {returnm_strgender;} Voidteacher::setage (int_age) {m_iage=_age;} Intteacher::getage () {returnm_iage;} Voidteacher:: Teach () {cout<<"start class now ....."<<Endl;}intMain () {Teacher T; T.setname ("Confucius"); T.setgender ("male"); T.setage ( -); cout<<t.getname () <<" "<<t.getgender () <<" "<<t.getage () <<" "<<Endl;    T.teach (); System ("Pause"); return 0;}

Operation Result:

Out-of-class definition mode:

The program framework is as follows

Header file (Teacher.h)

#include <string>usingnamespacestd;classteacher{public:    voidsetname (  string  _name);    Stringgetname ();    Voidsetgender (string  _gender);    Stringgetgender ();    Voidsetage (int  _age);    Intgetage ();     void teach (); Private :    stringm_strname;        Stringm_strgender;    Intm_iage;};

Source file:

#include"Teacher.h"#include<iostream>#include<stdlib.h>usingnamespacestd;/*  ************************************************************************//*define a teacher class that requires the same file-class definition and sub-file-class definition, respectively, as follows:/* data member:/* Name///age//gender/* member function:/* Package for data member function/* Lessons teach/* ************************************************************************/Voidteacher::setname (string_name) {m_strname=_name;} Stringteacher::getname () {returnm_strname;} Voidteacher::setgender (string_gender) {M_strgender=_gender;} Stringteacher::getgender () {returnm_strgender;} Voidteacher::setage (int_age) {m_iage=_age;} Intteacher::getage () {returnm_iage;} Voidteacher:: Teach () {cout<<"start class now ....."<<Endl;}intMain () {Teacher T; T.setname ("Confucius"); T.setgender ("male"); T.setage ( -); cout<<t.getname () <<" "<<t.getgender () <<" "<<t.getage () <<" "<<Endl;    T.teach (); System ("Pause"); return 0;}

Operation Result:

Vernacular C + + series (9)

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.