夯实基础,学习笔记(cpp类和对象)

/*类和对象
构造和析构 函数
构造函数的调用
构造函数分类:
	(有参、(默认构造)无参)
	普通、拷贝
封装
对象的特性
	成员变量和成员函数分开存储

深拷贝:在堆区重新申请空间,进行拷贝操作
浅拷贝:简单的赋值拷贝操作
		浅拷贝带来的问题:堆区内存重复释放。可以使用深拷贝来解决

初始化列表
类对象作为类成员(构造时先构造类对象,再构造自身,析构时,和构造顺序相反)

静态成员:
	静态成员函数只能访问静态成员变量
	不属于某一个对象的

*/

#include<iostream>
#include<string>
using namespace std;


class student{

public:
	student(int a) {

	}
	int age;

};

class per {

public:
	//构造函数
	per() {
		cout << "构造函数的调用" << endl;
	}
	per(int a) {
		this->a = a;
		cout << "有参构造" << endl;
	}
	per(int a, int * p) {
		this->a = a;
		this->p = p;
	}
	per(const per& p) {
		cout << "拷贝构造" << endl;
		//深拷贝
		this->p = new int(*p.p);
	}

	//初始化列表
	per(int a,int b,int c) :a(a),b(b),c(c) {//初始化列表

	}


	//析构函数
	~per() {
		//将堆区的数据释放干净
		if (p != NULL) {
			delete p;
			p = NULL;
		}
		cout << "析构函数 的调用" << endl;
	}

	int a ;
	int b ;
	int c ;
	static int d ;
	int* p;

	int getb() {
		cout << b << endl;
		return b;
	}

	static void staticfun(static int  d) {
		
		cout << d << endl;
		cout << "static 函数调用" << endl;
	}

private:

protected:

};//class per
int per::d = 10;

class stu : per {

};

bool same(per &p1, per &p2) {
	if (p1.a == p2.a) {
		return true;
	}
	else return false;
}

//调用
void test() {
	per p1(11,22,33);
	cout << p1.a << endl;
	cout << p1.b << endl;
	cout << p1.c << endl;
}
void test01() {
	//通过对象调用静态函数
	per p;
	p.staticfun(1);
	//通过类名访问
	per::staticfun(1);


 }

int main() {
	
	
	//per p1;
	//per p3;
	//stu s1 ;
	// 
	// 
	//test();
	test01();

	//p1.getb();
	
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Loafer_W

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

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

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

打赏作者

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

抵扣说明:

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

余额充值