C + + Learning Summary Review

Source: Internet
Author: User

?

  1. Use of Friends

    Divided into friend and friend functions

  2. BlankTest.cpp: Defines the entry point of the console application.
  3. //
  4. The common denominator of friend and friend functions: You can have one class as a parameter of another class or function.
  5. ?
  6. Friend class: It makes the current class A friend of another class, and then another class can access the private members of the current class.
  7. #include "stdafx.h"
  8. #include <iostream>
  9. using namespace std;
  10. ?
  11. class MyClass
  12. {
  13. ??? int m;
  14. ??? int N;
  15. Public: //If not public, compile error. A friend class cannot access a private member.
  16. ??? MyClass (int i, int j)
  17. ??? {
  18. ?????? m = i;
  19. ?????? n = j;
  20. ???}
  21. ??? Friend class test; //Use of friend class
  22. ??? ///Simple point is that the outer function can be accessed inline
  23. ??? Friend int Sub (MyClass K); //Use of the friend function
  24. };
  25. ?
  26. Class Test
  27. {
  28. Public:
  29. ??? void Test (MyClass K)
  30. ??? {
  31. ?????? cout << k.m << "<< k.n << Endl;
  32. ???}
  33. };
  34. ?
  35. int Sub (MyClass K)
  36. {
  37. ??? return K.M;
  38. }
  39. ?
  40. int _tmain (int argc, _tchar* argv[])
  41. {
  42. ??? MyClass *p = new MyClass (3,6);
  43. ??? Test *t = new test ();
  44. ??? T->test (*P);
  45. ??? cout << Sub (*p) <<endl;
  46. ??? return 0;
  47. }

    ?

    The sudden thought of static, const, static const, and their initialization

    A const-defined constant exceeds its scope and is freed, and static constants that are defined in the function do not release their storage space after a straight line.

    For each of the following initialization rules:

      1. Const-defined constants that need to be initialized in the initialization list.
      2. Static variables defined by static, which need to be initialized outside the class.
      3. const static, like the static const, is also required to be initialized outside the class.
      4. The const form of a method whose primary function is to prevent member functions from modifying the value of a member variable. A scope is an object, and different objects can have different values for const-defined constants.
      5. The method of static form has its main function in order to be used as a global method. The scope is the entire class. Typically used as a tool class, different objects can modify the values of static variables,

        You cannot modify the value of a const static constant.

    ?

    Note: In order to establish a constant value in a class, the Enum enumeration can be implemented in addition to the const static.

    ?

    Examples are as follows:

    BlankTest.cpp: Defines the entry point of the console application.

    //

    The difference and connection between const, static, const static, enum

    #include "stdafx.h"

    #include <iostream>

    using namespace Std;

    ?

    class MyClass

    {

    ???? const int m;

    Public:???? static int n;

    ???? const static int mn;

    ???? Enum

    ???? {

    ???????? Size1 = 50,//enum variable not in; Colon, only, comma.

    ???????? Size2 = 60

    ????};

    Public://If not public, compile error. A friend class cannot access a private member.

    ???? static void print ();

    ???? const void Print2 ();

    ?

    MyClass (int a);

    };

    ?

    int myclass::n = 10; Definition of static member + initialization

    const int MYCLASS::MN = 20;

    ?

    Myclass::myclass (int a): M (a)//use A to initialize a const member, where you can write a number directly, such as 10, is no problem.

    {

    ???? n + = 1;//Description We can make changes to the static variable in the constructor

    }

    ?

    ?

    void MyClass::p rint ()

    {

    ???? cout << "count=" << mn << Endl;

    }

    ?

    const void MyClass::p rint2 ()

    {

    ???? m = 20; Error, error message: The expression must be a modifiable left-hand value.

    ???? cout << "Const =" << m << Endl;

    ???? cout << "static =" << n << Endl;

    }

    int _tmain (int argc, _tchar* argv[])

    {

    ???? MyClass a (10);

    ???? A.print ();//accessing static member functions through objects

    ???? MyClass::p rint ();//access static member functions by class

    ?

    ???? A.print2 ();

    ???? A.N + = 1; The static variable can be changed. Static const variables cannot be changed.

    ???? A.print2 ();

    ?

    ???? cout << "enum =" << a.size1 << Endl;

    ???? A.mn + = 1;//error, static constants cannot modify the left value.

    ???? return 0;

    }

C + + Learning Summary Review

Related Article

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.