Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;继承如何获取变量值_C++_Variables_Inheritance - Fatal编程技术网

C++ C++;继承如何获取变量值

C++ C++;继承如何获取变量值,c++,variables,inheritance,C++,Variables,Inheritance,我正在尝试创建一个小型课程。 但在我的类中,我没有超类的值。 ,这是密码 h点 #ifndef Point_H #define Point_H #include <iostream> class Point{ public: Point(); void set_values (int a, int b); void set_values (int a, int b, int c); void afficher

我正在尝试创建一个小型课程。 但在我的类中,我没有超类的值。 ,这是密码

h点

#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
  public:
         Point();
         void set_values (int a, int b);
         void set_values (int a, int b, int c);
         void affichervaleurs();
  protected:
         int x ;
         int y ;
         int z ;
  };
#endif
void set_values (int a, int b);
void set_values (int a, int b, int c);
Carre.cpp

#include <iostream>
using namespace std;
#include "Point.h"

class Carre:public Point{
  public:
         //Carre::Carre(int a, int b);
         int Aire (){
             return (x * y); 
         }
         void affichercar(){
             cout << "Coordonnees X:" << x  << endl;
         }

  };
#include <iostream>
using namespace std;

int Carre::Aire (){
  return (x * y); 
}
void Carre::affichercar(){
  cout << "Coordonnees X:" << x  << endl;
}
与此类似,x和y不与超类链接

有人能帮助我如何从我的类Carre的超类中访问x和y值吗

其他奇怪的事情,这行没有出现:

MonCarre.affichercar();
谢谢

访问我的x和y值

Carre
中,您只需通过“x”和“y”访问它们


从外部看,您不能这样做,因为它们不是公共的。

您可以在Carre.h中定义函数
Aire
affichercar
,但只有在您想在Carre.cpp中编写实现时,才需要声明它们

编辑:你的Carre.cpp也错了。您只需要重写Carre.*文件

编辑2。让我做一些代码回顾,并给你一个代码的工作版本——只是总结一下评论中讨论的内容和我自己的想法

h点

#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
  public:
         Point();
         void set_values (int a, int b);
         void set_values (int a, int b, int c);
         void affichervaleurs();
  protected:
         int x ;
         int y ;
         int z ;
  };
#endif
void set_values (int a, int b);
void set_values (int a, int b, int c);
如果您想定义第二个函数的参数的默认值,您应该在它的声明中这样做,正如在对您的问题的评论中所建议的那样。就这样吧

void set_values (int a, int b);
void set_values (int a = 0, int b = 0, int c = 0);
但是在这种情况下,如果您编写了,例如,
set\u values(1,2)
,则不清楚应该调用什么函数,因此您的代码不会编译,因为存在歧义(注释中也提到了这一点)。因此,您只需使用默认参数保留此函数的一个扩展版本。在这种情况下,点h为:

#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
  public:
         Point();
         void set_values (int a = 0, int b = 0, int c = 0);
         void affichervaleurs();
  protected:
         int x ;
         int y ;
         int z ;
  };
#endif
现在我开始回答你们的问题:

问题是,当我拨打这条电话时:
您的
点不正确。h
代码错误;您已经为
Carre
粘贴了类定义。请更新您的问题。@Duk先生,谢谢。我更正了。我仍然很困惑;您的Carre.cpp包含了对类的重新定义。看起来OP想要做一个代码检查——是否应该将其移动到?我认为存在一些设计问题。首先,保护数据成员通常不是一个好主意。这定义了强耦合,因为子类依赖于基类的实现内部。第二,我认为说“一个矩形是一个点”从根本上是错误的,但这是继承模型所描述的。Carre有两个point会员不是更有意义吗?我想我就是这么做的?在我的课堂上:Carre我在做:int Aire(){return(x*y);};但返回值不是5*8。。。。我不知道我是否忘记了什么,但它不起作用。当然,如果你没有得到编译错误,那么你可能做得很好——如果输出不符合预期,你可能需要调试程序,但我不认为这是你的问题(即“帮我调试程序…”),Carre.Aire的返回值不返回x*y,就像我们的值不好一样。这就是为什么我问自己是否忘记了什么,或者有人看到了代码中的错误。(1)本网站不提供代码审查。(2) 我不知道你们看到的是什么输出,或者为什么你们认为这不是正确的输出。(3) 尝试在调试器中运行代码,然后单步执行代码。
void set_values (int a, int b);
void set_values (int a = 0, int b = 0, int c = 0);
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
  public:
         Point();
         void set_values (int a = 0, int b = 0, int c = 0);
         void affichervaleurs();
  protected:
         int x ;
         int y ;
         int z ;
  };
#endif
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point()
 // it's better to initialize these variables here
 // : x(0), y(0), z(0)
{
  x=0;
  y=0;
  z=0;
}
void Point::set_values (int a, int b, int c){
 x=a;
 y=b;
 z=c;
}
void Point::affichervaleurs(){
 cout << "X = " << x << endl;
 cout << "Y = " << y << endl;
}
class Carre:public Point{      
  public:
    // IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
    int Aire (){};
    // IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
    void affichercar(){};    
  };
#ifndef Carre_H
#define Carre_H
#include "Point.h"

class Carre:public Point{      
  public:
         int Aire (); // now it's declaration
         void affichercar(); // now it's declaration

  };
#endif
#include <iostream>
using namespace std;

int Carre::Aire (){
  return (x * y); 
}
void Carre::affichercar(){
  cout << "Coordonnees X:" << x  << endl;
}