Definition and implementation of C + + classes (Class) __c++

Source: Internet
Author: User
Tags class definition

reproduced from: http://www.cnblogs.com/mr-wid/archive/2013/02/18/2916309.html (but there are some errors in his content)

I. Introduction to "Class"
In C + +, use the "class"To describe"Object, the so-called "object" refers to all things in the real world. Then the class can be seen as an abstraction of something similar, find common ground between these different things, such as bicycles and motorcycles, they all belong to the "object", and have the same point, and some different points, the same as they all have the quality, have two wheels, all belong to transportation and so on. "All has the quality", "two wheels" belongs to this object's attribute, but "all can be used as the vehicle" belongs to this object to have the behavior, also called the method.

A class is a user-defined data type, and the type of data has a certain behavioral capability, that is, the method described in the class. Generally, the definition of a class contains two parts of the content, one is thePropertyAnd the other part is what it hasMethod。 In the category of "human", each person has his or her own name, age, date of birth, weight, etc., for the attributes of human beings, in addition, people can eat, sleep, walk, talk, etc. belong to human behavior.

The "human" class described in the above example is just some of the most basic attributes and behaviors that have a human object, which can be called human "base class"。 Besides, some people with some professions, such as students, and a student also have attributes that are not in the "base class", such as school, class and study number; You can also have behavior that the base class does not have, such as having to go to class every day and need exams.

A student class can be viewed as an extension of a base class, because he has all the attributes and behaviors of the base class, and on this basis adds some attributes and behaviors that are not available to the base class, such as "student" classes called "human" as the base class.Derived ClassesOrSub Class"。 In the student base Shanghai can be further expanded out of other more advanced classes, such as "Graduate" class.

In this way, we are no longer to introduce the other related knowledge of the class.



Ii. definition of C + + classes

C + + uses the keyword class to define a class, the basic form of which is as follows:

Class name
{
        public://Common behavior or attribute

    private:
        //public behavior or attribute
};

Description
①. Class names need to follow the general naming rules;

②. Public and private as the attribute/method restrictions of the keyword, private means that the content is private, can not be accessed or invoked outside, only by the internal access to the class; The public represents the properties and methods that can be accessed or invoked directly by the outside world.
Generally speaking, the attribute members of a class should be private and public only for the function interfaces that are used by the outside world, but this is not mandatory and can be adjusted according to the requirements;

③. The semicolon for the end part cannot be omitted.

Implementation of C + + class
In the above definition example we just define some of the properties and method declarations for this class, and do not implement it, and the implementation of the class is the process of accomplishing its methods. There are two ways to implement a class, one is to complete the definition of a member function when the class is defined, and the other to do so outside of the class definition.

1>. Defining member functions when a class is defined
The implementation of a member function can be completed at the same time as the class definition, such as code:

#include <iostream>
using namespace std;
Class Point
{public
    :
        void setpoint (int x, int y)//implementation SetPoint function
        {
            xpos = x;
            YPos = y;
        }

        void Printpoint ()       //Implementation Printpoint function
        {
            cout<< "x =" << xpos << Endl;
            cout<< "y =" << yPos << Endl;
        }

    Private:
        int xpos;
        int yPos;
};

int main ()
{point
    M;        Create an object point M
    m.setpoint (10, 20) with the defined class,//Set the X,y value of the M point
    m.printpoint ();     Output m-point information return

    0;
}


Run the output:

	x = ten
        y = 20

As opposed to the definition of a class, implementing a member function within a class is no longer a declaration within a class, but rather a function that is defined directly to define a member function in a class by default, and the compiler will strive to define it as a inline-type function 2>. Defining member functions outside of a class
Defining a member function outside of a class is done by declaring it inside the class, and then by using the scope operator outside the class: : to implement the form as follows:

return type class Name:: member function name (parameter list)
{
     //function Body
}

#include <iostream>
using namespace std;
Class Point
{public
    :
        void setpoint (int x, int y);//declare
        void Printpoint () within a class on a member function;

    Private:
        int xpos;
        int yPos;
};
void Point::setpoint (int x, int y)//via scope operator ':: ' implement SetPoint function
{
    xpos = x;
    YPos = y;
}
void point::p rintpoint ()       //Implementation Printpoint function
{
    cout<< "x =" << xpos << Endl;
    cout<< "y =" << yPos << Endl;
}
int main ()
{point
    M;        Create an object point M
    m.setpoint (10, 20) with the defined class,//Set the X,y value of the M point
    m.printpoint ();     Output m-point information return

    0;
}


In the case of the SetPoint member function, the form declared within the class is void SetPoint (int x, int y) , then the function header should be void point::setpoint (int x, int y) when defined outside the class in this form, the return type, the member function name, and the argument list are consistent in the form of declarations within the class.

scope, visible domain and life cycle of objects
The scope of the class object, the visible domain, and the lifetime are the same as the normal variable, and when the object's lifetime ends, the object is automatically revoked and the memory consumed is reclaimed, and it should be noted that if there is a new or malloc in the member function of the object The requested dynamic memory program will not release it, we need to manually clean it, otherwise it will cause memory leaks.

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.