Skip to content

Commit

Permalink
更改
Browse files Browse the repository at this point in the history
  • Loading branch information
YiGOffer committed Sep 13, 2021
1 parent babd559 commit f36ad29
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Goodgay::Goodgay()
void Goodgay::visit()
{
cout << "好基友正在访问 " << this->m_building->sittingRoom<<endl;
cout << "好基友正在访问 " << this->m_building->bedRoom << endl;
cout << "好基友正在访问啊" << this->m_building->bedRoom << endl;
}

int main()
Expand Down
71 changes: 71 additions & 0 deletions 类和对象/对象特性—深拷贝和浅拷贝.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
///*********************深拷贝(有在堆区开辟空间)、浅拷贝************************/
class Person
{
public:
Person()
{
cout << "无参构造函数调用" << endl;
}

Person(int age,int height)
{
m_height = new int(height);
m_age = age;
cout << "有参构造函数调用" << endl;
}



/******************************************/
//为什么要传入引用即Person &p,因为如果main代码中Person p3(p2)相当于Person p3=p2,调用了拷贝构造函数,就相当于Person p=p2 又调用了拷贝构造函数即Person p=p2 无限递归出错
如果这些知识你都能理解。下面就来解释一下为什么值传递会无限递归!
如果复制构造函数是这样的 :
test(test t);
我们调用
test ort;
test a(ort); --> test.a(test t=ort)==test.a(test t(ort))
-->test.a(test t(test t = ort))
==test.a(test t(test t(ort)))
-->test.a(test t(test t(test t=ort)))
/******************************************/


Person(const Person &p)
{
m_age = p.m_age;
//m_height = p.m_height; //编译器默认实现的拷贝 代码
//深拷贝操作
m_height = new int(*p.m_height);
cout << "拷贝函数调用" << endl;
}
~Person()
{
if(m_height!=NULL)
{
delete m_height;
m_height = NULL;
}
cout << "析构函数调用" << endl;

}
public:
int m_age;
int *m_height;
};

void test01()
{
Person p1;
Person p2(10,160);
cout << "P2的年龄" <<p2.m_age<<" p2的身高"<<*p2.m_height<< endl;
Person p3(p2);
cout << "P3的年龄" << p3.m_age << " p3的身高" << *p3.m_height << endl;

}
int main()
{
test01();
}



31 changes: 31 additions & 0 deletions 类和对象/类的封装案例1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "stdafx.h"
using namespace std;
#include<iostream>
#include<string>
const double PI = 3.14;
class circle
{

//访问权限
public://公共权限


//属性,即参数
int m_r;
int m_r1;

//行为
//获取圆的周长
double calculateZC()
{
return 2 * PI*m_r;
}
};

int main()
{
circle c1;
c1.m_r = 10;
cout << "圆的周长是:" << c1.calculateZC() << endl;
return 0;
}

0 comments on commit f36ad29

Please sign in to comment.