C + + Construction and destruction-10

Source: Internet
Author: User

One. Initialization of the object

1. All objects in general need a definite initial state

2. Solution

A. Provides a public initialize function for each class-even read

B. Immediately after the object is created, call the Initialize function to initialize

#include <stdio.h>classtest{Private:       inti;  Public:       voidInitialize () {i=0; }            intGet1 () {returni; }};intMain () {Test T1;    Test T2;        Test T3;    T1.initialize ();    T2.initialize ();        T3.initialize (); printf ("ti.i =%d\n", T1.get1 ()); printf ("ti.i =%d\n", T1.get1 ()); printf ("ti.i =%d\n", T1.get1 ()); printf ("Press any key to continue");    GetChar (); return 0; }

3.initialize is just a normal function that must show the call

4. Once the object is not initialized due to a mistake, the result will be indeterminate

An object with no initialization, the value of its internal member variable is constant.

Two. Constructors in C + +

1. A class in C + + can define a special member function that is the same as the class name.

2. This member function, which is the same as the class name, is called a constructor

3. Constructors can have parameters when they are defined, but there is no declaration of any return type

4. Calls to constructors

A. In general, the C + + compiler automatically calls constructors

B. Functions that need to be called manually in some cases

#include <stdio.h>classtest{Private:       inti; intJ; intK;  Public: Test (intv) {i= j = k =v; }        voidprint () {printf ("i =%d,j =%d,k =%d\n", i,j,k); }};intMain () {Test T1 (4); Test T2=5; Test T3= Test (6);;    T1.print ();    T2.print ();        T3.print (); Test ta[3] = {Test (1), Test (2), Test (3)};  for(intI=0;i<3; i++) {ta[i].print (); } printf ("Press any key to continue");    GetChar (); return 0; }

5. Two special constructors

#include <stdio.h>/*
Attention:
1. When no constructors are defined in a class, the C + + compiler provides a parameterless constructor and copy constructor
2. When any non-copy constructor is defined in a class, the C + + compiler does not provide us with an parameterless constructor
*/classtest{Private: inti; intJ; intK; Public: Test (Consttest&v) {printf ("Test (const test& v) \ n"); } Test () {printf ("Test () \ n"); } };intMain () {Test T1; Test T2=T1; printf ("Press any key to continue"); GetChar (); return 0; }

A. Non-parametric constructors

When a constructor is not defined in a class, the compiler provides an parameterless constructor by default, and its function body is empty

B. Copy Constructors

When a copy constructor is not defined in a class, the compiler provides a copy constructor by default, which simply replicates the value of the member variable

Three. Array class

Array.h

#ifndef _array_h_#define_array_h_classarray{Private:   intmlength; int*Mspace; Public: Array (intlength); Array (Constarray&obj); intlength (); voidSetData (intIndexintvalue); intGetData (intindex); voiddestory (); };#endif

Array.c

#include"Array.h"Array::array (intLength//Array Creation{    if(Length <0) {length=0; } mlength=length; Mspace=New int[mlength];} Array::array (Constarray&obj) {Mlength=obj.mlength; Mspace=New int[Mlength];  for(intI=0; i<mlength; i++) {Mspace[i]=Obj.mspace[i]; } }intArray::length ()//Array Size{    returnmlength; }voidArray::setdata (intIndexintValue//Setting Array Values{Mspace[index]=value; }intArray::getdata (intIndex//Get Array Values{    returnMspace[index];}voidArray::d estory ()//Destroying Space{mlength= -1; Delete[] mspace; }

Main.c

#include <stdio.h>#include"Array.h"intMain () {Array A1 (Ten);  for(intI=0; I<a1.length (); i++) {a1.setdata (i,i); }         for(intI=0; I<a1.length (); i++) {printf ("element%d:%d\n", I,a1.getdata (i)); } Array A2=A1;  for(intI=0; I<a2.length (); i++) {printf ("element1%d:%d\n", I,a2.getdata (i));    } a1.destory ();        A2.destory (); printf ("Press any key to continue");    GetChar (); return 0; }

Output results

  

C + + Construction and destruction-10

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.