» 首页 » 电脑_数码 » 编程 » 函数重载的问题

函数重载的问题

哪位帮忙测试下,给个结果

class A
{
public:
func() { cout << 'A' << endl;}
}

class B : public A
{
public:
func(char *p) { cout << p << endl;}
}

class C :public A
{
public:
func() { cout << 'C' << endl; }
func(char *p) { cout << p << endl; }
}

main()
{
A a, *pa;
B b;
C c;

a.func();
b.func();
c.func();

b.func("BB");
c.func("CC");

pa = &b;
pa->func();
pa->func("BBB");

pa = &c;
pa->func();
pa->func("CCC");
}


visual studio 2005测试
1>------ 已启动生成: 项目: 0000000000, 配置: Debug Win32 ------
1>正在编译...
1>template.cpp
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2236: 意外的“class”“B”。是否忘记了“;”?
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2143: 语法错误 : 缺少“;”(在“:”的前面)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2059: 语法错误 : “:”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “cout”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “endl”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2059: 语法错误 : “public”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(21) : error C3874: “main”的返回类型应为“int”而非“C”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “B”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2146: 语法错误 : 缺少“;”(在标识符“b”的前面)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “b”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(27) : error C2228: “.func”的左边必须有类/结构/联合
1> 类型是“'unknown-type'”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(30) : error C2228: “.func”的左边必须有类/结构/联合
1> 类型是“'unknown-type'”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(35) : error C2660: “A::func”: 函数不接受 1 个参数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(39) : error C2660: “A::func”: 函数不接受 1 个参数
1>生成日志保存在“file://e:\ooooooooooo\c++{answer}00000000\Debug\BuildLog.htm”
1>0000000000 - 19 个错误,3 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
=========================================================

#include<iostream>
using namespace std;

class A
{
public:
void func() { cout << "A" << endl;}
};

class B : public A
{
public:
void func(char *p) { cout << p << endl;}
};

class C : public A
{
public:
void func() { cout << "C" << endl; }
void func(char *p) { cout << p << endl; }
} ;

void main()
{
A a;
A *pa = NULL;
B b;
C c;

char *p = "abd";

a.func();
b.func(p);
c.func();

b.func("BB");
c.func("CC");

pa = &b;
pa->func();
pa->func();

pa = &c;
pa->func();
pa->func();

system("pause");
}

=============================================
1。
#include<iostream>
using namespace std;
2。
类的定义以“;”结束
3。
定义函数要有返回值,没有返回值可用void
func()==>>void func()
4。
使用“cout” 出现问题
5。
若要在dos下看到效果则在代码后加“system("pause");”

visual studio 2005测试
1>------ 已启动生成: 项目: 0000000000, 配置: Debug Win32 ------
1>正在编译...
1>template.cpp
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2236: 意外的“class”“B”。是否忘记了“;”?
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2143: 语法错误 : 缺少“;”(在“:”的前面)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2059: 语法错误 : “:”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “cout”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “endl”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2059: 语法错误 : “public”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(21) : error C3874: “main”的返回类型应为“int”而非“C”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “B”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2146: 语法错误 : 缺少“;”(在标识符“b”的前面)
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “b”: 未声明的标识符
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(27) : error C2228: “.func”的左边必须有类/结构/联合
1> 类型是“'unknown-type'”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(30) : error C2228: “.func”的左边必须有类/结构/联合
1> 类型是“'unknown-type'”
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(35) : error C2660: “A::func”: 函数不接受 1 个参数
1>e:\ooooooooooo\c++{answer}00000000\template.cpp(39) : error C2660: “A::func”: 函数不接受 1 个参数
1>生成日志保存在“file://e:\ooooooooooo\c++{answer}00000000\Debug\BuildLog.htm”
1>0000000000 - 19 个错误,3 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
=========================================================

#include<iostream>
using namespace std;

class A
{
public:
void func() { cout << "A" << endl;}
};

class B : public A
{
public:
void func(char *p) { cout << p << endl;}
};

class C : public A
{
public:
void func() { cout << "C" << endl; }
void func(char *p) { cout << p << endl; }
} ;

void main()
{
A a;
A *pa = NULL;
B b;
C c;

char *p = "abd";

a.func();
b.func(p);
c.func();

b.func("BB");
c.func("CC");

pa = &b;
pa->func();
pa->func();

pa = &c;
pa->func();
pa->func();

system("pause");
}

=============================================
1。
#include<iostream>
using namespace std;
2。
类的定义以“;”结束
3。
定义函数要有返回值,没有返回值可用void
func()==>>void func()
4。
使用“cout” 出现问题
5。
若要在dos下看到效果则在代码后加“system("pause");”

 相关问题
·函数重载的问题
·怎样在EXCEL2007里面做函数输出不同的答案,字体颜色不一样
·求一道VF编程...我们的作业
·什么是开放性城市,它是怎么定义?
·3d 在web中用什么技术可以实现
·有没有什么代码可以让网页横着动的?
·执行下面的程序段后,*(ptr+5)的值为??
·数学建模 对计算机有那些要求,特别是C++方面的。
·php5 session保存对象
·mysql+php的时候,有时候为什么php会找不到mysql
·用DM清0后数据恢复100分!
·编写一个程序,要求输入一个5位数.把该数分解成单独的数位...
·举几个keil c 与proteus环境下联合仿真实例
·VS2005中使用<iframe src=\"Login.aspx\"></iframe>
·vc程序员最低需要什么条件才能工作?薪水不要多,只要保证...

 《函数重载的问题》答案收集时间:2008-06-14 14:37:02



©2007 电脑技术问答录