C + + Fundamentals (1)

Source: Internet
Author: User

1. About inheritance and access.

C + + in public,protected, private access to the label summary, that is, access to the label usage restrictions.

First: Private, public, access to the Protected Access label.
Private: only 1. The function in the class, 2. Its friend function is accessed.
The object of the class cannot be accessed by any other access.

Protected: Can be 1. A function in this class, a function of 2. Subclass, and 3. Its friend function is accessed.
But cannot be accessed by objects of that class.

Public: Can be 1. The function in the class, the function of the 2. Subclass, 3. Its friend function is accessed by 4. The object of the class.

Note: The friend function consists of 3 types:

(1): ordinary non-member function set as friend;

(2): member functions of other classes that are set as friends;

Class B;
Class A
{ ...
Friend int b::f ();
...
};

(3): Set as all member functions in the friend class.

Second: The class inherits the method property changes.
You can imagine the inheritance of a class as access to the parent class object. Not that it's private. The protected and public properties of the stepfather class become private in the subclass because the subclass can access its own private, saying it can access the private property of the parent class. For the parent class, the property changes after the quilt class inherits, and the direct effect of this change is that the subclass accesses the parent class with restricted access permissions, and if the original permission of the parent class is required, the public inheritance method should be used.


The private property cannot be inherited.
With private inheritance, the protected and public properties of the parent class become private in subclasses;
With protected inheritance, the protected and public properties of the parent class become protected in the subclass;
With public inheritance, the protected and public properties in the parent class do not change;

As shown below:
Public:protected:private:
Public inherited public protected not available
Protected inherited protected protected not available
Private private is not available

Protected inheritance and private inheritance can reduce access rights.

This article is from the "Go Straight" blog, please be sure to keep this source http://cnmtjp.blog.51cto.com/204390/36548

1#include <iostream>2#include <string>3 using namespacestd; 4 classTeacher5 {    6      Public:    7Teacher (Char*temp)8     {    9Director =New Char[Ten]; Ten strcpy (director,temp);  One     }   A~Teacher () -     {     -cout<<"Free Heap area director memory Space 1 times";  the         Delete[] Director;  -Cin.Get();  -     }     -     Char*Show ();  +     protected:     -     Char*Director;  + };  A Char*teacher::show () at {     -     returnDirector;  - }     - classStudent - {     -      Public:     in Student () -     {     toNumber =1;  +Score = -;  -     }     the     voidShow ();  *      $     protected:    Panax Notoginseng     intNumber ;  -     intscore;  theTeacher Teacher ("Wang Dali");//error, a member of a class cannot be initialized with a constructor with parameters in the class if it is an object of another class +      A };  the      + voidstudent::show () - {     $Cout<<teacher.show () <<endl<<number<<endl<<score<<Endl;  $ }     - voidMain () - {     the Student A;  - a.show (); WuyiStudent b[5];  the      for(intI=0; i<sizeof(b)/sizeof(Student); i++)     -     {     Wu b[i].show ();  -     }     AboutCin.Get();  $}
View Code

Unfortunately, the program can not be compiled successfully, why?


Because: the class is an abstract concept, not an entity, and cannot contain attribute values (here is the argument of the constructor), only the object occupies a certain amount of memory space, contains a definite attribute value!

See: Http://pcedu.pconline.com.cn/empolder/gj/c/0503/568909.html.

3. Calls to constructors and destructors are sequential:

http://blog.csdn.net/xw13106209/article/details/6899370

4. About deep copy, shallow copy

Http://www.cnblogs.com/BlueTzar/articles/1223313.html

5. Common knowledge points

http://blog.csdn.net/lushujun2011/article/details/6827555

C + + Fundamentals (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.