C++继承未返回正确的值
C++ inheritance not returning the correct values
#include<iostream.h>
#include<conio.h>
class base
{
public:
int a;
int b;
void read();
void display();
protected:
int c;
};
void base :: read()
{
cout<<"nEnter values of a , b and c :";
cin>>a>>b>>c;
}
void base :: display()
{
cout<<"nValues of a,b and c :"<<a<<" "<<b<<" "<<c;
}
class derived : public base
{
int x;
public:
int y;
void read();
void display();
protected:
int z;
};
void derived :: read()
{
x=a;
y=b;
z=c;
}
void derived :: display()
{
cout<<"nValue of A :"<<a<<" "<<x;
cout<<"nValue of B :"<<b<<" "<<y;
cout<<"nValue of C :"<<c<<" "<<z;
}
void main()
{
clrscr();
base b;
cout<<"nBase Class :";
b.read();
b.display();
derived d;
cout<<"nDerived Class :";
d.read();
d.display();
getch();
}
输入和输出:基类 :输入 a、b 和 c 的值:1 2 3
a ,b 和 c 的值:1 2 3
派生类:A 的值 : 1342 1342B 的值 : -14 -14C的值:11330 11330
我想使用此代码,以便我可以轻松理解私有,公共和受保护数据的继承...
请更正此代码,以便我可以真正了解c ++继承或者请回复解释C ++继承的代码..
当你实例化d
时,你不初始化基类的数据成员。它们的值是不确定的(不会对默认初始化的内置类型或 POD 执行初始化(,并且从中读取实际上是未定义的行为。
必须确保在读取基类数据成员之前对其进行初始化。有很多方法可以做到这一点,具体取决于您的需求。至少,你可以通过在构造函数中初始化它们来确保它们被初始化为零:
class base
{
public:
base() : a(), b(), c() {} // default constructor
...
这将导致你调用d.read()
设置d.x
、d.y
、d.z
0
,因为它所做的只是分别从a
、b
和c
进行分配。
生类的方法不会自动调用基类的相同方法 - 它替换基实现,不会添加到基实现中。如果你也想使用基类实现,你必须自己做。
void derived :: read()
{
base::read();
x=a;
y=b;
z=c;
}
当您创建 Derived
的实例时,它不会获取您从控制台分配给 Base
实例的a
b
和c
的值:它是另一个独立的对象!
void derived :: read() {
x=a;
y=b;
z=c;
}
这实际上将垃圾放入x
y
和z
。您可以在derived::read()
中调用base::read()
来初始化a
b
并c
我猜你试图做如下的事情:
#include<iostream.h>
#include<conio.h>
class base
{
public:
int a;
int b;
virtual void read();
virtual void display();
protected:
int c;
};
void base :: read()
{
cout<<"nEnter values of a , b and c :";
cin>>a>>b>>c;
}
void base :: display()
{
cout<<"nValues of a,b and c :"<<a<<" "<<b<<" "<<c;
}
class derived : public base
{
int x;
public:
int y;
virtual void read();
virtual void display();
protected:
int z;
};
void derived :: read()
{
base::read()
x=a;
y=b;
z=c;
}
void derived :: display()
{
base::display()
cout<<"nValue of A :"<<a<<" "<<x;
cout<<"nValue of B :"<<b<<" "<<y;
cout<<"nValue of C :"<<c<<" "<<z;
}
void main()
{
clrscr();
base b;
cout<<"nBase Class :";
b.read();
b.display();
derived d;
cout<<"nDerived Class :";
d.read();
d.display();
getch();
}
请注意,要使用多态性,您应该使用 virtual 关键字,以免完全屏蔽函数,但是,这在指针和将派生类型的对象分配给某个基指针的情况下是相关的。
另请注意,您实现读取的方式实际上会将垃圾放入 xyz,因为 abc 不会通过基本对象的读取函数读取,因为没有人调用它。因此,这就是为什么您在那里获得随机值而不是在控制台中输入的值的原因。
我建议阅读一些关于 c++ 多态性的信息。我发现下面的资源是一个很好的教程:http://www.cplusplus.com/doc/tutorial/polymorphism/
我希望这有所帮助。
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 如何继承返回对当前对象的引用的方法?
- 返回自身内部的双重嵌套类的成员函数,该类继承自从自身继承的类
- 派生类继承的 getter,找不到返回正确值的方法
- MSVC:协变返回类型和虚拟继承
- 模板类的专门继承使成员函数返回模板类型,而不是继承的类类型
- 创建一个函数,该函数返回具有现有类继承的不同类型
- 返回指针到继承的C 类
- 为什么dynamic_cast返回具有多级继承的 null
- 继承的函数返回派生类,而不是基类
- 通用lambda,继承和尾随返回类型:此有效代码
- C++ 如何从具有不同返回类型的接口进行多个继承
- 具有多重继承的纯虚函数重载和协变返回类型
- C 多重继承,虚拟方法覆盖问题和协变量返回类型
- 继承的加法赋值操作,如何返回正确的类型
- 在 c++ 中返回一个字符串作为私有继承类
- 虚拟函数未返回具有多级继承的派生类型
- C++ 无法返回继承的类对象
- 多重继承中返回的奇怪 int 值
- C++继承未返回正确的值