C++继承未返回正确的值

C++ inheritance not returning the correct values

本文关键字:返回 继承 C++      更新时间:2023-10-16
#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.xd.yd.z 0,因为它所做的只是分别从abc进行分配。

生类的方法不会自动调用基类的相同方法 - 它替换基实现,不会添加到基实现中。如果你也想使用基类实现,你必须自己做。

void derived :: read()
{
    base::read();
    x=a;
    y=b;
    z=c;
}

当您创建 Derived 的实例时,它不会获取您从控制台分配给 Base 实例的a bc的值:它是另一个独立的对象!

 void derived :: read() {
     x=a;
     y=b;
     z=c;
 }

这实际上将垃圾放入x yz。您可以在derived::read()中调用base::read()来初始化a bc

我猜你试图做如下的事情:

   #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/

我希望这有所帮助。