掌握C++——C++基本概念回顾 孙鑫教程整理笔记

1.源程序

#include <iostream>
using namespace std;
class Animal
{
public:
	Animal()//无参构造函数(即默认构造函数)
	{
		cout << "animal construct" << endl;
		cout << "******************\n";
	}
	Animal(int height,int weight)//含参构造函数
	{
		cout << "animal construct with two parameters" << endl;
		cout << "******************\n";
	}
	~Animal()
	{
		cout << "animal deconstruct" << endl;
		cout << "******************\n";
	}
	void eat()
	{
		cout << "animal eat" << endl;
	}
	void breathe()
	{
		cout << "animal breathe" << endl;
	}
	virtual void run()//虚函数
	{
		cout << "animal run" << endl;
	}
	//virtual void sleep() = 0;//纯虚函数,含有纯虚函数的类称为抽象类
};

class Fish : public Animal	//继承
{
public:
	Fish():Animal(400,300),a(1)//子类无参构造函数向父类含参构造函数传递参数;子类常量初始化
	{
		cout << "fish construct" << endl;
		cout << "******************\n";
	}
	~Fish( )
	{
		cout << "fish deconstruct" << endl;
		cout << "******************\n";
	}
	void breathe()//函数的覆盖
	{
		Animal::breathe();//::是作用域标识符,即此处可以访问父类Animal的breathe()
		cout << "fish bubble" << endl;
	}
	void run()//函数的覆盖
	{
		cout << "fish run" << endl;
	}
	void sleep()
	{
		cout << "fish sleep" << endl;
	}
private:
		const int a;
};

void fn(Animal* pAn)//全局函数fn
{
	pAn->breathe();
}
void fn2(Animal* pAn2)//全局函数fn2
{
	pAn2->run();
}

void main()
{
	Animal an;
	an.eat();
	cout << "******************\n";
	Fish fh;
	fh.breathe();

	cout << "******************\n类型转换(内存模型要一致)\n";
	Animal* pAn;//指向父类Animal的指针pAn
	pAn = &fh;//把子类Fish的对象fh的地址传给pAn
	fn(pAn);//调用全局函数fn,从运行结果可以看出调用的是父类的breathe(),如果非要调用子类的函数呢,就要用到虚函数了,见下
	
	cout << "******************\nC++的多态性:(virtual)传递子类的地址,子类有的调用子类的,子类没有的再调用父类的\n";
	Animal* pAn2;
	pAn2 = &fh;
	fn2(pAn2);

	cout << "******************\n引用(变量的别名)\n";	
	int a = 6;
	cout << "a = " << a <<endl;
	int& b = a;//b是a的一个引用,并且必须在定义的时候就初始化
	b = 5;//当我改变b的值的时候,a的值也发生了改变
	cout << "a = " << a << endl;
}

2.运行结果

在这里插入图片描述

3.整理源程序

main.cpp

#include "Animal.h"
#include "Fish.h"
#include <iostream>
using namespace std;
void fn(Animal* pAn)//全局函数fn
{
	pAn->breathe();
}
void fn2(Animal* pAn2)//全局函数fn2
{
	pAn2->run();
}

void main()
{
	Animal an;
	an.eat();
	cout << "******************\n";
	Fish fh;
	fh.breathe();

	cout << "******************\n类型转换(内存模型要一致)\n";
	Animal* pAn;//指向父类Animal的指针pAn
	pAn = &fh;//把子类Fish的对象fh的地址传给pAn
	fn(pAn);//调用全局函数fn,从运行结果可以看出调用的是父类的breathe(),如果非要调用子类的函数呢,就要用到虚函数了,见下
	
	cout << "******************\nC++的多态性:(virtual)传递子类的地址,子类有的调用子类的,子类没有的再调用父类的\n";
	Animal* pAn2;
	pAn2 = &fh;
	fn2(pAn2);

	cout << "******************\n引用(变量的别名)\n";	
	int a = 6;
	cout << "a = " << a <<endl;
	int& b = a;//b是a的一个引用,并且必须在定义的时候就初始化
	b = 5;//当我改变b的值的时候,a的值也发生了改变
	cout << "a = " << a << endl;
}

animal.h

#ifndef ANIMAL_H_H
#define ANIMAL_H_H

class Animal
{
public:
	Animal();//无参构造函数(即默认构造函数)
	
	Animal(int height, int weight);//含参构造函数
	
	~Animal();
	
	void eat();
	
	void breathe();
	
	virtual void run();//虚函数
	
	//virtual void sleep();//纯虚函数,含有纯虚函数的类称为抽象类
};

#endif

animal.cpp

#include "Animal.h"
#include <iostream>
using namespace std;

Animal::Animal()//无参构造函数(即默认构造函数)
	{
		cout << "animal construct" << endl;
		cout << "******************\n";
	}
Animal::Animal(int height, int weight)//含参构造函数
	{
		cout << "animal construct with two parameters" << endl;
		cout << "******************\n";
	}
Animal::~Animal()
	{
		cout << "animal deconstruct" << endl;
		cout << "******************\n";
	}
void Animal::eat()
	{
		cout << "animal eat" << endl;
	}
void Animal::breathe()
	{
		cout << "animal breathe" << endl;
	}
void Animal::run()//虚函数  注意这里定义的时候已经加了virtual,所以此处不加了
	{
		cout << "animal run" << endl;
	}
//void Animal::sleep() = 0;//纯虚函数,含有纯虚函数的类称为抽象类

fish.h

#include "Animal.h"

#ifndef FISH_H_H
#define FISH_H_H

class Fish : public Animal	//继承
{
public:
	Fish();
	
	~Fish();
	
	void breathe();//函数的覆盖
	
	void run();//函数的覆盖
	
	void sleep();
	
private:
	const int a;
};

#endif

fish.cpp

#include "Fish.h"
#include <iostream>
using namespace std;

Fish::Fish() :Animal(400, 300), a(1)//子类无参构造函数向父类含参构造函数传递参数;子类常量初始化
	{
		cout << "fish construct" << endl;
		cout << "******************\n";
	}
Fish::~Fish()
	{
		cout << "fish deconstruct" << endl;
		cout << "******************\n";
	}
void Fish::breathe()//函数的覆盖
	{
		Animal::breathe();//::是作用域标识符,即此处可以访问父类Animal的breathe()
		cout << "fish bubble" << endl;
	}
void Fish::run()//函数的覆盖
	{
		cout << "fish run" << endl;
	}
void Fish::sleep()
	{
		cout << "fish sleep" << endl;
	}

4.课程链接

孙鑫

5.讲义

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值