C++重写、重载、重定义、名称覆盖

代码如下:
parent: --------------------------------------------------child:
void print()--------------------------------------------void print() //重定义
void print(int i,int j) //和父类中的print()重载
void show() --------------------------------------------void show(int i) //名称覆盖
virtual int get()-----------------------------------------int get() //重写

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;

namespace ccc {
	class Parent {
	public:
		void print() {
			cout << this->a << endl;
			cout << this->b << endl;
		}
		void print(int i, int j) {  //和print()进行类内重载
			cout << i << j << endl;
		}

		void show() {
			cout << "这是父类" << endl;
		}

		virtual int get() {
			return 0;
		}
	
		int a;
		int b;
	};
	class Child :public Parent {
	public:
		void print() { //重定义
			cout << this->a << this->b << this->c << endl;
		}
		void show(int i) {
			cout << "这是子类" << endl;//名称覆盖,这个show和父类中show名称一样,函数参数不一样,发生了名称覆盖,并不会重载
		}

		int get() {    //重写,get和父类中get相同,但是virtual 关键字,所以是重写。
			return 1;
		}

	private:
		int c;
	};
}
using namespace ccc;
void main() {
	Child c;
	c.show();//不可调用,正确调用方法c.Parent::show();

}

1、子类和父类中的函数名相同,函数参数相同,没有virtual关键字,重定义
2、子类和父类中的函数名相同,函数参数相同,有virtual关键字, 重写
3、子类和父类中的函数名相同,函数参数不同,名称覆盖,不会发生重载
4、重载只在子类或者父类中进行,不会跨类。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值