2021-08-05 面向对象 友元函数 友元类 内部类 内存分布 以及c语言中动态内存管理方式

// 面向对象 c++类和对象 内存管理3-7.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//

#include

int main()
{
std::cout << “Hello World!\n”;
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

//类和对象 友元函数的连续输出。
//#include
//using namespace std;
//class Date
//{
// friend ostream& operator<<(ostream& _cout, const Date& d);
//public:
// Date(int year, int month, int day)
// :_year(year)
// , _month(month)
// , _day(day)
// {}
//private:
// int _year;
// int _month;
// int _day;
//
//
//};
//ostream& operator<<(ostream& _cout, const Date& d)
//{
// _cout << d._year << “-” << d._month << " " << d._day;
// return _cout;
//}
//void test()
//{
// Date d(2021, 3, 7);
// //简写形式
// cout << d;
// //完整形式
// operator<<(cout, d);
// //连续输出的简写形式
// cout << d << endl;
// //连续输出的完整形式
// operator<<(cout, d)<<endl;
// //对象的连续输出。
// cout << d << d;
// //连续输出,就是返回类型就是输出流的引用。
// operator<<(operator<<(cout, d), d);
//
// //operator(cout,endl);
// cout.operator<<(endl);
// cout << endl;
//}
//int main()
//{
// test();
// return 0;
//}

运算符输入,cin 自定义成员的输入运算符的操作
//#include
//using namespace std;
//class Date
//{
// friend ostream& operator<<(ostream& _cout, const Date& d);
// friend istream& operator>>(istream& _cin, const Date& d);
//public:
// Date(int year, int month, int day)
// :_year(year)
// , _month(month)
// , _day(day)
// {}
//private:
// int _year;
// int _month;
// int _day;
//
//
//};
//ostream& operator<<(ostream& _cout, const Date& d)
//{
// _cout << d._year << “-” << d._month << " " << d._day;
// return _cout;
//}
//istream& operator>>(istream& _cin, Date& d)
//{
// _cin >> d._year >> d._month >> d._day;
// return _cin;
//}
//void test()
//{
// Date d(2021, 3, 1);
// cin >> d;
// //连续输入
// cin >> d >> d >> d;
//
//}
//int main()
//{
// test();
// return 0;
//}

//友元类的操作
//#include
//using namespace std;
//class Date; // 前置声明
//class Time
//{
// friend class Date; // 声明日期类为时间类的友元类,则在日期类中就直接访问Time类中的私有成员变量
//public:
// Time(int hour, int minute, int second)
// : _hour(hour)
// , _minute(minute)
// , _second(second)
// {}
//
//private:
// int _hour;
// int _minute;
// int _second;
//};
//
//class Date
//{
//public:
// Date(int year = 1900, int month = 1, int day = 1)
// : _year(year)
// , _month(month)
// , _day(day)
// {}
// void SetTimeOfDate(int hour, int minute, int second)
// {
// // 直接访问时间类私有的成员变量
// _t._hour = hour;
// _t._minute = minute;
// _t._second = second;
// }
//
//private:
// int _year;
// int _month;
// int _day;
// Time _t;
//};
//

//内部类 作用是类似于友元类 可以通过外部类的访问权限,限制内部类的可见范围
//定义的位置会影响内部类的可见范围。
//public:作用域;外部可见
//private:作用域;外部不可加
//内部类是独立于外部类,不从属于外部类。
//外部类不是内部类的友元类。 友元关系也是单向的。
//相比于友元类 在内部中是可以直接访问外部类的static成员。
//#include
//using namespace std;
//class A
//{
// //友元类的声明
// friend class C;
// //内部类
// class B
// {
// public:
// void setA(A& a)
// {
// a._a = _b;
// }
// private:
// int _b = 2;
// };
//private:
// int _a = 1;
// static int _sa;
//};
//
//int A::_sa = 10;
友元类
//class C
//{
//public:
// void setA(A& a)
// {
// a._a = _c;
// }
//private:
// int _c = 3;
//};
//
//void test()
//{
//
//
//}

//习题讲解
//1.求1 + 2 + 3 + … + n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A ?
//B : COJ链接(课堂讲解)
//#include
//using namespace std;
//class Solution
//{
//public:
// //使用内部类
// class Sum
// {
// public:
// //使用构造函数间接完成一个累加的操作。
// Sum()
// {
// //直接访问外部类
// _sum += _i;
// ++_i;
// }
// };
//
// int Sum_Solution(int n)
// {
// //1.方法 return n * (n + 1) / 2;
// 方法2/int sum = 0;
// //for (int i = 1; i <= n; ++i)
// // sum += 1;
// //return sum;
/
// //重置
// _sum = 0;
// _i = 0;
// Sum array[n];
// return _sum;
// }
//private:
// static int _i;
// static int _sum;
//};
//int Solution::_i = 1;
//int Solution::_sum = 0;

//封装
//相关的数据以及行为定义在一起

//内存管理
//存储的位置 和sizeof和strlen的区别
//sizeof表示的是实际类型的大小 strlen关注的是反斜杠的位置,不包含反斜杠“/”=求有效字符的个数。

#include
using namespace std;
void test()
{
//申请空间
char* ptr = (char*)malloc(0x7fffffff);
char* ptr2 = (char*)malloc(0x7fffffff);
}
void test()
{
char* ptr = (char*)malloc(sizeof(char));
//调整空间大小
char* ptr2 = (char*)malloc(sizeof(char));
//申请新的空间,功能和malloc相同
char* ptr3 = (char*)realloc(NULL, sizeof(char));
//会根据需求判断是否要进行空间的释放。 所以不用显示的去释放传入的空间,他会在空间内部进行自己去释放空间。
//只需要去显式relloc返回的空间
char* ptr4 = (char*)realloc(ptr3, sizeof(char)*100);

char* ptr5 = (char*)realloc(NULL, sizeof(char));
char* ptr6 = (char*)realloc(ptr5, sizeof(char) * 100);


//这里会申请空间并且是全0. 按照字节来申请空间。
char* ptr7 = ((char*)calloc(4, sizeof(char)));


//空间的释放
//ptr指向的空间不能显式释放   因为可能两者是指向同一个位置。

//free(ptr)
free(ptr2);
free(ptr3);
free(ptr4);

}

int main()
{
test();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值