C + + Primer class 12.3-12.5 scope, construtor,friend__c++

Source: Internet
Author: User

12.3 Class Scopes

1. In member functions that are defined outside the class, the formal parameter list and the member function body appear after the member name. These are defined in a class scope, so you can refer to other members without qualification. For example, the definition of the two parameter version of Get in class screen:

     Char Screen::get (index R, index c) const
     {
         Index row = r * Width;      Compute the row location return
 
         Contents[row + c];   Offset by c to fetch specified character
     }

The function uses the index type defined in screen to specify its formal parameter type. Because the formal parameter list is within the scope of the screen class, it is not necessary to indicate that we want screen::index. What we want is defined in the current class scope, which is implied. Similarly, using index, width, and contents refers to the names declared in the screen class.

2. The function return type is not necessarily in the class scope. You must use the fully qualified type name Screen::index to specify that the index you want is the name defined in class screen.

3. A name lookup in a class scope. (The best way to read a book is to take a different name)

12.4 constructors

※ The constructor is a special member function. Called when the class object is defined. Constructors cannot be called by defined class objects, and constructors can define multiple or constructors that allow overloading.

※ If no constructors are defined, the system assigns a default constructor with no parameters to the class, and the compiler does not generate a default constructor as long as the class defines a constructor. The compiler will automatically generate a default constructor only if a class does not have a constructor defined.

※ the definition of class object can not be written as Sales_item myobj (); The compiler will understand that a function declaration that returns a sales_item type called myobj. The correct wording is to remove the parentheses behind it.

※ The constructor is not allowed to be defined as a const, so the definition produces a syntax error: Sales_item () const {};

※ Constructors perform initialization of class data members. Conceptually, a constructor can be considered to be performed in two phases: (1) initialization phase and (2) ordinary computational phase. The calculation phase consists of all the statements in the body of the constructor function.

※ Regardless of whether the member is explicitly initialized in the constructor initialization list, the data members of the class type, such as String, are always initialized during the initialization phase . Initialization occurs before the start of the calculation phase.

※ initializes the data member using the version of the constructor initialization list, and does not have a constructor version that defines the initialization list to assign a value to the data member in the constructor function body.

※ Remember, you can initialize objects of a const object or reference type, but you cannot assign values to them. To complete initialization before the function body of the constructor is started. The only chance to initialize a const or reference type data member is in the constructor initialization list.

※ The constructor initialization list specifies only the values that are used to initialize the member, and does not specify the order in which the initialization is performed. The order in which members are initialized is the order in which members are defined.

※ Constructors with default arguments are considered by the compiler to be the default constructor, and the default constructor can have only one.

※http://write.blog.csdn.net/postedit/51541387

※ Which of the following statements is incorrect (if any)? Why?

(a) The class must provide at least one constructor.

(b) There are no formal parameters in the parameter list of the default constructor.

(c) If a class has no meaningful default value, the class should not provide a default constructor.

(d) If a class does not have a default constructor defined. The compiler automatically generates one and initializes each data member.

is the default value for the related type.

Troubleshooting

(a) not correct. Because a class can also not provide a constructor, a default constructor synthesized by the compiler is used.

(b) Not correct. Because a constructor that provides a default argument for all parameters also defines a default constructor, which is a physical parameter in the list of constructor parameters.

(c) Not correct. Prisoners for if a class does not have a default constructor, meaning that the class provides a constructor, but does not provide its own default constructor, the class is not available in an environment where the compiler needs to implicitly use the default constructor, so if a class defines another constructor, it should usually provide 1 default constructors ( See section 12.4.3).

(d) not correct. Because the compiler-synthesized default constructor does not initialize the insulted data member to the associated type's default value, it initializes the member with the same rule song as the variable initialization: Members of the class type perform their own default constructors; Members of the built-in and composite types, Only initialize objects defined in the global scope (see 12.4.3).

※ A constructor called by default that can be invoked with a single argument defines an implicit conversion from the formal parameter type to the class type.

Class Mycls
{public
    :
        int i;

        MYCLS (int i) {};
 
        Explicit Mycls (string s) {};

};


Mycls obj (2); You can also use this constructor mycls obj = 2; A type conversion is done here, but the wording is not intuitive.

You can prevent a constructor from being used in a context that requires an implicit conversion by declaring the constructor as explicit: Mycls obj ("Tom"), unable to use mycls obj = "Tom" because the conversion is prohibited, usually unless there is a clear reason to define an implicit conversion, The single parameter constructor should be explicit.

The ※EXPLICIT keyword can only be used on a constructor declaration inside a class. Do not repeat it on definitions outside the definition body of a class.

12.5 friend Yuan

※ in the need to allow certain non member functions to access a class of private members (and protected into the shell). While still blocking general access, friends are useful.

※ The advantages of using friends: You can flexibly implement tasks that require access to private or protected members of several classes, and are easy to mix with other languages that do not support class concepts, such as C language, assembly language, and so on: You can use the I/O stream Library of the C language more naturally by making the old friend Meta function overload.

※ The disadvantage of using friends: A class will grant other functions or classes to the access rights of its non-public members, which will destroy the encapsulation of the class and reduce the reliability and maintainability of the class.

※ The relationship between friends is one-way rather than bidirectional. If a class B is declared to be a friend of Class A, not equal to a friend class of Class B, a member function in Class A cannot access private data in Class B.

※ Friends of the relationship can not be passed, if Class B is a class of friends, Class C is a Class B friend class, not equal to C class is a friend class.

#include <iostream> using namespace std; Class Bezaa;
Forward Declaration of class Bezaa in order for example to compile.
    Class Aazaa {Private:int A; Public:aazaa () {a = 0;}
    void Show (aazaa& x, bezaa& y);
 
friend void Show (aazaa& x, bezaa& y);//Declaration of global Friend};
    Class Bezaa {Private:int B; Public:bezaa () {b = 6;} friend void Show (aazaa& x, bezaa& y);//Declaration of global Friend friend void Aazaa::show (aazaa& x, Bez Aa&y);
 
Declaration of friend from other class}; Definition of amember function of Aazaa;  This is a friend of the Bezaa void Aazaa::show (aazaa&x, bezaa& y) {cout << "show via function memberof
  Aazaa "<< Endl;
  cout << "aazaa::a =" << x.a << Endl;
 
cout << "bezaa::b =" << y.b << Endl;} Friend for Aazaaand Bezaa, definition of global function void Show (aazaa& x,bezaa& y) {cout <<Show via Globalfunction "<< Endl;
  cout << "aazaa::a =" << x.a << Endl;
 
cout << "bezaa::b =" << y.b << Endl;}
   int main () {Aazaa A;
 
   Bezaa b;
   Show (A,B);
   A.show (A,B);
System ("pause"); }


The output results are:

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.