一、對象間的賦值ios
/***A.h文件***/ #pragma once class A { public: int va; A(void); A(char* name); A(const A& a); virtual void print(); char* name; void setVa(int va){ this->va = va; } public: ~A(void); };
/****A.cpp文件******/ #include "StdAfx.h" #include "A.h" #include <iostream> using namespace std; A::A(void) { cout<< "a constructor is invoked .." << endl; this->va = 5; } A::A(char* name) { this->name = name; cout<< "constructor of object of Class A, whose name is " << name << " , is invoked .." << endl; } A::~A(void) { cout<< "a[name is "<< name <<"] is destroy .." << endl; } A::A(const A& a){ //cout<< "a[name="<< this->name <<"] copy constructor is invoked.." << endl; cout<< "A copy constructor is invoked.." << endl; this->name = a.name; this->va = a.va; } void A::print(){ cout << "this is a" << endl; }
#pragma once #include "a.h" class B : public A { public: int va;//在這裏從新聲明瞭va變量 B(char* name4A); void print(); static int i; public: ~B(void); };
#include "StdAfx.h" #include "B.h" #include <iostream> using namespace std; B::B(char * name4A):A(name4A) { this->setVa(10); cout<< "b constructor is invoked .." << endl; this->va = 3; } B::~B(void) { cout<< "b is destroy .." << endl; } void B::print(){ cout << "this is B" << endl; } int B::i = -1;
// cplusplusprimer.cpp : 定義控制檯應用程序的入口點。 // #include "stdafx.h" #include "A.h" #include "B.h" #include <iostream> using namespace std; static int i; int j ; int _tmain(int argc, _TCHAR* argv[]) { A a("aSelf"); B b("aFromB"); //A a1("aSelf1"); cout<<"Hello C-Free!"<<endl; a.va = -1; b.va = -2; //a1.va = -3; cout<< "a.va=" <<a.va<<endl; cout<< "b.va=" <<b.va<<endl; cout<< "*************************************" <<endl; a= b; a.name = "aSelf"; //下面的這條命令 //在這裏你認爲輸出的值應該是什麼呢 //你是否是認爲輸出的應該是-2,但事實上呢輸出的是10,爲何呢?(問題1) cout<<"a.va="<<a.va<<endl; cout<<"b.va="<<b.va<<endl; a.print(); cout<< "*************************************" <<endl; /************************************************************************/ /* 通常來講,變量(無論是普通變量,仍是類成員變量)都須要初始化的,若是沒初始化就使用了,可能會出現未知的錯誤 /************************************************************************/ cout<<"i="<<i<<endl;//靜態變量系統會分配一個默認值給他 cout<<"j="<<j<<endl; /* int q; cout<<"j="<<q<<endl;//由於沒有進行初始化,因此這裏出錯了 */ cout<<"b.i="<<b.i<<endl; a.print(); return 0; }
關於以上面這段代碼中註釋裏面提出的問題1,你可否解答呢.函數
在C++,每一個類事實上都會默認對"="進行重寫this
operator=()
若是對一個類定義了兩個或多個對象,則這些同類的對象之間能夠互相賦值,或者說,一個對象的值能夠賦給另外一個同類的對象。這裏所指的對象的值是指對象中全部數據成員的值。
對象之間的賦值也是經過賦值運算符「=」進行的。原本,賦值運算符「=」只能用來對單個的變量賦值,如今被擴展爲兩個同類對象之間的賦值,這是經過對賦值運算符的重載實現的。
實際這個過程是經過成員複製來完成的,即將一個對象的成員值一一複製給另外一對象的對應成員。
對象賦值的通常形式爲
對象名1 = 對象名2;
注意對象名1和對象名2必須屬於同一個類。例如
Student stud1,stud2; //定義兩個同類的對象
┆
stud2=stud1; //將stud1賦給stud2
經過下面的例子能夠了解怎樣進行對象的賦值。
例9.9 對象的賦值。
#include <iostream>
using namespace std;
class Box
{
public :
Box(int =10,int =10,int =10); //聲明有默認參數的構造函數
int volume( );
private :
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box::volume( )
{
return (height*width*length); //返回體積
}
int main( )
{
Box box1(15,30,25),box2; //定義兩個對象box1和box2
cout<<″The volume of box1 is ″<<box1.volume( )<<endl;
box2=box1; //將box1的值賦給box2
cout<<″The volume of box2 is ″<<box2.volume( )<<endl; return 0;
}
運行結果以下:
The volume of box1 is 11250
The volume of box2 is 11250
說明:
(1) 對象的賦值只對其中的數據成員賦值,而不對成員函數賦值。
(2) 類的數據成員中不能包括動態分配的數據,不然在賦值時可能出現嚴重後果。
注:
一、若是是子類對象給父類對象賦值,那麼子類對象只會把從父類那邊繼承過來的數據成員賦值給父類對象成員
的數據成員。
二、若是子類重寫了父類的數據成員,例如該數據成員爲member,那麼子類對象將擁有兩份member數據成員
這也解釋了問題1那裏輸出的是10,而不是-2。當子類對象賦值給父類對象時,對於被重寫了的數據成員如何賦值給父類對象對應的數據成員.也就是子類對象要選擇哪一個數據成員給相對應的父類數據成員呢? 答案是:子類對象在構造時它同時也會構造一個父類對象x,這些對象x所對應的數據成員member將被賦值給對應的父類對象的數據成員。