c++没有给出预期的输出
C++ is not Giving Expected output
我是it专业的学生,在我们的课本中有很多简单继承的程序,但是这些都是很长的实用程序,所以很难理解和记住,所以我试图用c++编写一个简单的继承程序,但是程序没有给出预期的输出。
I've use c++ for windows 7
代码如下:
#include"iostream.h"
#include"conio.h"
class A
{
public:
int a,b;
void setData(int i,int j)
{
a=i;
b=j;
}
};
class B: public A
{
public:
int compare()
{
return(a>b?a:b);
}
};
void main()
{
A a;
B b;
//int c;
clrscr();
a.setData(25,9);
cout<<"answerswer: "<<b.compare();
getch();
}
输出如下
answer : 1213
所以请帮助我N告诉我为什么输出是这样!!!!我只是想学习
您在A
类对象上使用setData
,而在B
类对象上调用compare
。在这两种情况下都使用b
int main()
{
A a;
B b;
//int c;
clrscr();
b.setData(25,9);
cout<<"answerswer: "<<b.compare();
getch();
return 0;
}
也改变main
方法的签名
a
和b
是不同的对象,变量a,b
存储在每个对象的内存空间中。您初始化了a
的成员,并试图与对象b
进行比较
So plz help me N tell me Why the Output is like this?
你得到的答案都是垃圾值
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词