您的位置:首页 > 编程语言 > C语言/C++

【C++】【学习笔记】【019】覆盖方法和重载方法

2014-11-11 21:11 453 查看
覆盖方法
1、当我们需要在基类里提供一个通用的函数,但在它的某个子类里需要修改这个方法的实现,在C++里,覆盖(overriding)就可以做到。简言之,覆盖方法就是共性里面寻找特性;
2、覆盖的实现:在子类里重新声明这个方法,然后再改写一下它的实现代码就行了,就像是一个新增加的方法一样;
3、子类里的方法,如果和基类里的方法名字一样,不管参数是否一样,都会把基类里的所有同样名字的方法覆盖的;

样例:为Animal添加eat()方法,并在Pig和Turtle中进行覆盖;

#include <iostream>
#include <string>

// 动物基类
class Animal
{
public:
std::string mouth;
void eat();
void sleep();
void drool(); // 流口水
};

void Animal::eat()
{
std::cout << "Animal: I'm eating!" << std::endl;
}

void Animal::sleep()
{
std::cout << "Animal: I'm sleeping!" << std::endl;
}

void Animal::drool()
{
std::cout << "Animal: I'm drooling!" << std::endl;
}

// 猪子类
class Pig : public Animal
{
public:
void climb();
void eat();
};

void Pig::climb()
{
std::cout << "Pig: I'm climbing!" << std::endl;
}

// 覆盖基类的eat方法
void Pig::eat()
{
Animal::eat();
std::cout << "Pig: I'm eating!" << std::endl;
}

// 乌龟子类
class Turtle : public Animal
{
public:
void swim();
void eat();
};

void Turtle::swim()
{
std::cout << "Turtle: I'm swimming!" << std::endl;
}

void Turtle::eat()
{
Animal::eat();
std::cout << "Turtle: I'm eating!" << std::endl;
}

int main()
{
Pig pig;
Turtle turtle;

pig.eat();
turtle.eat();

pig.climb();
turtle.swim();

return 0;
}
输出:
Animal: I'm eating!
Pig: I'm eating!
Animal: I'm eating!
Turtle: I'm eating!
Pig: I'm climbing!
Turtle: I'm swimming!


重载方法
1、重载机制:使得可以定义多个同名的方法(函数),只是他们的输入参数必须不同。编译器是依靠不同的输入参数来区分不同的方法;
2、重载并不是一个真正的面向对象特性,它只是可以简化编程工作的捷径,而简化编程工作正是C++的全部追求;
注意
1、对方法(函数)进行重载一定要有的放矢,重载的方法(函数)越多,程序就越不容易看懂;
2、对方法进行覆盖(注意区分覆盖和重载)时一定要看仔细,因为只要声明的函数输入参数和返回值与原来的不一致,则该方法将是一个重载方法而不是覆盖方法。这种错误很难调试;
3、对从基类继承来的方法进行重载,程序永远不会像预期的那样工作(继承之后尝试的重载本质上是覆盖);

样例:为Animal添加eat(int eatCount)方法,进行重载;

#include <iostream>
#include <string>

// 动物基类
class Animal
{
public:
std::string mouth;
void eat();
void eat(int eatCount);
void sleep();
void drool(); // 流口水
};

void Animal::eat()
{
std::cout << "Animal: I'm eating!" << std::endl;
}

void Animal::eat(int eatCount)
{
std::cout << "Animal: I'm eating " << eatCount << " foods." << std::endl;
}

void Animal::sleep()
{
std::cout << "Animal: I'm sleeping!" << std::endl;
}

void Animal::drool()
{
std::cout << "Animal: I'm drooling!" << std::endl;
}

// 猪子类
class Pig : public Animal
{
public:
void climb();
};

void Pig::climb()
{
std::cout << "Pig: I'm climbing!" << std::endl;
}

// 乌龟子类
class Turtle : public Animal
{
public:
void swim();
};

void Turtle::swim()
{
std::cout << "Turtle: I'm swimming!" << std::endl;
}

int main()
{
Pig pig;
Turtle turtle;

pig.eat();
turtle.eat();

pig.eat(2);

pig.climb();
turtle.swim();

return 0;
}
输出:
Animal: I'm eating!
Animal: I'm eating!
Animal: I'm eating 2 foods.
Pig: I'm climbing!
Turtle: I'm swimming!


样例:为Pig从Animal继承来的方法eat()进行重载(本质是覆盖);

#include <iostream>
#include <string>

// 动物基类
class Animal
{
public:
std::string mouth;
void eat();
void sleep();
void drool(); // 流口水
};

void Animal::eat()
{
std::cout << "Animal: I'm eating!" << std::endl;
}

void Animal::sleep()
{
std::cout << "Animal: I'm sleeping!" << std::endl;
}

void Animal::drool()
{
std::cout << "Animal: I'm drooling!" << std::endl;
}

// 猪子类
class Pig : public Animal
{
public:
void eat(int eatCount);
void climb();
};

void Pig::eat(int eatCount)
{
std::cout << "Pig: I'm eating " << eatCount << " foods." << std::endl;
}

void Pig::climb()
{
std::cout << "Pig: I'm climbing!" << std::endl;
}

// 乌龟子类
class Turtle : public Animal
{
public:
void swim();
};

void Turtle::swim()
{
std::cout << "Turtle: I'm swimming!" << std::endl;
}

int main()
{
Pig pig;
Turtle turtle;

pig.eat(); <span style="color:#ff0000;">// 这里编译出错!!</span>
pig.eat(2);
turtle.eat();

pig.climb();
turtle.swim();

return 0;
}
||=== Build: Debug in test_cpp (compiler: GNU GCC Compiler) ===|
D:\13....SVNLib\01.Exercise\03.CPP\test_cpp\main.cpp||In function 'int main()':|
D:\13....SVNLib\01.Exercise\03.CPP\test_cpp\main.cpp|65|error: no matching function for call to 'Pig::eat()'|
D:\13....SVNLib\01.Exercise\03.CPP\test_cpp\main.cpp|65|note: candidate is:|
D:\13....SVNLib\01.Exercise\03.CPP\test_cpp\main.cpp|37|note: void Pig::eat(int)|
D:\13....SVNLib\01.Exercise\03.CPP\test_cpp\main.cpp|37|note: candidate expects 1 argument, 0 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: