C++的重载函数定义和用法
2014-12-06 11:25
204 查看
/************************************************************************/
#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);
}
#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);
}
相关文章推荐
- C++重载函数定义和用法
- 关于C/C++中typedef的定义与用法总结
- 浅谈C++中指针和引用的区别 指针和引用在C++中很常用,但是对于它们之间的区别很多初学者都不是太熟悉,下面来谈谈他们2者之间的区别和用法。 1.指针和引用的定义和性质区别: (1)指针:指针是一
- C++模板用法:定义、使用 示例
- 关于C++中typedef的用法定义函数指针
- 从Java到C++——常量值的定义和基本用法
- 从Java到C++——常量值的定义和基本用法
- 关于C/C++中typedef的定义与用法总结
- c++ STL Map 用法 小于号重载和仿函数定义
- C/C++ struct 结构体定义 用法详解
- 从Java到C++——常量值的定义和基本用法
- c++重载函数定义和使用
- C++的声明和定义及extern用法
- C++中结构体的声明和定义的用法
- C++中的聚合类定义与用法分析
- C++中宏的定义与用法(现已被内联函数所代替)
- 变量的声明与定义以及关键字extern的用法
- [C++]实验五:掌握类的声明和对象的定义
- [原创] 一劳永逸:关于C/C++中指针、数组与函数复合定义形式的直观解释
- static定义变量的特殊用法和static定义变量的优先级展示。