【发布时间】:2018-10-05 07:09:29
【问题描述】:
在做 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++ 代码中做同样的事情?
【问题讨论】:
-
您的问题是什么?请阅读How to Ask。
-
你显然有一些 Python 代码和一些 C++ 代码。 C++ 代码在哪些方面不能满足您的需求?再次,请阅读How to Ask。
-
你想在 c++ 中模拟的 python 代码是什么,这个 c++ 代码如何不满足这些要求?这只是直接继承。为什么需要在 C++ 中对其进行保护? python 类将这些成员公开(我假设是这种情况,因为它们没有前导下划线)。
标签: python c++ class inheritance protected