C + + Primer learning note _28_ operator overloading and conversion (3)--overloading of member functions, overriding and hiding, type conversion operators, * operator overloading, operator overloading

Source: Internet
Author: User
Tags function prototype

C + + Primer learning note _28_ operator overloading and conversion (3)--overloading of member functions, overriding and hiding, type conversion operators, * operator overloading, operator overloading
overloading, overwriting and hiding of member functionsfor a member function of the same name at the class level, there are three relationships: overloading, overwriting, and hiding, clearing 3 relationships, and helping to write high-quality code.

1. Overloading of member functions
The concept of overloading is relatively simple, only in the same class definition of a member function with the same name has an overloaded relationship, the main feature when the function parameter type and the number of different, but not the number of function parameters and type are the same, relying solely on the return value of different types to distinguish the function, whichand the overloading of normal functions isexactly the same.. In addition, overloading and member functions are not related to virtual functions, for example:
Class a{...    virtual int fun ();    void fun (int);    void Fun (double, double);    static int fun (char);    ...};
the 4 fun functions in the definition of Class A above are overloaded relationships.
features that are overloaded by member functions:
(1) the same range (in the same class);
(2) the same function name;
(3) different parameter list;
(4) The virtual keyword is optional.

2. Coverage of member functions
overriding refers to overriding a function in a derived class that has the same name in a base class, requiring that the base class function be a virtual function and:
(1) has the same number of parameters as the virtual function of the base class;
(2) has the same parameter type as the virtual function of the base class;
(3) The same return type as the virtual function of the base class: either the same as the base class virtual function, or the pointer (or reference) is returned, and the pointer (or reference) type returned by the derived class virtual function is a subtype (derived type) of the pointer (or reference) type returned by the substituted virtual function in the base class.
as in the following code, the fun in B covers the fun in A.
Class A{public:    virtual void Fun (int.) {}};class b:public a{public:    void Fun (int) {}};
the features covered are as follows:
(1) different ranges (in the derived and base classes, respectively)
(2) the same function name;
(3) the same parameters
(4) The base class function must have a virtual keyword

the difference between overloading and overwriting is as follows:
(1) overlay is the relationship between the subclass and the parent class, which is the vertical relation, and the relationship between the different methods in the same class when overloading is the horizontal relation;
(2) The override requires the same parameter list, overload requires a different list of parameters, overwriting requires the same return type, overloading requires different;
(3) in the overlay relationship, the calling method body is determined by the type of the object, and the overloaded relationship selects the method body based on the argument table and formal parameter list at the time of invocation.

3. Hiding of member functions
hiding means that in some cases, a function in a derived class masks a function with the same name in the base class, including:
(1) Two function parameters are the same, but the base class function is not a virtual function. The difference between a and overwrite is whether the base class function is a virtual function. as follows:
Class A{public:    void Fun (int) {}};class b:public a{public:    void Fun (int) {}  //Hides the fun function of the parent class};
If the above code is:
B B;b.fun (2);
The call will be a function fun in B, if you need to invoke the function fun in a, you can call it in the following form:
B B;b.a::fun (2);


(2) Two function parameters are different, regardless of whether the base class function is a virtual function, the base class function will be masked. The difference between a and overloaded function is that two functions are not in the same class. such as the following code:
Class A{public:    virtual void Fun (int) {}};class b:public a{public:    void Fun (char) {}  //Hides the fun function of the parent class};
If the above code is:
B B;b.fun (2);
is wrong, because the function fun in A is hidden, and the compiler cannot find the function of the type int. This can be done by:
B B;b.a::fun (2);
Call the fun function in a.

"Example 1"
Analyze the running results of the following programs ()
Class A{public:    A () {cout << "construct a" << Endl;}    ~a () {cout << "destroy A" << Endl;}}; Class B:public A{public:    B () {cout << "construct B" << Endl;}    ~b () {cout << "destroy B" << Endl;}}; void Main () {    B obj;}
A, construct a
construct B
Destroy ADestroy B

B, construct A
construct B
Destroy BDestroy A
Answer: B. The constructor of the base class executes first, and the destructor sequence is reversed from the execution order of the constructor function.

"Example 2"
in the following code, the constructors and destructors for a are executed separately ().
A *pa = new A[10];d elete []pa;
A, 1, 1 B, 10, ten C, 1, ten D, 10, 1
Answer: B. The array PA has a total of 10 Class A objects, each of which executes constructors and destructors.


Two, type conversion operators

Must be a member function, not a friend function
No parameters
Cannot specify return type
Function prototype: operator type name ();


#include <iostream>using namespace Std;class integer{public:    Integer (int n);    ~integer ();    Integer &operator++ ();    Integer operator++ (int n);    operator int ();    void Display () const;private:    int N_;};i Nteger::integer (int n): N_ (n) {}integer::~integer () {}integer &integer::operator + + () {    ++n_;    return *this;} Integer integer::operator++ (int n) {    integer tmp (n_);    n_++;    return TMP;} Integer::operator Int () {    return n_;} void Integer::D isplay () const{    cout << n_ << Endl;} int add (int a, int b) {    return a + b;} int main (void) {    Integer n (+);    n = $;    N.display ();    int sum = Add (n, +);    cout << sum << endl;    int x = n;    int y = static_cast<int> (n);    return 0;}

Operation Result:

200

300

Explanation: where n = 200; is to implicitly convert an int to a interger class; int x = n; is to call operator int to convert the Interger class to int, or you can use static_cast, and the Add function also calls operator int for conversion.


third, operator overloading

(1) C + + "and". " The difference

-A is a pointer to its member operator. is a member operator of a struct


struct a{   int A;   int b;}; A *point = malloc (sizeof (struct A));p oint->a = 1; A object;object.a = 1;


(2) class * operator-> (); class & Operator* ();

#include <iostream>using namespace Std;class dbhelper{public:    dbhelper ()    {        cout << "DB ..." << Endl;    }    ~dbhelper ()    {        cout << "~db ..." << Endl;    }    void Open ()    {        cout << "Open ..." << Endl;    }    void Close ()    {        cout << "Close ..." << Endl;    }    void query ()    {        cout << "Query ..." << Endl;    }}; Class Db{public:    DB ()    {        db_ = new DBHelper;    }    ~db ()    {        delete db_;    }    DBHelper *operator-> ()    {        return db_;    }    DBHelper &operator* ()    {        return *db_;    } Private:    dbhelper *db_;}; int main (void) {    db db;    Db->open ();    Db->query ();    Db->close ();    (*DB). Open ();    (*DB). Query ();    (*DB). Close ();    return 0;}
Operation Result:
DB ...
Open ...
Query ...
Close ...
Open ...
Query ...
Close ...
~db ...


Explanation: Db->open (); Equivalent to (db.operator-> ())->open (); Calls operator-> to return a pointer to the DBHelper class, calling the DBHelper member function open (). The advantage of this is that you do not need to know when the DB object needs to be released, and when the lifetime ends, a destructor for the DB class is called, with the delete db_; The destructor of the DBHelper class is also called.

(*DB). Open (); Equivalent to (db.operator* ()). Open ();


Reference:

C + + Primer Fourth Edition

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer learning note _28_ operator overloading and conversion (3)--overloading of member functions, overriding and hiding, type conversion operators, * operator overloading, operator overloading

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.