C++学习(九)—类和对象(四)

new和delete运算符

#include<iostream>

using namespace std;

//  malloc 和 new区别
//	new和delete是一个运算符  malloc和free是一个库函数
//  malloc 返回值是 void *    new返回的是new出来的对象的指针
//  malloc 需要判断是否开辟成功  而new内部做好了操作(内部会malloc数据在堆区  判断内存是否分配成功  调用构造函数)
//  malloc 不会调用构造函数  而new调用构造函数
//  malloc 对应的是释放是  free    new对应的是释放是  delete

//  注意事项



class Person
{
public:
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}
	Person(int age)
	{
		cout << "Person的有参构造函数调用" << endl;
		m_age = age;
	}
	Person(const Person &p)
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_age = p.m_age;
	}

	~Person()
	{
		cout << "Person的析构函数调用" << endl;
	}



	int m_age;
};


void test01()
{
	//	Person p1; // 开辟到栈上

	//  开辟到堆区
	Person *p2 = new Person;
	Person *p3 = new Person(10);
	Person *p4 = new Person(*p3);

	//  释放new出来的对象
	delete p2;
	delete p3;
	delete p4;

}
void test02()
{
	//	不要用void * 去接受new出来的对象  原因是不能释放
	void * p = new Person;
	delete p;
}

//  利用new创建数组  在堆区创建数组  类中必须存在默认构造函数,否则无法创建

//  如果数组是在栈上开辟的,那麽可以指定利用哪个构造函数来初始化对象

//  如果是数组  释放的时候要在  delete后加[]

void test03()
{
	//	int* pint = new int[10];
	//	char* pchar = new char[64];
		Person* p = new Person[10];
	//	Person p[3] = { Person(10),Person(10),Person(10) };
		delete [] p;
}


int main()
{
	//  test01();
	//	test02();
	test03();
	system("pause");
	return 0;
}

静态成员

静态成员变量

#include<iostream>

using namespace std;

class Person
{
public:
	int a;
	//  1、数据共享  
	//	2、在编译阶段就分配了内存
	//	3、在类外声明、类外进行初始化
	static int b;
private:
	static int c;
};

int Person::b = 10;

void test01()
{
	//	1、通过对象进行访问
	Person p1;
	Person p2;
	Person p3;

	cout << "p1:b" << p1.b << endl;
	cout << "p2:b" << p2.b << endl;
	cout << "p3:b" << p3.b << endl;

	p3.b = 20;
	cout << "p1:b" << p1.b << endl;
	cout << "p2:b" << p2.b << endl;
	cout << "p3:b" << p3.b << endl;
	//	2、通过类名进行访问

	cout << "Person:b = " << Person::b << endl;

	//	静态成员变量也是有访问权限的

	//	Person::c = 10;
}



int main()
{
	test01();
	system("pause");
	return 0;
}

静态成员函数

#include<iostream>

using namespace std;

class Person
{
public:
	int a;
	//  1、数据共享  
	//	2、在编译阶段就分配了内存
	//	3、在类外声明、类外进行初始化
	static int b;

	static void f()
	{
		d = 100;
		cout << "静态成员函数调用" << endl;
	}

	static int d;
private:
	static int c;
};

int Person::b = 10;

void test01()
{
	//	1、通过对象进行访问
	Person p1;
	Person p2;
	Person p3;

	cout << "p1:b" << p1.b << endl;
	cout << "p2:b" << p2.b << endl;
	cout << "p3:b" << p3.b << endl;

	p3.b = 20;
	cout << "p1:b" << p1.b << endl;
	cout << "p2:b" << p2.b << endl;
	cout << "p3:b" << p3.b << endl;
	//	2、通过类名进行访问

	cout << "Person:b = " << Person::b << endl;

	//	静态成员变量也是有访问权限的

	//	Person::c = 10;
}


void test02()
{
	//  1、通过对象调用
	Person p1;
	p1.f();

	//	2、通过类名调用

	Person::f();

	//	3、静态成员函数是无法访问非静态成员变量的  因为无法区分变量归属于谁  但它可以访问静态成员变量  因为都是共享的

	//  4、非静态成员函数都可以访问

	//  5、静态成员函数也是有访问权限的
	
}


int main()
{
	//	test01();
	test02();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

walkerrev_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值