derived

C++ primer 笔记(四)

北城余情 提交于 2020-01-21 08:45:49
第15章 object-oriented programming, OOP 面向对象编程 :数据抽象,继承,动态绑定。 通过基类的引用或指针调用虚函数时,发生动态绑定。 除了构造函数之外,任意非 static 成员函数都可以是虚函数。保留字virtual只在类内部的成员函数声明中出现,不能用在类定义体外部出现的函数定义上。 protected不能被外部访问,但可被派生类访问。 private继承:【Derived对象】不能调用Base中的任何东西;所有继承方式【Derived类】都还能调用Base类中的public和protected的东西(成员函数)。Derived类的Derived则都无法调用。 ----------------------------------------------------------- private继承 基类成员 private成员 public成员 protected成员 内部访问 不可访问 可访问 可访问 对象访问 不可访问 不可访问 不可访问 ------------------------------------------------------------ public继承 基类成员 private成员 public成员 protected成员 内部访问 不可访问 可访问 可访问 对象访问 不可访问 可访问 类定义中可访问,外部不可访问

一文搞懂各种属性及方法

烈酒焚心 提交于 2020-01-17 17:06:51
class Base: def __init__(self): self.__x = "a" __private = 1024 count = 100 @classmethod def rcount(cls): print(Base.count) @staticmethod def sta_rcount(): print(Base.count + 1) def free(): print("freedom") class Derived(Base): def test(self): ''' 测试私有类属性、私有实例属性可访问性 :return: ''' print(Base._Base__private) print(Derived._Base__private) print(self._Base__x) pass b = Base() dc1 = Derived() # 类属性 print(b.count, dc1.count, Base.count, Derived.count) # 类方法 dc1.rcount() b.rcount() Base.rcount() Derived.rcount() # 静态方法 dc1.sta_rcount() b.sta_rcount() Base.sta_rcount() Derived.sta_rcount() # 自由方法 Base

Prevent calling base class implemented interface method in the derived class C#

微笑、不失礼 提交于 2020-01-13 20:35:49
问题 Is it possible to implement an interface in a base class and allow calling/overriding the implemented method in the first derived class level but prevent calling it from any further derived classes? public interface IInterfaceSample { bool Test(); } public class Base: IInterfaceSample { public virtual bool Test() { return True; } } public class Sub1: Base { //I need to be able to override the Test method here public override bool Test() { return True; } } //Under a separate project: public

Prevent calling base class implemented interface method in the derived class C#

北慕城南 提交于 2020-01-13 20:34:06
问题 Is it possible to implement an interface in a base class and allow calling/overriding the implemented method in the first derived class level but prevent calling it from any further derived classes? public interface IInterfaceSample { bool Test(); } public class Base: IInterfaceSample { public virtual bool Test() { return True; } } public class Sub1: Base { //I need to be able to override the Test method here public override bool Test() { return True; } } //Under a separate project: public

Calling base method from derived class

ぐ巨炮叔叔 提交于 2020-01-11 09:51:52
问题 I have, for example, such class: class Base { public: void SomeFunc() { std::cout << "la-la-la\n"; } }; I derive new one from it: class Child : public Base { void SomeFunc() { // Call somehow code from base class std::cout << "Hello from child\n"; } }; And I want to see: la-la-la Hello from child Can I call method from derived class? 回答1: Sure: void SomeFunc() { Base::SomeFunc(); std::cout << "Hello from child\n"; } Btw since Base::SomeFunc() is not declared virtual , Derived::SomeFunc()

Get address of base object from derived object

谁说我不能喝 提交于 2020-01-01 19:19:33
问题 I'm getting a very confusing error in my program. I think I may have two different objects of the same class where I thought I had the same object. It is confusing because I am dealing with a very large framework where it is not simple to obtain a pointer to the object I need. My question is, if I have a class Derived which in inherits from Base, and I have a pointer to a Derived object, how can I get the address of the Base object from the derived object? I am working with the source code of

Protected Member Access

社会主义新天地 提交于 2019-12-29 10:15:35
https://msdn.microsoft.com/en-us/library/bcd5672a.aspx 官方的说法 The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances. protected关键字是一个成员访问修饰符。一个protected的成员,一个protected成员,在其所在的类中,可由其派生类的实例访问。 可访问性级别的介绍: https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx protected :Access is limited to the containing class or types derived from the containing class. protected关键字:只能由包含的类进行访问,或者从包含该成员的类所派生的类进行访问。 疑惑的地方: 错误观点 我本以为 只要是派生类的实例,就可以访问受保护的成员 。 子类的实例,可以赋值给父类类型的变量。通过父类类型的变量,是不允许访问protected成员的。 http://blogs.msdn.com/b

Not defining Virtual method in derived class as well

十年热恋 提交于 2019-12-25 07:49:59
问题 I have the following class hierarchy. class A { public: virtual bool foo() const; }; class B : public A { // Redeclare foo as virtual here? }; class C : public B { bool foo() const {/*Definition*/ return true;} }; class D : public B { bool foo() const {/*Definition*/ return false;} }; So the foo() method the class C and D wants to implement, B doesn't. How can I achieve that? Do I have to re-declare the foo() as virtual in class B? Note: Ignore minor syntactical error here and there. This is

calling help(MyClass) also shows base class attributes: how to avoid that?

微笑、不失礼 提交于 2019-12-25 02:09:44
问题 MyClass is derived from "list": MyClass(list) I would like to document MyClass nicely. Unfortunately, when trying help(MyClass), I get my own documentation, but I also get a lot of stuff about "list". Would there be a simple way to control that? I read something about metaclasses, but I was unable to do something. Thanks for your suggestions, Michel 回答1: Well, that is what help does. It introspects into your class and show the name and the associated __doc__ for each callable attribute in the

C and derived data types?

限于喜欢 提交于 2019-12-24 01:36:07
问题 I know the fundamental data types in C - char, int, float etc. But What exactly are derived data types in C language? 回答1: 6.2.5.20 of the standard (well, a draft; hooray free :) covers derived types : 20 Any number of derived types can be constructed from the object, function, and incomplete types, as follows: -- An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. Array types are characterized by their