C + + Basic summary article

Source: Internet
Author: User

Today concludes a week of study. The main content of learning is C + + basic knowledge, mainly from the following main points summary: template base class
Call virtual function polymorphism failure in link base class importance of virtual destructor in base class
Link friend functions and class operator overloads
Links between C + + classes and C. languages
to a link.

Why the class constructor initializes the list
1. If we have a class member, it itself is a class or a struct, and this member has only one constructor with parameters and no default constructor. To initialize this class member, you must call the constructor with the parameter of the class member, if there is no initialization list, then he will not be able to complete the first step, will be the error.
2. If there is a const modifier in a class member, you must assign a value to the Const property when the object is initialized
When a const object is contained in a class member, or when a reference is made, they must also be initialized through the member initialization list.

function returns a static variable reference that can be used to make the left value

Demo Program:

#include <stdio.h>


int getA1 ()
{
        static int a =10;
        A + +;
        return A;
}

int* getA2 ()
{
        static int a =20;
        a++;
        Return &a;
}

int& getA3 ()
{
        static int a =30;
        A + +;
        printf ("a:%d\n", a);
        return A;
}

int main ()
{

        int cc = GETA1 ();
        printf ("cc:%d\n", cc);

        Int*w = GetA2 ();
        printf ("w:%d\n", *w);

        int t = getA3 ();//a:31
        printf ("t:%d\n", t);

        Geta () = m;  The left operand must be a left value of 

        //geta2 () = (int*) 100;//cannot be a left value
        *geta2 () =100;
        printf ("*geta2 ():%d\n", *geta2 ());

        GetA3 () = m;
        GetA3 ();

        return 0;
}

Run Result:

Cc:11
w:21
a:31
t:31
*geta2 ():
a:32

Static variables in a class template

Just give me an example:

#include <iostream>
using namespace std;

Template<typename t>

Class AA
{public
:
        AA ()
        {
                a++;
        }
        Static T A;
        void PrintA ()
        {
                cout<< "a:" <<a<<endl;
        }
};
Template<typename t>
T aa<t>::a = 0;


int main ()
{
        aa<int> Aa1;
        Aa1.printa ();
        Aa<int> Aa2;
        Aa2.printa ();
        Aa<int> Aa3;
        Aa3.printa ();


        Aa<char> BB1;
        bb1.a = ' a ';
        Bb1.printa ();
        Aa<char> Bb2;
        Bb2.printa ();
        Aa<char> Bb3;
        Bb3.printa ();

        return 0;
}

The output results are:

A:1
a:2
a:3
a:a
a:b
a:c

Another drawing for a family:

Do not understand the words, they find csdn Daniel wrote the article, here only to do a tip.
12. Abstract class, your pain ...
An abstract class is a class that contains a pure virtual method. Remember: As long as a pure virtual method appears in a class (for example: virtual void func () = 0; is a pure virtual function), then this class is an abstract class. Abstract classes have many taboos, but the bottom line is that abstract classes cannot create objects. To fully understand this abstract class, give a few abstract classes of taboos:

#include <stdio.h>

class b{public
:
        virtual void print () =0;
        virtual void func () =0;
        B () {}
        B (int a, int b=0) {printf ("%d:%d\n", A,b);}
;

int main () {
        //b b;//abstract class cannot establish object
        B *p;//abstract class can use pointer

        //abstract class cannot be function return value and function parameter
        //b fun1 ();
        void Fun2 (B);

        b& fun3 (b&);//abstract class reference can be a function parameter and function return value
}

Why an abstract class cannot be a problem with function arguments and function return values. Abstract class reference is OK. My understanding is that an abstract class produces an implicit assignment or creates an abstract class object when it makes function arguments and function return values. Because an abstract class cannot be instantiated, an error occurs when an abstract class returns a value as a function argument and function. Why is it possible to quote. My explanation is that the reference is actually an address and that the address does not have an initialization of the abstract class.

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.