孙鑫MFC2

1.继承

#include "stdafx.h"
#include "iostream"
using namespace std;


class Animal
{
public:
	void eat()
	{
		cout<<"animal eat"<<endl;
	}
	void sleep()
	{
		cout<<"animal sleep"<<endl;
	}
	void breath()
	{
		cout<<"animal breath"<<endl;
	}

};

class Fish : public Animal //集成Animal类中的public部分
{

};




int _tmain(int argc, _TCHAR* argv[])
{
	Animal an;
	an.eat();
	Fish fh;
	fh.sleep();

	return 0;
}

2.子类对父类的的访问权限

#include "stdafx.h"
#include "iostream"
using namespace std;


class Animal
{
public:
	void eat()
	{
		cout<<"animal eat"<<endl;
	}
protected:
	void sleep()
	{
		cout<<"animal sleep"<<endl;
	}
private:
	void breath()
	{
		cout<<"animal breath"<<endl;
	}

};

class Fish : public Animal //集成Animal类中的public部分
{
public:
	void test()//子类能访问protected & public,不能访问private
	{
		sleep();
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
	Animal an;
	an.eat();
	Fish fh;
	fh.test();//main中只能访问public
			//通过这种方式就可以访问到animal中的protected
	return 0;
}

3.子类对象定义要先构造父类

  子类先析构


#include "stdafx.h"
#include "iostream"
using namespace std;


class Animal
{
public:
	Animal()
	{
		cout<<"animal construct"<<endl;
	}
	~Animal()
	{
		cout<<"animal deconstruct"<<endl;
	}
	void eat()
	{
		cout<<"animal eat"<<endl;
	}
	void sleep()
	{
		cout<<"animal sleep"<<endl;
	}
	void breath()
	{
		cout<<"animal breath"<<endl;
	}

};

class Fish : public Animal //集成Animal类中的public部分
{
public:
	Fish()
	{
		cout<<"fish construct"<<endl;
	}
	~Fish()
	{
		cout<<"fish deconstruct"<<endl;
	}
	void test()//子类能访问protected & public,不能访问private
	{
		sleep();
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
//	Animal an;
//	an.eat();
	Fish fh;
//	fh.test();//main中只能访问public
			//通过这种方式就可以访问到animal中的protected
	return 0;
}

4.当父类没有合适的构造函数可供子类调用的解决办法

子类向父类传递参数的方法

构造函数不写,系统会自动添加。当构造函数我们写了,系统不会自动提供构造函数


class Animal
{
public:
	Animal(int height,int weight)
	{
		cout<<"animal construct"<<endl;
	}
	~Animal()
	{
		cout<<"animal deconstruct"<<endl;
	}
	void eat()
	{
		cout<<"animal eat"<<endl;
	}
	void sleep()
	{
		cout<<"animal sleep"<<endl;
	}
	void breath()
	{
		cout<<"animal breath"<<endl;
	}

};

class Fish : public Animal //集成Animal类中的public部分
{
public:
	//	Fish()//当Animal(int height,int weight),没有不带参数的构造函数,错误。
	//解决办法
	Fish() : Animal(400,300)
	{
		cout<<"fish construct"<<endl;
	}
	~Fish()
	{
		cout<<"fish deconstruct"<<endl;
	}
	void test()//子类能访问protected & public,不能访问private
	{
		sleep();
	}
};

5.子类如果有常量,初始化方法之一

class Fish : public Animal //集成Animal类中的public部分
{
public:
	//	Fish()//当Animal(int height,int weight),没有不带参数的构造函数,错误。
	//解决办法
	Fish() : Animal(400,300),a(1)//常量a的初始化,可以放在这里
	{
		cout<<"fish construct"<<endl;
	}
	~Fish()
	{
		cout<<"fish deconstruct"<<endl;
	}
protected:
	const int a;
};

6.函数的覆盖(父类子类之间)

class Animal
{
public:
	int a;
	Animal(int height,int weight){
//		cout<<"animal construct"<<endl;
	}
	~Animal(){
//		cout<<"animal deconstruct"<<endl;
	}
	void eat(){
		cout<<"animal eat"<<endl;
	}
	void sleep(){
		cout<<"animal sleep"<<endl;
	}
	void breath(){
		cout<<"animal breath"<<endl;
	}

};

class Fish : public Animal //集成Animal类中的public部分
{
public:
	//	Fish()//当Animal(int height,int weight),没有不带参数的构造函数,错误。
	//解决办法
	Fish() : Animal(400,300),a(1)//常量a的初始化,可以放在这里
	{
//		cout<<"fish construct"<<endl;
	}
	~Fish()
	{
//		cout<<"fish deconstruct"<<endl;
	}
	void breath()
	{
		cout<<"fish bubble"<<endl;
	}
protected:
	const int a;
};


int _tmain(int argc, _TCHAR* argv[])
{
//	Animal an;
//	an.eat();
	Fish fh;
	fh.breath();
	return 0;
}

6.作用域标识符(子类中希望调用父类的同名的函数)

class Fish : public Animal //集成Animal类中的public部分
{
public:
	//	Fish()//当Animal(int height,int weight),没有不带参数的构造函数,错误。
	//解决办法
	Fish() : Animal(400,300),a(1)//常量a的初始化,可以放在这里
	{
//		cout<<"fish construct"<<endl;
	}
	~Fish()
	{
//		cout<<"fish deconstruct"<<endl;
	}
	void breath()
	{
		Animal::breath();//  :: 作用域标识符
		cout<<"fish bubble"<<endl;
	}
protected:
	const int a;
};


7.子类和父类的首地址是一样的,所以显示的是父类的breath


#include "stdafx.h"
#include "iostream"
using namespace std;


class Animal
{
public:
	int a;
	Animal(int height,int weight){
	}
	~Animal(){
	}
	void eat(){
		cout<<"animal eat"<<endl;
	}
	void sleep(){
		cout<<"animal sleep"<<endl;
	}
	void breath(){
		cout<<"animal breath"<<endl;
	}

};

class Fish : public Animal //集成Animal类中的public部分
{
public:
	//	Fish()//当Animal(int height,int weight),没有不带参数的构造函数,错误。
	//解决办法
	Fish() : Animal(400,300),a(1)//常量a的初始化,可以放在这里
	{
	}
	~Fish()
	{
	}
	virtual void breath()
	{
		cout<<"fish bubble"<<endl;
	}
protected:
	const int a;
};

void fn(Animal *pAn)
{
	pAn->breath();
}

int _tmain(int argc, _TCHAR* argv[])
{
	Fish fh;
	Animal *pAn;
	pAn = &fh;
	fn(pAn);

	return 0;
}


多态性  根据地址决定调用的函数

	virtual void breath(){
		cout<<"animal breath"<<endl;
	}

父类是虚函数,编译器会根据对象类型,选择调用的函数。(迟绑定)

父类不是虚函数,根据地址,选择调用父类的函数

注释掉子类中的函数,就会调用父类的。


总结:多态性就是,父类的虚函数做子类的备胎。


如果,父类中的是纯虚函数,就不能用于实例化对象。要求子类中不能没有要调用的函数。

	virtual void breath() = 0;//纯虚函数
	/*{
		cout<<"animal breath"<<endl;
	}*/

8.引用

	int a = 6;
	int &b = a;//引用
b是a的别名,没有内存空间。改变a或b都会修改其2者共同对应的值。


9.防止重复编译头文件,在头文件中

#ifndef FISH_H  //使用这种语句防止重复编译头文件
#define FISH_H

#include "Animal.h"

class Fish : public Animal //集成Animal类中的public部分
{
public:
	Fish();
	void breath();
};

#endif

注意,在  .cpp中,#include "stdafx.h"//一定要出现在最前端

#include "stdafx.h"//一定要出现在最前端

#include "Fish.h"

#include "iostream"
using namespace std;

Fish:: Fish():Animal(300,400)
{}

void Fish:: breath()
{
	cout<<"fish bubble"<<endl;
}


10.this 指针

指向对象本身,代表对象的地址。

也就是说,在对象要调用成员函数时,this是隐含的,用于指示地址。

实现多个对象可以共同调用类中的共同的成员,使用this中指向的地址来分辨不同的对象。


对象调用 pt.output(10,10),成员函数获得两个形参,也获得了地址。即 this = &pt.


11.类的继承和访问特性



12.程序编译链接原理与过程




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值