C++的重载函数定义和用法
来源:互联网 发布:永恒纪元戒官网通知 编辑:程序博客网 时间:2024/05/01 13:15
/************************************************************************/
#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);
}
0 0
- C++的重载函数定义和用法
- [C++]函数的重载、重写和重定义的区别
- 虚函数的重载和重新定义
- 定义函数的指针,C++和C不同是因为函数重载
- 构造函数的定义和构造函数的重载
- 【C语言用法】C语言的函数“重载”
- C++_重载、重写和重定义的区别
- C++_重载、重写和重定义的区别
- indexof函数的定义和用法
- php similar_text()函数的定义和用法
- C宏定义#和##的用法
- C语言:宏定义的#和##用法
- 【C++】函数的重载
- 【c++】函数的重载
- c++ STL Map 用法 小于号重载和仿函数定义
- 【C++】重载运算符和重载函数
- c++重载函数定义和使用
- 类的定义和使用,集合的应用和构造函数,重载函数的应用!!
- linux入门-菜鸟级操作1
- 【android】巧用android:divider属性设置LinearLayout中元素之间的间隔
- linux升级mysql到5.7
- HDFS源码解析(一)
- .NET程序在windows操作系统上独立运行的技术要点
- C++的重载函数定义和用法
- 手机短号
- poj 2528 Mayor's posters (线段树+区间离散)
- Python mysqldb 操作mysql出现中文乱码问题
- android调试时apk可运行,导出签名的apk后闪退,报找不到class
- 【Android】Eclipse换成舒服的黑色背景
- Gradle 入门
- Eclipse各个版本号名字的由来
- 小白github教程