C++的构造函数属性初始化_静态成员_this指针

//构造函数的属性初始化列表

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<iostream>
#include<stdarg.h>

using namespace std;

class Teacher {
private:
	char *name;
public :
	Teacher(char *name) {
		this->name = name;
		cout << "Teacher有参构造函数" << endl;
	}
	~Teacher() {
		cout << "Teacher析构函数" << endl;
	}
	char* getName() {
		return this->name;
	}
};

class Student {
private:
	int id;
	//属性对象
	//Teacher t = Teacher("miss cang");
	Teacher t1;
	Teacher t2;
public:
	Student(int id,char *t1_n,char *t2_n) :t1(t1_n),t2(t2_n) {
		this->id = id;
		cout << "Student有参构造函数" << endl;
	}
	void myprint() {
		cout << id <<","<<t1.getName()<<","<<t2.getName()<< endl;
	}
	~Student(){
		cout << "Student析构函数" << endl;
	}
};

void func() {
	Student s1(10,"miss bo","mrs liu");
	Student s2(10, "miss zhang", "jason");
	s1.myprint();
	s2.myprint();
}

void main() {
	func();
	getchar();
}

//C++ 通过new(delete)动态内存分配
//C malloc (free)

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<iostream>
#include<stdarg.h>

using namespace std;

class Teacher {
private:
	char* name;
public:
	Teacher(char* name) {
		this->name = name;
		cout << "Teacher构造参数" << endl;
	}
	Teacher() {
		cout << "Teacher析构参数" << endl;
	}
	void setName(char* name) {
		this->name = name;
	}
	char* getName() {
		return this->name;
	}
};


void func() {
	//C++
	//会调用构造和析构函数
	Teacher *t1 = new Teacher("jack");
	cout << t1->getName() << endl;
	//释放
	delete t1;
	
	//C
	//Teacher *t2 = (Teacher*)malloc(sizeof(int)*10);
	//t2->setName("jack");
	//free(t2);
}

void main() {
	func();
	//数组类型
	//C
	//int *p1 = (int*)malloc(sizeof(int)*10);
	//p1[0] = 9;
	//free(p1);

	//C++
	int *p2 = new int[10];
	p2[0] = 2;
	//释放数组[]
	delete[] p2;

	getchar();
}

//static 静态属性和方法

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<iostream>
#include<stdarg.h>

using namespace std;

class Teacher {
public :
	char* name;
	//计数器
	static int total;
public :
	Teacher(char *name) {
		this->name = name;
		cout << "Teacher的构造函数"<< endl;
	}
	~Teacher() {
		cout << "Teacher的析构函数" << endl;
	}
	void setName(char *name) {
		this->name = name;
	}
	char* getName() {
		return this->name;
	}
	static void count() {
		total++;
		cout << total << endl;
	}
};

//静态属性初始化赋值
int Teacher::total = 9;
void main() {
	Teacher::total++;
	cout << Teacher::total << endl;
	//直接通过类名访问
	Teacher::count();
	//也可以通过对象名访问
	Teacher t1("yuehang");
	t1.count();
	getchar();
}

//类的大小

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<iostream>
#include<stdarg.h>

using namespace std;

class A {
public:
	int i;
	int k;
	int j;
	static int m;
};

class B {
public :
	int i;
	int j;
	int k;
	void myprint() {
		cout << "打印" << endl;
	}
};

void main() {
	cout << sizeof(A) << endl;
	cout << sizeof(B) << endl;
	// C/C++ 内存分区:栈 堆 全局(静态  全局) 常量区(字符串) 程序代码区
	//普通属性与结构体相同的内存布局

	//在java中 jvm stack (基本数据类型,对象的引用)
	//Native Method stack (本地方法栈)
	//方法区

	getchar();
}

//this ,当前对象的指针
//函数是共享的,必须要有能够识别当前对象是谁的方法

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<iostream>
#include<stdarg.h>

using namespace std;

class Teacher {
private:
	char *name;
	int age;
public :
	Teacher(char *name,int age) {
		this->name = name;
		this->age = age;
		cout << "Teacher的构造函数" << endl;
	}
	~Teacher() {
		cout <<	"Teacher的析构函数" << endl;
	}
	//常函数,修饰的是this
	//既不能改变指针的值,又不能改变指针指向的内容
	//const Teacher* const this
	void myprintf() const {
		printf("%#x\n",this);
		//不能改变属性的值
		//this->name = "jason";
		//不能改变this指针的值
		//this = (Teacher*)0x00009;
		cout << this->name << "," << this->age << endl;
	}
	void myprintf2() {
		cout << this->name << "," << this->age << endl;
	}
};

void main() {
	Teacher t1("jack",20);
	const Teacher t2("rose",22);
	//常量对象只能调用常量函数,不能调用非常量函数
	t2.myprintf();
	//常函数,当前对象不能被修改,防止数据成员被非法访问
	printf("%#x\n",&t1);
	t1.myprintf();
	printf("%#x\n",&t2);
	t2.myprintf();
	getchar();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值