Object-oriented Programming-c++ Default constructor & Copy constructor& destructor & Operator overloading "Nineth class notes"

Source: Internet
Author: User

Let's take notes first:

The content of this class is about

    1. constructor function
    2. Destructors
    3. Operator overloading
    4. return * This

The content is very fine, everybody reviews the note to take care of the procedure review:)

#include <iostream>using namespacestd;classInteger { Public:    inti; intGeti ()Const{return  This-i;} voidSETI (inti) { This->i =i;} Integer (intj =0); Integer (Integer&c); ~Integer ();};i Nteger::integer (Integer& c) {//Constructer Functioni = -; cout<<"Integer (integer & C)"<<Endl;} Integer::integer (intJ): I (j) {//Copy constructer Functioncout <<"Integer (int j)"<<Endl;} Integer::~integer () {//Destructer Functioncout <<"~integer ()"<<Endl;}ConstInteger & Fun (ConstInteger &inch) {cout<<inch. Geti () <<Endl; return inch;}intMain () {Integer B (8);    Fun (b). Geti ();    Integer A (b); return 0;}

The following program is about operator overloading

Log print allows you to track the order in which the destructors are observed

There are also examples of reference returns, friend functions

#include <iostream>using namespacestd;classInteger {Private:    inti; Public:    intGeti ()Const{return  This-i;} voidSETI (inti) { This->i =i;} Integer (intj =0); Integer (Integer&c); ~Integer (); ConstInteger &operator= (ConstInteger &inch); FriendConstIntegeroperator+ (ConstInteger & LHS,ConstInteger &rhs);};ConstInteger & Integer::operator= (ConstInteger &inch) {//Member Functioni =inch. i + -; //return in;    return* This;}ConstIntegeroperator+ (ConstInteger & LHS,ConstInteger & RHS) {//Global FunctionsInteger out;  out. i = lhs.i +rhs.i; return  out;} Integer::integer (Integer& c) {//Constructer Functioni = -; cout<<"Integer (integer & C)"<<Endl;} Integer::integer (intJ): I (j) {//Copy constructer Functioncout <<"Integer (int j)"<<' '<< J <<Endl;} Integer::~integer () {//Destructer Functioncout <<"~integer ()"<<' '<< I <<Endl;}intMain () {Integer B (8), A, C, D; cout<<"A.geti ()"<< A.geti () <<Endl; D= c = A =b; cout<<"A.geti ()"<< A.geti () <<Endl; cout<<"C.geti ()"<< C.geti () <<Endl; cout<<"D.geti ()"<< D.geti () <<Endl; D= A +b; cout<<"D.geti ()"<< D.geti () <<Endl; return 0;}

Object-Oriented Programming-c++ Default constructor & Copy constructor& destructor & Operator Overloading "Nineth lesson Notes"

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.