网站建设要求 牛商网,WordPress全球用户量2019,WordPress微信域名防封源码,大兴企业网站建设当一个子类从父类继承时#xff0c;父类的所有成员成为子类的成员#xff0c;此时对父类成员的访问状态由继承时使用的继承限定符决定。 1.如果子类从父类继承时使用的继承限定符是public#xff0c;那么 (1)父类的public成员成为子类的public成员#xff0c;允许类以外的代…当一个子类从父类继承时父类的所有成员成为子类的成员此时对父类成员的访问状态由继承时使用的继承限定符决定。 1.如果子类从父类继承时使用的继承限定符是public那么 (1)父类的public成员成为子类的public成员允许类以外的代码访问这些成员 (2)父类的private成员仍旧是父类的private成员子类成员不可以访问这些成员 (3)父类的protected成员成为子类的protected成员只允许子类成员访问 2.如果子类从父类继承时使用的继承限定符是protected那么
(1)父类的public成员成为子类的protected成员只允许子类成员访问 (2)父类的private成员仍旧是父类的private成员子类成员不可以访问这些成员
(3)父类的public成员成为子类的protected成员只允许子类成员访问 3.如果子类从父类继承时使用的继承限定符是private那么
(1)父类的public成员成为子类的private成员只允许子类成员访问 (2)父类的private成员仍旧是父类的private成员子类成员不可以访问这些成员 (3)父类的protected成员成为子类的private成员只允许子类成员访问其实这些都很有的规律的,子类public时表示最大的继承权限是public,所以子类按照原样继承,子类protected继承时最大继承权限是protected, 所以基类的public成员降级成为protected了....子类private继承时所以都成为private了, 不过子类不能访问基类的private成员.. 子类默认的是private继承基类... 举个使用private继承的例子,Boost::Utility库的不可以复制的类 noncopyable #include boost/utility.hpp
或者是
#include boost/noncopyable.hpp #ifndef BOOST_NONCOPYABLE_HPP_INCLUDED
#define BOOST_NONCOPYABLE_HPP_INCLUDEDnamespace boost {// Private copy constructor and copy assignment ensure classes derived from
// class noncopyable cannot be copied.// Contributed by Dave Abrahamsnamespace noncopyable_ // protection from unintended ADL
{class noncopyable{protected:noncopyable() {}~noncopyable() {}private: // emphasize the following members are privatenoncopyable( const noncopyable );const noncopyable operator( const noncopyable );};
}typedef noncopyable_::noncopyable noncopyable;} // namespace boost#endif // BOOST_NONCOPYABLE_HPP_INCLUDED类 boost::noncopyable 被规定为作为私有基类来使用它可以有效地关闭复制构造和赋值操作: #include boost/utility.hppclass please_dont_make_copies : boost::noncopyable {
public:void do_stuff() {std::cout Dear client, you just cannot copy me!;}
};这样就禁止了复制和赋值.... c big three 三法则英语rule of threethe Law of The Big ThreeThe Big Three三法则三大定律在 C 程序设计里它是一个以设计的基本原则而制定的定律三法则的要求在于假如类型有明显地定义下列其中一个成员函数那么程序员必须连其他二个成员函数也一同编写至类型内亦即下列三个成员函数缺一不可。 [1]:
析构函数Destructor复制构造函数copy constructor复制赋值运算符copy assignment operator 上述三个函数是特别的成员函数假如程序员没有自行定义或是编写声明它们那么编译器会自动地创建它们并且会编译至应用程序内。相反地假如程序员有定义上述三者其中一个函数那么由编译器自动产生出来的上述三个函数是不会搭配到这个类型内。三法则Rule of three这个专有名词是由 Marshall Cline 于 1991 年创立的[2]。 class_a.h文件 #ifndef _CLASS_A_H_
#define _CLASS_A_H_#ifndef _MSC_VER
#undef NULL
#define NULL 0
#endif
#include iostream
#include cstdlib
#define BUFFER_SIZE 7using namespace std;class ClassA
{
public:// 三種建構子ClassA(){coutClassA():endl;this-setAlloc(BUFFER_SIZE);this-setData();}ClassA(const int n){coutClassA(const int n):endl;this-setAlloc(n);this-setData();}// 複製建構子ClassA(const ClassA clone){coutClassA(const ClassA clone):endl;this-setAlloc(clone.m_N);this-setData(clone.m_pn);}// 複製指定運算子成員函式ClassA operator(const ClassA clone){coutClassA operator(const ClassA clone)endl;// 保護禁止自己設值給自己if ( this ! clone ){this-setData(clone.m_pn);}return *this;}// 解構子~ClassA(){cout~Destructor!!!endl;// 釋放記憶體delete [] this-m_pn;}// 配置void setAlloc(const int n){this-m_N n;// 配置一塊記憶體給指標this-m_pn new int[this-m_N];}// 填入一堆的整數值void setData(int* pn NULL){for ( int i 0; i this-m_N; i ){// 給初始值if ( pn NULL ){this-m_pn[i] (2 * i 1);}// 複製指標儲存的整數值else{this-m_pn[i] pn[i];}}}// 列印顯示void print(void){for ( int i 0; i this-m_N; i ){cout this-m_pn[i];}coutendl;}
private:// 指標int* m_pn;// 元素個數int m_N;
};#endif主函数 // Headers and Macros
#ifndef _MSC_VER
#undef NULL
#define NULL 0
#endif
#include iostream
#include cstdlib
#include class_a.h
using namespace std;
//
//Main Function
#ifndef _MSC_VER
int
#else
void
#endif
main(int argc, char** argv)
{// 區塊{// 建立第一個物件ClassA A(BUFFER_SIZE);cout A ;A.print();{// 開始執行 ClassA(const ClassA clone)ClassA B A;cout B ;B.print();}{ClassA C;// 開始執行 ClassA operator(const ClassA clone)C A;cout C ;C.print();}}system(PAUSE);return
#ifndef _MSC_VEREXIT_SUCCESS
#endif;
}http://zh.wikipedia.org/wiki/%E4%B8%89%E6%B3%95%E5%89%87_(C%2B%2B%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88)