C + + Constructor _ copy constructor

Source: Internet
Author: User

Copy constructor

Functions automatically generated by the system:

Common constructors

Copy constructor

If you define a normal constructor, the system will no longer automatically generate ordinary constructors;

If you define a copy constructor, the copy constructor is no longer automatically generated by the system.

If there is no custom copy constructor, the system automatically generates a default copy constructor.

The copy constructor is automatically called when the instance object is initialized with direct initialization or copied.

A copy constructor is a special constructor that initializes a newly created object when it is created by using an object that was previously created in the same class. Copy constructors are typically used to:

    • Initializes a newly created object by using another object of the same type.

    • The Copy object passes it as a parameter to the function.

    • Copies the object and returns the object from the function.

If a copy constructor is not defined in the class, the compiler defines it by itself. If a class has a pointer variable and has a dynamic memory allocation, it must have a copy constructor. The most common form of a copy constructor is as follows:

ClassName (const classname &obj) {   //  Body of Constructor }
Defining formats

    Class name (const class name & variable name)

class student{public:    "Jim"//  General Constructor    Student (const//  copy constructor private:    string  m_strname;}

code example:

Demo.cpp

#include <iostream>#include<stdlib.h>#include<string>#include"Teacher.h"using namespacestd;/*************************************************************************teacher Class Custom copy constructor data member: Name Age member function: Wrapper function for data members *************************************************************************/voidTest (Teacher t) {}intMainvoid) {Teacher T1; //Calling normal functionsTest (T1);//Call copy ConstructorTeacher t2 = t1;//Call copy ConstructorTeacher t3 (t1);//Call copy ConstructorSystem ("Pause"); return 0;}

Teacher.h

#include <string>using namespacestd;classteacher{ Public:    //declaring constructors, parameters given default valuesTeacher (stringName ="CJJ",intAge = A); //declaring copy ConstructorsTeacher (ConstTeacher &tea); //declare member functions and list all the member functions    voidSetName (string_name); stringGetName (); voidSetage (int_age); intgetage ();Private:    stringM_strname; intM_iage; };

Teacher.cpp

#include"Teacher.h"#include<iostream>using namespacestd;//define constructors, use initialization lists, initialize parameters of constructors//M_imax (m) is a constant and can only be initialized with an initialization listTeacher::teacher (stringNameintAge ): M_strname (name), M_iage (age) {cout<<"Teacher (String name,int age)"<<Endl;}//Defining copy ConstructorsTeacher::teacher (ConstTeacher &tea) {cout<<"teacher::teacher (const Teacher &tea)"<<Endl;}//out-of-class definitions, function bodies that write out member functionsvoidTeacher::setname (string_name) {M_strname=_name;}stringTeacher::getname () {returnM_strname;}voidTeacher::setage (int_age) {M_iage=_age;}intTeacher::getage () {returnm_iage;}

Operation Result:

C + + Constructor _ copy constructor

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.