C++类和对象代码

无参构造函数

#include<iostream>
using namespace std;
class CStu
{
public:
	int age;
	float f;
	CStu()//构造函数:无返回值,且在创建对象的时候(CStu stu;)调用
	{
		age = 22;
		f = 12.12f;
	}
	void fun()//一般函数
	{
		age = 22;
		f = 12.12f;
	}
};
int main()
{
	CStu stu;//声明栈区
	//stu.age;
	//stu.f;
	//CStu*stu = new CStu;//声明堆区,指针,且必须new空间才行,也不会调用构造函数
	cout << stu.age << endl << stu.f << endl;
	system("pause");
	return 0;
}
//“stu”:“CStu *”与“CStu”的间接寻

有参构造函数

#include<iostream>
using namespace std;
class CStu
{
public:
	int age;
	float time;
	CStu(int a, float b = 24.3)//也可先默认参数赋值,但可以被改变
	{
		age = a;
		time = b;
	}

};
int main()
{
	CStu stu(,0);//构造函数参数传递
	CStu *stu1 = new CStu(56, 66.8f);
	cout << stu.age << ' ' << stu.time << endl;
	cout << stu1->age << ' ' << stu1->time << endl;


	system("pause");
	return 0;
}

成员函数定义

#include<iostream>
#include"head1.h"
using namespace std;//源文件类外定义
//一定加上类名作用域CStu::否则不识别
/*CStu::CStu()//无参构造函数
{
	age = 1234;
	time = 88.65f;
}*/
CStu::CStu(int a, float b)//有参构造函数,此处定义不可写默认参数值
{
	age = a;
	time = b;
}
int CStu::fun()//普通函数,注意类名作用域加在函数名fun之前!!!
{
	cout << age << endl;
	return 0;
}
int main()
{
	CStu stu;
	CStu*stu1 = new CStu(88,78.39f);
	cout << stu.age << ' ' << stu.time << ' ' << stu.fun() << endl;
	
	cout << stu1->age << ' ' << stu1->time<<' '<< stu1->fun() << endl;

	system("pause");
	return 0;
}

类的类型转化

#include<iostream>
using namespace std;
class A
{
public:
	int a;
	virtual void fun()
	{}
};
class B :public  A
{
	int b;
	void fun ()
	{}
};
class Cother
{

	int c;

};
int main()
{
	A *p;//父类
	B *s=0;//子类
	Cother * o=0;//其他类


	//p = static_cast<A*>(s);
	//o = static_cast<Cother*>(s);//“ = ”: 无法从“B * ”转换为“Cother *
	//o = (Cother*)s;强制类型转换不报错但不建议使用


	const A*q=0;
	A*q1 = const_cast<A*>(q);//const_cast只能转化为原来的类型
	//B*q1 = const_cast<A*>(q);//无法从“A *”转换为“B *”
	//基类型到派生类型的强制转换需要 dynamic_cast 或 static_cast
	//A*q1 = const_cast<B*>(q);     //	“const_cast” : 无法从“const A *”转换为“B *”	


	//dynamic_cast  父子类互相转化或者自己转自己
	p = dynamic_cast <A*>(s);//子类直接转父类
	s = dynamic_cast<B*>(p);//父类转子类必须是多态类型  即父类A中有virtual虚函数
	A*p1 = dynamic_cast<A*>(p);//自己转自己


	//reinterpret_cast<type>(expression)用于危险类型的转换 如long转short等
	//内存一定要够用,一般底层编程用,代码移植性差,C3老湿不建议使用
	
	struct dat
	{
		short a;
		short b;
	};
	long value =0xA224B118;
	dat* h = reinterpret_cast<dat*>(&value);
	cout<<hex<<h->a;
	
 	system("pause");
	return 0;
}
//注:新式转换比较智能,对于不合理的会有限制,老式的强制转换不会提示
//尽量不使用类型转换,一定要用就用新式的!

内部类

#include<iostream>
using namespace std;
class Ou
{
public:
	int a;
	Ou()
	{
		a = 122;

	}
public:
	class In
	{
		int b;
	public:
		In()
		{

			b = 88;

		}
		void fun()
		{
			//cout << a << endl; 非静态成员引用必须与特定对象相对	
			Ou s1;
			cout << s1.a << endl;

		}
	};
public:
	//外部类里创建一个内部类的对象
	In s2;
};
int main()
{
	Ou s3;
	s3.s2.fun();

	system("pause");
	return 0;
}

初始化列表

#include<iostream>
using namespace std;
//const常量和&引用只能初始化不能赋值!
class CStu
{
public:
	int a;
	float f;//声明顺序:a,f
	CStu(float c, int d ) :f(c), a(d)//初始化列表.成员初始化顺序只与声明的顺序有关,和初始化列表书写的顺序无关!
	{
		
	}
	void show()
	{
		cout << a<< f << endl;
	}
};
int main()
{
	CStu stu1(18.66f,12);
	stu1.show();
	cout << stu1.a << stu1.f << endl;
	system("pause");
	return 0;
}

友元函数

#include<iostream>
using namespace std;
class CStu
{
private:
	int age;
	void fun()
	{
		age = 12;
		cout << age << endl;
	}
	friend void fun1();//需要声明
	//友元函数:针对private或protected类型权限,类内成员对于这个函数相当于public
	friend int main();//不需要声明
	friend class CTeach;//友元类:不相干的类2只能通过友元类来调用类1函数
public:
	//接口函数
	int Get()
	{

		return age;
	}
	void Set()
	{
		age = 34;
	}
};
class CTeach
{
public:
	CStu stu2;
	void fun2()
	{
		stu2.fun();
	}
};
void fun1()
{
	CStu stu;
	stu.fun();
}
int main()
{
	CStu stu1;
	stu1.Set();
	int a = stu1.Get();
	cout << a << endl;
	stu1.fun();
	fun1();
	CTeach teach;
	teach.fun2();

	system("pause");
	return 0;
}

多态虚函数

#include<iostream>
using namespace std;
class father
{
public:
	virtual void show()
	{
		cout << "我是father\n";
		
	}


};
class son :public father
{
public:
	int a;
	virtual void show()//不写也默认virtual
	{

		cout << "我是son" << endl;
		
	}

};
class child :public son
{
public:
	int a;//virtual针对于函数成员,不针对变量成员
	 void show()
	{

		cout << "我是child" << endl;

	}

};

int main()
{
	father * p = new son;//父类指针指向子类空间调用子类函数  多态:父类指针可以有多种状态
	p->show();//父类show函数没有virtual的话调用父类show   有virtual的话调用子类show即虚函数的重写!

	father * s = new child;
	s->show();//三重继承的话父类virtual,子类也默认virtual调用孙子类show
	//虚函数函数名和参数列表以及返回值必须相同,否则无法满足以上调用顺序,按原来的方式调用
	
	child ch;
	father &q =  ch;//父类对象的引用也可以调用虚函数
	ch.show();
	q.show();
	
	
	system("pause");
	return 0;
}
/*注意:构造函数不能是虚函数而是内联函数!
内联inline函数不能是虚函数,也没有任何意义!
*/

协变


//虚函数函数名和参数列表以及返回值必须相同,否则无法满足以上调用顺序,按原来的方式调用
特殊情况
协变:返回值不同情况 重写虚函数返回类型有差异,且不是来自“Son::show”的协变
#include<iostream>
using namespace std;
class Father
{
public:
	virtual Father & show()
	{
		cout << "我是Father\n";
		return *this;
	}


};
class Son :public Father
{
public:
	int a;
	virtual Son & show()//不写也默认virtual
	{

		cout << "我是Son" << endl;
		return (*this);  //一般加上小括号
	}

};
int main()
{
	Father * x = new Son;
	x->show();
	return 0;
}

虚继承

#include<iostream>
using namespace std;
class A //父类A叫做虚基类
{ 

public:
	int a;


};
class B : virtual public A
{
public:

};
class C : virtual public A
{
public:

};
class D : public B,public C
{
public:

};
int main()
{
	D p;
	p.a;

	system("pause");
	return 0;
}
//虚继承用来解决多继承中访问不明确的问题

虚析构

#include<iostream>
using namespace std;
class Father
{
public:
	virtual ~Father()//多态中,如果释放父类指针,只会调用父类析构函数,加了虚析构就会子类父类都调用
	{
		cout << "father" << endl;

	}
	

};
class Son :public Father
{
public:
       ~Son()//隐式加上virtual
	{
		cout << "son" << endl;
	}
	
};
class WL
{
public:
	~WL()
	{
		cout << "WL" << endl;

	}

};
int main()
{
	/*Father *q = new Son;
	delete q;
	*/

	Father *z = new Son;
	delete z;
	//delete (WL*)z;//强制转化为WL类
	/*
delete特点
1.指针是谁的类型调用谁的析构函数!
2.申请的空间一定释放!对象不泄露,对象的成员申请的空间会被泄露

*/
	system("pause");
	return 0;

}

纯虚函数

#include<iostream>
using namespace std;
class Father
{
public:
	Father()
	{

	}
	virtual ~Father()
	{

	}
	virtual void fun() = 0;//有纯虚函数的类,必须用  子类重写  该纯虚函数,才能实例化对象!!!
	//有纯虚函数的类 (不全是)称为 抽象类  无法创建对象
	//全是纯虚函数的类(可以有构造函数)        称为 接口类

};
int main()
{
	//Father f; 不能实例化抽象类	纯虚函数

	system("pause");
	return 0;
}

取虚表的内容

#include<iostream>
using namespace std;
class Father
{
public:
	virtual void fun()
	{

		cout << "我是fun" << endl;
	}
	virtual  void show()
	{
		cout << "我是Father\n";

	}


};
class Son :public Father
{
public:
	
	 void show()//不写也默认virtual
	{

		cout << "我是Son" << endl;

	}

};
int main()
{
	Father * fa = new Son;
	fa->show();
	typedef void(*p)();//函数指针命名为p
	(p)(*((int *)*(int *)fa + 0));//强制转换为p类型 函数指针
	((p)(*((int *)*(int *)fa + 0)))   ();//后面+圆括号调用fun函数
	((p)(*((int *)*(int *)fa + 1)))   ();
	int *pp=(int*)*((int *)*(int *)fa + 2);
	system("pause");
	return 0;
	
}

运算符重载基础

#include<iostream>
using namespace std;
class CZ
{
public:
	int age;
	CZ(int nage)
	{
		 age= nage;
	}
	int operator +(int a)//类内 只写一个参数,并且默认类的对象在左边
	{
		return (this->age + a);
	}
	int operator >=(CZ&st2)
	{

		return (age >= st2.age);
	}
	int operator &(CZ&st2)
	{

		return (age & st2.age);
	}
	int operator -()//负号不是减号,一元运算符类内不用写参数
	{

		return (-age);
	}
	int operator !()//取反 age为0返回1,非0返回1 一元运算符类内不用写参数
	{

		return (!age);
	}
};
 int  operator - (CZ&st, int a)//类外 写两个参数
	{
	 return (st.age - a);

	}
 void operator <<(ostream&os, const CZ& st)//st不修改可顺便加上const,参数1是ostream引用。参数2是对象的常量引用
 {
	 os << st.age;
 }

int main()
{
	CZ st(51);
	CZ st2(66);
	int a = 10;
	cout << st + a << endl;
	cout << st - a << endl;
	cout << (st >= st2) << endl;//一定要加小括号()否则会报错
	cout << (st2 >= st) << endl;
	cout << (st&st2)<< endl;
	cout << -st<<endl;
	cout << !st << endl;
	//cout <<st << endl;
	system("pause");
	return 0;
}//不要滥用运算符重载,比如把+重载为-
发布了20 篇原创文章 · 获赞 26 · 访问量 3562

猜你喜欢

转载自blog.csdn.net/qq_42837890/article/details/104328925