Namespace - Inheritance - Members Access

Namespace to avoid naming conflicts

namespace MJ{    //定义一个命名空间
    int g_age;    //这是全局变量
    class Person {
        int m_age;
    };
}   

class Person {
    int m_height;
}

MJ::Person person;
MJ::g_age =10;    //使用全局变量

using namespace MJ;    //从这一行开始,下面都使用命名空间的

Namespace created by default are nested inside in the global namespace, it has no name

::func();    //使用的是全局命名空间下的func()

Namespace other programming languages

  • java Package package name (Folder)
  • Objective-C class prefix MJPerson

inherit

struct Person {    //有共性的类
    int m_age;
    void run() {}
};

struct Student : Person {    //继承Person,把它所有东西拿过来
    int m_score;
    void study() {}
}; 
  • Student is a sub-class, subclass, derived class
  • Person is the parent class, superclass, the superclass
  • C ++ does not JAVA, Objective-C class base of
    Java classes inherit all defined it, java.lang.Object, also do not write the default write
    Objective-C is inherited NSObject

After the memory layout object inheritance

//继承相当于包含一样,拿过来 struct Person{ ...}; struct Student : Person { ...}; struct GoodStudent : Student { ...}; // parent row in front of the memory address

Members access

  • public: public (struct default)
  • protected: internal subclass, the current class can access the internal
  • private: private, internal access only the current class (class default)
struct Student : private Person { ...};
class Student : public Person { ...};   //使用这种方式把父类拿过来用

Guess you like

Origin www.cnblogs.com/sec875/p/12284139.html