C + + I want to use this (vii)

Source: Internet
Author: User

Next, continue with the remainder of the object-based programming syntax:

6. Const member functions and const objects for classes

Const data member: just like the const constant, only one in the class (and in the constructor), one outside the class, must be initialized.
Const member function : The normal member function is then added Const. It can read the values of data members, but cannot modify them. To modify, the data member must be mutable before it is added. To specify that it can be arbitrarily changed. Mutable is ansic++ considering the actual programming, it might be necessary to modify a data member in a const object. The const member function can be overloaded with non-const member functions of the same parameter table.
Const Object : Only const member functions can be called, but constructors and destructors are the only member functions that are not const member functions but can be called by a const object.

Here is a simple example
#include <iostream>
using namespace Std;

Class AClass
{
Public
AClass (int a,int b);
void Print () const;

void Print (); Overloaded Const function
void Set (int a);
Protected
Private
const int x;
mutable int y;
};

Aclass::aclass (int a,int B): X (a), Y (b)
{  }

void AClass::P rint () const
{
y = 9;
cout<< "x=" <<x << "y=" << y<<endl;
}

Voidaclass::P rint ()//implementation, non-const object calls the function instead of the const function
{
y = 8;
cout<< "x=" <<x << "y=" << y<<endl;
}

void Aclass::set (int a)
{
y = A;
}

int main ()
{
AClass a (in);

A.print (); X=1 y=8
Const AClass b (n);
B.print (); X=1 y=9

return 0;
}

*7. Operator overloading

Operator overloading is a function that defines an overloaded operator that is automatically called when an overloaded operator is required to perform the corresponding operation. In other words, operator overloading is achieved by defining functions. Operator overloading is essentially an overload of a function. The functions of overloaded operators are generally formatted as follows:

function type operator operator name (formal parameter table column) {Overloaded handling of operators}

For example, to use "+" for the addition of complex (complex numbers), the prototype of the function can be:

Complex operator + (Complex & C1,complex &c2);

Where operator is the keyword that is used specifically to define the functions of overloaded operators, the operator name is the predefined operator that C + + provides to the user. Note: The function name is made up of operator and operators. The above operator+ is the function name, which means "to operator + reload". As long as this is mastered, it can be found that this type of function differs from other functions in form.

The overloads of the operators can be implemented in two ways, with the member function implementation and the friend function implementing the operator overloading comparison:

1. When the member function and the friend function implement operator overloading, its function name must start with the keyword operator, followed by the appropriate operator, but the parameter number of the friend function is more than the implementation of the member function;

2. When the member function is implemented, the first operand must be the object of the class, but when the friend function is implemented, as long as one of the operands is the object of the class, two operands can be exchanged to meet the exchange rate when the friend function is implemented.

3. Some operators only allow the implementation of member functions (such as assignment operators), and some only allow the implementation of friend functions (such as extracting, inserting operators), etc.;

4. The member function implies this pointer, while the friend function does not have this pointer;

The specific implementation method is not known, because operator overloading is based on object programming in a less used thing, do not spend too much time to study. Operator overloading is closely related to polymorphism, which is the core of object-oriented. What we're talking about here is the object-based, ADT.



*8. Other

Well, in fact, the concepts related to the class are not only above, such as:

A. In addition to the protect modifier outside public and private, I did not mention that the reason is simple, the protection type is used for the inheritance relationship, our object-based classes are not inherited;

B. Friends of the concept, is also very complex, if you are based on object style code will need friend, then there are generally only two cases: one is that your class too much, the type is messy, the difference is not small, this time you already need object-oriented programming style, you have to consider the inheritance relationship, you can not use the flat class alone. The second scenario is that your class rarely needs to be UF, so redesign your type, because your previous design must be unreasonable.

C. Well, there are complex operator overloads, in fact, the operator overloading itself has a lot of object-oriented ideas inside, so the object-based style is seldom used, at most, a few simple unary operator overloads.

" A Better C = C with Class = C + new/delete + parameter default + function overload + few operator overloads + no inheritance Class "

This is the last article in this series, and as a finishing touches, I introduce two descriptions:

Description One: C + + has four main parts: better C,adt,oo, and GP, as well as developing functional, Generative,meta programming and so on. Better C, only increases the class C subset of simple features such as function overloading, reference types, default parameters, and so on. ADT C + +, the entire program is composed of a planar concrete class (concrete Class) object, no inheritance, no polymorphism.

Description Two:"C + + three people talk" See the devil once pointed out that C + + programming paradigm can be divided into adt+pp,gp,oo three directions. 1, adt+pp adt:abstract data type; Abstract data type Pp:procedure programme; Process-oriented Programming paradigm ADT+PP is the process-oriented programming paradigm + abstract data type, which you can understand as the predecessor of C + +: C with class. 2, (Generic programming, generic programming) known as the programming thought of another revolution. is a parameterized (parameterization)-based programming technique designed to generalize and optimize useful algorithms or data structures as much as possible. 3, OO: Object-oriented programming.

The above ideas and Essentia C + + books on the Basic Agreement, also shows a problem: C + + is a multi-paradigm programming language, different programmers use him to write different paradigms of the program, no pros and cons. This series is the first of the above programming paradigm, you can call him C with class also can be called C+adt also can be called based on object, anyway, meaning is a. The basic grammatical composition is: process-oriented syntax (c syntax) + Flat class syntax (that is, only abstractions and encapsulation, no inheritance, and polymorphism).

digression: In fact, the simple C can be done based on the object, and there is no pain, but if you are like the class keyword and "." Operator brings the thrill, then this "C + + I want to use this" series describes the C with class is your good choice. In addition, there are many even use C to achieve generics, single-inheritance, polymorphic .... But I really do not recommend you to use, one too alternative, no human and material support, and secondly, after all, is a simulation of the implementation, not only limited functionality, the feeling of egg pain is not common people can get used to. Choosing a language for this paradigm is an orthodox choice, like Ruby in the bones of Oo, which is born of PP C and so on.

Future message: really crazy about the programming paradigm, this series is the "Object-based (Pp+adt) programming paradigm" salute it, although still want to write a pure C implementation of object-based programming series, But it's also possible to immerse myself in the feast of the object-oriented programming paradigm that Ruby (or some other better oo language) brings to me. Ha ha

C + + I want to use this (vii)

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.