0
点赞
收藏
分享

微信扫一扫

C++经典问题_08 函数重载和函数重写

仲秋花似锦 2022-03-31 阅读 104

文章目录

一. 函数重载的定义

二. 函数重载的条件

三. 函数重载的例子

void func(int a); // 1
void func(double a); // 2
void func(int a,int b); // 3
void func(int a,double b); // 4
bool func(int a,double b); // 5
int func(int a); // 6

1和2构成重载,参数列表不同,一个是int,一个是double.
1和6不构成重载,仅仅是参数列表不同
2和6构成重载
3和4构成重载,参数列表不同
3和5构成重载,参数列表和返回值都不同
4和5不构成重载,仅仅是返回值不同

类的构造函数就是一个很好的函数重载的例子

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
	Person()
	{
		mName = "无名氏";
		mAge = 1000;
		mSex = "人妖";
		mAddress = "天堂";
		cout << "Person的无参构造被调用" << endl;
	}
	Person(string name):mName(name),mAge(1000),mSex("男"),mAddress("不知道")
	{
		cout << "Person() 的有参构造被调用参数是(string name)" << endl;
	}
	Person(string name, int age, string sex) :mName(name), mAge(age), mSex(sex)
	{
		cout << "Person() 的有参构造被调用参数是(string name,int age,string sex)" << endl;
	}
	Person(string name, int age, string sex, string address) :mName(name), mAge(age), mSex(sex),mAddress(address)
	{
		cout << "Person() 的全参构造函数被调用" << endl;
	}

private:
	string mName;
	int mAge;
	string mSex;
	string mAddress;
};
int main()
{
	// 调用无参
	Person p;
	// 调用一个参数的构造函数
	Person p1("张三");
	// 调用三个参数的构造函数
	Person p2("李四", 18, "男");
	// 调用四个参数的构造函数
	Person p3("王五", 25, "男", "中国!");

	system("pause");
	return 0;
}

结果 :

四. 函数重写的定义

五. 函数重写的条件

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
class A {}; // 类A的声明
class B :public A {};// 类B的声明
class Base
{
public:
	virtual A &show()
	{
		cout << "Base::show()被调用!" << endl;
		return *(new A);
	}
};

class Derived :public Base
{
public:
	// 返回值是B类型,B类型是A类型的派生类型
	B &show()
	{
		cout << "Derived::show()被调用!" << endl;
		return *(new B);
	}
};

int main()
{
	Base b;
	Derived d;
	Base *p = &b;
	p->show(); // 调用的是Base()类中的show()
	p = &d;
	p->show(); // 调用的是Derived()类中的show()

	system("pause");
	return 0;
}

结果:

六. 函数的重定义

  • 基本条件
  • 功能
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
class Base
{
public:
	void print_info()
	{
		cout << "Base:: print_info() 被调用!" << endl;
	}
	virtual void print_info_2(void)
	{
		cout << "Base:: 虚函数print_info_2() 被调用!" << endl;
	}
};

class Derived :public Base
{
public:
	void print_info()
	{
		cout << "Derived::print_info() 被调用!" << endl;
	}
	virtual void print_info_2(int a) // 不会发生重写,因为参数不同
	{
		cout << "Base:: 虚函数print_info_2() 被调用!" << endl;
	}
};


int main()
{
   // 首先是定义两个对象
	Base b;
	Derived d;
	Base *p; // Base类型的指针
	p = &b;  // 指向了b
	p->print_info(); // 调用的Base里面的函数
	p->print_info_2(); // 调用的Base里面的函数

	p = &d;
	p->print_info(); // 指向改变了,但是依旧调用的是Base::里面的函数
	p->print_info_2(); // 指向改变了,但是依旧调用的Base::里面的函数

	system("pause");
	return 0;
}

结果:

举报

相关推荐

0 条评论