Python class inheritance to c++
Python class inheritance to c++
在进行C 类之前,我决定首先在Python中进行。
有很好的来源可以参加Python类,但我找不到C 的有用来源。
Python代码:
class Human:
def __init__(self, first, last, age, sex):
self.firstname = first
self.lastname = last
self.age = age
self.sex = sex
...
class Student(Human):
def __init__(self, first, last, age, sex, school, semester):
super().__init__(first, last, age, sex)
self.school = school
self.semester = semester
...
C 代码:
class Human {
protected:
string name;
string lastname;
int age;
string sex;
public:
Human(string name, string lastname, int age, string sex):
name(name), lastname(lastname), age(age), sex(sex){
}
~Human();
};
class Student: protected Human{
public:
string school;
int semester;
//Student(string school, int semester);
~Student();
};
我该如何在C 代码中执行相同的操作?
您可以使用初始化器列表在C 中调用超级构造函数并初始化类变量。
作为简化的示例:
class Human {
int age;
string name;
public:
Human(string name, int age) : age(age), name(name) {} // initializer list
};
class Student : public Human {
string school;
public:
Student(string name, int age, string school)
: Human(name, age), school(school) {} // initializer list
}
学生构造函数的Human(name, age)
部分在基本Human
类中调用构造函数。
您评论的Student(string school, int semester)
构造函数无法正确初始化Human
基类,因为它不包含有关Human
(名称,年龄,性别等(的任何信息。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- OpenGL VBO Indexing ( How to compute Index Array)
- 为什么我会" void value not ignored as it ought to be"?
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")