构造函数、析构函数、继承

构造函数和析构函数`

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

class cir{
public :
	cir(){
		cout << "构造函数"<<endl;
	}
	cir(int a){
		age = a;
		cout << "构造函数" << endl;
	}

	~cir()
	{
		
		cout << "析构函数调用" << endl;
	}
	//拷贝构造函数
	cir(const cir &p)
	{
     age = p.age;
	 cout << "kabei" << endl;
	}
	int age;
};
void dowork(cir p)
{

}
void test2()
{
	cir p;
	dowork(p);
}
	void test()
	{
		cir p;//默认构造函数调用
		cir p2(10);
		cir p3(p2);//掉哟个拷贝构造函数
		cout << "p2的年龄为:" << p2.age << endl;
	}
int main()
{
	test();
	system("pause");
}

初始化列表

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

class cir{
public :
	cir(int a,int b,int c):A(a),B(b),C(c)//初始化列表
	{
	

		
	}
	int A;
	int B;
	int C;
};

	void test()
	{
		cir p(20,30,10);//默认构造函数调用
		cout << "A:" << p.A << endl;
		cout << "B:" << p.B << endl;
		cout << "C:" << p.C << endl;
	}
int main()
{
	test();
	system("pause");
}

//当其它类作为本类对象,构造先构造类对象,在构造自身,析构相反

#include<iostream>
#include <string>
using namespace std;
//当其它类作为本类对象,构造先构造类对象,在构造自身,析构相反
class Phone{
public:
	Phone(string pName){
		m_pName = pName;
		cout << "Phone的构造函数";
	}
	string m_pName;
};
class person{
public :
	person(string name, string pName) :Name(name), phone(pName)
	{
		cout << "person的构造函数";
	}

	string Name;
	Phone phone;
};
void test()
{
	person p("zhagsan", "pingguo3");
	cout << p.Name<<endl;
	cout << p.phone.m_pName;
}
int main()
{
	test();
	system("pause");
}

静态成员函数和静态成员变量

#include<iostream>
#include <string>
using namespace std;
//1 所有对象都共享同一份数据
//2编译阶段就分配内存
//3类内声明,类外初始化操作

//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量

class person{
public :
	static int A;
	int B;
	static void func()
	{
		A = 10;          //静态成员函数不可以访问非静态成员变量
		
		cout << "静态成员函数";
	}

};
int person::A = 100;
void test()
{
	person p;
	cout << p.A << endl; 
	p.func();//通过对象访问

	//通过类名访问
	person::func();
	
}
int main()
{
	test();
	system("pause");
}
//空对象占用一个内存
//C++编译器会给每个空对象也分配一一个字节空间, 是为了区分空对象占内存的位置
// 每个空对象也应该有一个独一无二的内存地址二
#include<iostream>
#include <string>
using namespace std;
//成员变量和成员函数分开存储的

class person{
public :
	 int A;
	 static int b;

};
int person::b = 0;
void test()
{
	//空对象占用一个内存
	//C++编译器会给每个空对象也分配一一个字节空间, 是为了区分空对象占内存的位置
	// 每个空对象也应该有一个独一无二的内存地址二

	person p;
	cout << sizeof(p) << endl;

	
	
}
int main()
{
	test();
	system("pause");
}

//this指针的用途:
//●当形参和成员变星同名时,可用this指针来区分
//●在类的非静态成员函数中返回对象本身,可使用return *this
//this指针指向被调用的成员函数所属的对象

#include<iostream>
#include <string>
using namespace std;
//this指针的用途:
//●当形参和成员变星同名时,可用this指针来区分
//●在类的非静态成员函数中返回对象本身,可使用return *this
//this指针指向被调用的成员函数所属的对象


class person{
public :
	 int A;
	 person(int a)
	 {
		 this->A = a;

	 }
	 person& personself(person p){//引用
		 this->A += p.A;
		 return *this;//返回对象本身

	 }

};

void test()
{


	person p(10);
	cout << p.A;
	
	person p2(10);
	p2.personself(p).personself(p).personself(p);
	cout << "p2.A=" << p2.A << endl;
}
int main()
{
	test();
	system("pause");
}

const修饰成员函数
常函数: .
●成员函数后加const后我们称为这个函数为常函数
●常函数内不可以修改成员属性
●成员属性声明时加关键字mutable后,在常函数中依然可以修改
I
常对象:
●声明对象前加const称该对象为常对象
●常对象只能调用常函数

//友元
//友元的目的就是让一个函数或者类访问另-个类中私有成员
//友元的三种实现
//全局函数做友元

#include<iostream>
#include <string>
using namespace std;
//友元
//友元的目的就是让一个函数或者类访问另-个类中私有成员
//友元的三种实现
//全局函数做友元
//类做友元
//成员函数做友元

class Building{
	
	friend void goodGay(Building *building);
public :
	Building(){
		m_SittingRoom = "客厅";
		m_bedZRoom = "卧室";
	}
public:
	string m_SittingRoom;
private:
	string m_bedZRoom;
};

//全局函数
void goodGay(Building *building)
{
	cout << "好基友全局函数 正在访问:" << building->m_SittingRoom << endl;
	cout << "好基友全局函数 正在访问:" << building->m_bedZRoom << endl;
}
void test()
{
	Building building;
	goodGay(&building);
	
}
int main()
{
	test();
	system("pause");
}

//类做友元

#include<iostream>
#include <string>
using namespace std;
//友元
//友元的目的就是让一个函数或者类访问另-个类中私有成员
//友元的三种实现
//全局函数做友元
//类做友元
//成员函数做友元
class Building;
class GoodGay{
public:
	GoodGay();
	void vist();
private:
	Building *builing;

};

class Building{
	friend class GoodGay;
public:
	
	Building();
public:
	string m_SittingRoom;
private:
	string m_bedZRoom;
};

Building::Building(){
	this->m_bedZRoom="卧室";
	this->m_SittingRoom = "客厅";
}

GoodGay::GoodGay(){
	 builing = new Building;
}
void GoodGay::vist()
 {
	cout << "好基友全局函数 正在访问:" << builing->m_SittingRoom << endl;
	cout << "好基友全局函数 正在访问:" << builing->m_bedZRoom << endl;
 }
void test()
{
	GoodGay gg;
	gg.vist();
	
}
int main()
{
	test();
	system("pause");
}

//成员函数做友元

#include<iostream>
#include <string>
using namespace std;
//友元
//友元的目的就是让一个函数或者类访问另-个类中私有成员
//友元的三种实现
//全局函数做友元
//类做友元
//成员函数做友元
class Building;
class GoodGay{
public:
	GoodGay();
	void vist();
	void vist2();
	Building *builing;

};

class Building{
	friend void GoodGay::vist();
public:
	
	Building();
public:
	string m_SittingRoom;
private:
	string m_bedZRoom;
};
//类外实现成员函数
Building::Building(){
	m_bedZRoom="卧室";
	m_SittingRoom = "客厅";
}

GoodGay::GoodGay(){
	 builing = new Building;
}
void GoodGay::vist()
 {
	cout << "vist好基友全局函数 正在访问:" << builing->m_SittingRoom << endl;
	cout << "好基友全局函数 正在访问:" << builing->m_bedZRoom << endl;
 }
void GoodGay::vist2()
{
	cout << "vist2好基友全局函数 正在访问:" << builing->m_SittingRoom << endl;
	//cout << "好基友全局函数 正在访问:" << builing->m_bedZRoom << endl;
}
void test()
{
	GoodGay gg;
	gg.vist();
	gg.vist2();
}
int main()
{
	test();
	system("pause");
}

继承
公共继承成公共
保护继承父类继承来的成为保护
私有继承父类继承过来成为私有

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

class Base{
public:
	void header(){
		cout << "首页、公开课、注册.....(公共头)" << endl;
	}
	void footer(){
		cout << "帮助中心、交流和作、站内地图...(公共底部)" << endl;
	}
	void lelf()
	{
		cout << "Java、python...(公共分类列表)" << endl;
	}

};
class Java :public Base{
public:
	void content()
	{
		cout << "java视频"<<endl;
	}
};

class CPP :public Base{
public:
	void content()
	{
		cout << "CPP视频"<<endl;
	}
};
void test()
{
	Java ja;
	ja.header();
	ja.footer();
	ja.lelf();
	ja.content();
	CPP cpp;
	cpp.header();
	cpp.header();
	cpp.footer();
	cpp.lelf();
	cpp.content();

}
int main()
{
	test();
	system("pause");
}

//继承中构造和析构顺序
//子类继承父类后,当创建子类对象,也会调用父类的构造函数
//问题 : 父类和子类的构造和析构顺序是谁先谁后 ?
//狗造先父再子析构相反

#include<iostream>
#include <string>
using namespace std;
//继承中构造和析构顺序
//子类继承父类后,当创建子类对象,也会调用父类的构造函数
//问题 : 父类和子类的构造和析构顺序是谁先谁后 ?
//狗造先父再子析构相反
class Base{
public:
	Base(){
		cout << "Base构造函数";
	}
	~Base(){
		cout << "Base析构函数";
	}
	int a;
protected:
	int b;
private:
	int c;
};
class Java :public Base{
public:
	Java(){
		cout << "java构造函数";
	}
	~Java(){
		cout << "Java析构函数";
	}
	int d;
};

void test()
{
	Java ja;
	
}
int main()
{
	test();
	system("pause");
}

子类父类同名变量和函数

#include<iostream>
#include <string>
using namespace std;
//继承同名成员处理方式
//问题:当子类与父类出现同名的成员,如何通过子类对象, 访问到子类或父类中同名的数据呢 ?
//   访问子类同名成员直接访问即可
//   访问父类同名成员需要加作用域

class Base{
public:
	Base(){
		a = 100;
	}
	void func(){
		cout << "base函数";
	}
	int a;
protected:
	int b;
private:
	int c;
};

class Java :public Base{
public:
	Java(){
		a = 200;
	}
	void func(){
		cout << "son函数";
	}
	int d;
};

void test()
{
	Java ja;
	//cout << ja.a<< endl;
	//cout << "base 下的" << ja.Base::a << endl;
	ja.func();
	ja.Base::func();
}
int main()
{
	test();
	system("pause");
}
#include<iostream>
#include <string>
using namespace std;
//继承同名成员处理方式
//问题:当子类与父类出现同名的成员,如何通过子类对象, 访问到子类或父类中同名的数据呢 ?
//   访问子类同名成员直接访问即可
//   访问父类同名成员需要加作用域

class Base{
public:


	static int a;
protected:
	int b;
private:
	int c;
};

class Java :public Base{
public:
	static int a;
};
int Java::a = 10;
int Base::a = 20;
void test()
{
	Java j;
	
	cout << j.a << " 父类 " <<j.Base::a;
	cout << Java::Base::a;
}
int main()
{
	test();
	system("pause");
}

多继承

class Java :public Base,public fahter{}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值