1.拷贝浅拷贝
1.OOP书写类,能够有效表达一个实体的抽象类型
2.浅拷贝 拷贝构造和operator=, 构造 析构
3.对象生成 先分配内存,调用构造函数初始化对象的成员变量 =》 对象产生了
对象析构了 =》 对象就不存在了
4.对象的构造和对象的析构是相反的
类与类之间常用的关系:
组合 : a part of… 一部分
继承 : a kind of… 一种
组合
构造函数的初始化列表
1.函数调用传对象时,按对象引用来传递,会少两个函数
2.函数返回对象的时候,应该返回一个临时对象,不要先定义,再返回
3.调用返回对象的函数时,应该以初始化的方式调用,不要以赋值的方式调用
2.C/C++ const讲解
C89 C++99
const修饰的量一定要初始化
const定义的量叫做 常量
常量const的编译方式,在编译时期,拿常量的值把常量的名字替换掉
常量 =》 常变量
class CDate
{
public:
CDate(int y, int m, int d)
{
mYear = y;
mMonth = m;
mDay = d;
}
void show()
{
cout << mYear << "/" << mMonth << "/" << mDay << endl;
}
private:
int mYear;
int mMonth;
int mDay;
};
class CBook
{
public:
CBook(char *n, int a, double p, // int mAmount=a;
int y, int m, int d)
: mdate(y, m, d) // 初始化列表中给成员对象指定构造方式
, mAmount(a)
, mPrice(p) // 构造函数的初始化列表 > 构造函数体
{
if (n != NULL)
{
mpName = new char[strlen(n) + 1];
strcpy(mpName, n);
}
else
{
mpName = new char[1];
*mpName = 0;
}
//在当前构造函数体中,成员变量相当于已经定义过了
//mAmount = a; int mAmount; mAmount=a;
//mPrice = p;
}
~CBook()
{
delete[]mpName;
mpName = NULL;
}
void show()
{
cout << "name:" << mpName << endl;
cout << "amount:" << mAmount << endl;
cout << "price:" << mPrice << endl;
mdate.show();
}
private:
char *mpName;
double mPrice;
int mAmount;
CDate mdate; // 成员对象
CBook(const CBook&);
CBook& operator=(const CBook&);
};
int _tmain(int argc, _TCHAR* argv[])
{
CBook book1("30天精通C++", 200, 100.0, 2012, 10, 1);
book1.show();
return 0;
class Test
{
public:// Test() Test(a) Test(a, b)
Test(int a = 5, int b = 5) :ma(a), mb(b)
{ cout << "Test(int)" << this<<endl; }
~Test()
{ cout << "~Test()" << this<<endl; }
Test(const Test &src) :ma(src.ma), mb(src.mb)
{ cout << "Test(const Test&)" << endl; }
Test& operator=(const Test &src)
{
cout << "operator=" << endl;
ma = src.ma;
mb = src.mb;
return *this;
}
private:
int ma;
int mb;
};
Test t1(10, 10);
int main2()
{
Test t2(20, 20);
Test t3 = t2;
static Test t4 = Test(30, 30);
t2 = Test(40, 40);
t2 = (Test)(50, 50);
t2 = 60;
Test *p1 = new Test;
Test *p2 = new Test[2];
Test *p3 = &Test(70, 70);
Test &p4 = Test(80, 80);
delete p1;
delete[]p2;
return 0;
}
Test t5(90, 90);
int main()
{
int *p1 = new int[2];
int *p2 = new int[2]();
cout << p1[0] << " " << p1[1] << endl;
cout << p2[0] << " " << p2[1] << endl;
Test *p3 = new Test[2];
Test *p4 = new Test[2]();
// C++其实把一切类型都当作对象来处理
int a = int();
cout<<a<<endl;
return 0;
}
class Test
{
public:
Test(int data = 100) :ma(data)
{ cout << "Test(int)" << endl; }
~Test()
{ cout << "~Test()" << endl; }
Test(const Test &src) :ma(src.ma)
{ cout << "Test(const Test&)" << endl; }
Test& operator=(const Test &src)
{
cout << "operator=" << endl;
ma = src.ma;
return *this;
}
int getData(){ return ma; }
private:
int ma;
};
/*
1.函数调用传对象时,按对象引用来传递,会少两个函数
2.函数返回对象的时候,应该返回一个临时对象,不要先定义,再返回
3.调用返回对象的函数时,应该以初始化的方式调用,不要以赋值的方式调用
*/
Test GetTestObject(Test &t)
{
int value = t.getData();
//Test tmp(value);
//return tmp;
return Test(value);
}
int main()
{
Test t1;
Test t2 = GetTestObject(t1);
cout << t2.getData() << endl;
//Test t3(10);
//t3 = Test(10); // Test t4(10);
return 0;
}
3.构造 析构 拷贝构造 operator=函数
class Test
{
public:
Test(int data = 100) :ma(data)
{ cout << "Test(int)" << endl; }
~Test()
{ cout << "~Test()" << endl; }
Test(const Test &src) :ma(src.ma)
{ cout << "Test(const Test&)" << endl; }
Test& operator=(const Test &src)
{
cout << "operator=" << endl;
ma = src.ma;
return *this;
}
int getData(){ return ma; }
private:
int ma;
};