c++重载函数定义和使用
来源:互联网 发布:python opencv pil 编辑:程序博客网 时间:2024/05/21 18:40
/************************************************************************/
#include <iostream.h>
#include <WINDOWS.H>
/************************************************************************/
/* 定义一个CLOCK类 */
/************************************************************************/
class
CLOCK
{
private
:
int
hour, minute, second;
public
:
CLOCK();
CLOCK(
int
,
int
,
int
);
void
update();
void
display();
};
/************************************************************************/
/* 定义CLOCK函数 */
/************************************************************************/
CLOCK::CLOCK()
{
hour = minute = second = 0;
cout <<
"\t One object initialalized."
<< endl;
}
/************************************************************************/
/* 定义CLOCK重载函数 */
/************************************************************************/
CLOCK::CLOCK(
int
h,
int
m,
int
s)
{
hour = h;
minute = m;
second = s;
cout <<
"\t An object initialalized."
<< endl;
}
/************************************************************************/
/* */
/************************************************************************/
void
CLOCK::update()
{
second++;
if
(second == 60)
{
second = 0;
minute ++;
}
if
(minute == 60)
{
minute = 0;
hour++;
}
if
(hour == 24)
{
hour = 0;
}
}
/************************************************************************/
/* */
/************************************************************************/
void
CLOCK::display()
{
cout << hour <<
":"
<< minute <<
":"
<< second <<endl;
}
/************************************************************************/
/* main函数 */
/************************************************************************/
void
main()
{
CLOCK MyClock(12, 0, 0);
//声明类名
while
(1)
{
MyClock.display();
MyClock.update();
Sleep(1000);
}
}
上面的是函数的重载,
下面是函数的隐藏和覆盖
#include <IOSTREAM.H>
int
a = 5;
int
&b = a;
/************************************************************************/
/* Base */
/************************************************************************/
class
Base
{
public
:
virtual
void
fn();
};
/*Derived类的fn(int)函数隐藏了Base类的fn函数*/
class
Derived:
public
Base
{
public
:
/*派生类的函数与基类的函数同名,但参数列表不同
在这种情况下,不管基类的函数声明是否有virtual
关键字,基类的函数都将被隐藏。*/
void
fn(
int
);
};
/*Derived2类的fn()函数隐藏了Derived类的fn(int)
*/
class
Derived2:
public
Derived
{
public
:
void
fn();
};
class
animal
{
public
:
void
eat()
{
cout <<
"animal eat"
<< endl;
}
void
sleep()
{
cout <<
"animal sleep"
<< endl;
}
void
breathe()
{
cout <<
"animal breathe"
<< endl;
}
};
class
fish:
public
animal
{
public
:
void
breathe()
{
cout <<
"fish bubble"
<< endl;
}
};
void
fn(animal *pAn)
{
pAn->breathe();
}
void
main()
{
animal *pAn;
fish fh;
pAn = &fh;
fn(pAn);
}
0 0
- c++重载函数定义和使用
- [C++]函数的重载、重写和重定义的区别
- xcode objcet c 函数定义和使用
- 定义函数的指针,C++和C不同是因为函数重载
- 类的定义和使用,集合的应用和构造函数,重载函数的应用!!
- 【C++】重载运算符和重载函数
- C++的重载函数定义和用法
- 虚函数的重载和重新定义
- C/C++]可变参函数的定义和使用【转】
- C++《析构函数的定义和使用》
- c/c++-虚函数和重载
- 构造函数的定义和构造函数的重载
- 171108—内联函数和重载函数&宏定义。
- C++(多态实现原理)函数重写,重载,重定义
- C++_函数的重载使用举例
- 【C++】复制构造函数(拷贝构造函数)和=运算符重载使用的区别
- 结构体的操作符重载和内部的函数定义及使用(结构体的进一步使用)
- C语言宏定义函数的使用(定义单行和多行)
- Spring-Boot学习(3)-----Spring-Boot整合mybatis
- linux内核SMP负载均衡浅析
- 542. 01 Matrix 题解
- Problem 1075 分解素因子
- HBA 卡和RAID 卡
- c++重载函数定义和使用
- Problem 1062 洗牌问题
- Linux 串口调试工具汇总
- 硬盘接口协议
- linux中fork()函数详解
- ARM基础系列——ARM指令集
- [NOIP]奖学金(1)
- The server encountered an internal error () that prevented it from fulfilling this request.h--模型驱动
- Problem 1059 老师的苦恼