C + + Learning note 1

  • 1, C + + basic concepts

1, object

The idea of object-oriented is to regard the world as composed of various objects with behaviors. Any object has certain characteristics and behaviors (data and algorithm),
That is, the feature is the data, the behavior is the algorithm, and the algorithm is the function of the object.

Object is a variable that contains data and methods
2, class

The abstract of entity with common character is user-defined data type; the design of class is a key step, which directly affects code reuse and program expansion

To seek the commonness of things in a large system, the system with commonness should be defined as a class; the design class should have a clear goal, and a good class should be easy to understand and use

3. Encapsulation, inheritance, polymorphism

Encapsulation: protect the data and operate on the data with the method of class

Inheritance: prevent repeated development; when we design one class and another class, the second class and the first class have common features, and the second class can inherit the properties and methods of the first class. There are sharing features (including data and program code sharing) between classes, new parts (including non shared data and program code) between classes, and hierarchical structure between classes

Polymorphism: polymorphism means that there are many states. A function can accomplish multiple functions is called polymorphism.

In c language: function name is an int abs(int) which cannot be duplicated

In C + +: the function name is int abs(int) float abs(float), which can be duplicated. It is decided which abs function to call by passing different parameters.

Determine the entry address of a function in C + +, not only through the function name. In C + +, the entry address of a function is determined by the function name and parameters
To jointly determine, the two together determine the entry address of a function.

2, C + + Basics

1. Namespace

The so-called namespace refers to various visible ranges of indicator identifiers (variables or functions, etc.). In programming, if two variables with the same name are defined in a function, a redefinition error will be reported. Namespace is to solve this kind of problem

#include<iostream>
//i:input
//o:output
int main()
{
    std::cout<<"hello C++"<<std::endl; //cout in std namespace
    //std::endl stands for newline
    // < represents flow direction
    return 0;
}

Definition of namespace:

namespace myName{
                                  //namespace member
} note that there is no semicolon here
namespace CompanyA{
                                      void fun()
                                                   { }

                                      int num;
                                      }

CompanyA::fun(); CompanyA::num=100;
 

#include<iostream>
//A namespace
namespace A{
    int num;
    void fun(void)//If there is no parameter in C + +, void must be filled in. It cannot be empty
    {
        std::cout<<"A Medium fun"<<std::endl;
    }
} 
//B namespace
namespace B{
    int num;
    void fun(void)//If there is no parameter in C + +, void must be filled in. It cannot be empty
    {
        std::cout<<"B Medium fun"<<std::endl;
    }
}
/*-----------------------Usage 1-----------------------------*/
int main()
{
    //fun(); / / error. I don't know whether to call the fun of namespace A or the fun of namespace B
    A::fun();//Call fun of namespace A
    B::fun();//Call fun of B namespace
    A::num=10;
    B::num=20;
    std::cout<<"A Medium num= "<<A::num<<std::endl;
    std::cout<<"B Medium num= "<<B::num<<std::endl;
    return 0;
}
/*-----------------------Usage 2-----------------------------*/
using namespace B;
//using namespace A;
int main()
{
    //B::fun();
    fun();//If you use two namespaces, A and B, the fun call is ambiguous
    return 0;
}

Usage 1: when referencing variables and functions in different spaces, you need to bring namespace and scope operators

Usage 2: if you use the namespace declared by using before, you can directly reference the variables and functions of the declared namespace. If you use the two namespaces at the same time, the variables and functions of both spaces can be used in the same way, and the variables or functions unique to a namespace can also be used directly

2. New I/O flow

I/O stream: a sequence of characters input or output from an I/O device

cout and cin are object variables

cin can continuously input multiple data; cout can continuously output multiple data

The system default format is used by default, but format control is available

endl operators are equivalent to line breaks in C

#include<iostream>
int main()
{
    //printf("name is %s\n","lucy");
    std::cout<<"name is"<<"lucy"<<std::endl;
    int num=0;
    //scanf("%d",&num);
    //std::cin stands for standard input, that is, keyboard
    std::cin>>num;
    std::cout<<"num= " <<num <<std::endl;
    char name[32]=" ";
    std::cin>>name;
    std::cout<<"name is "<<name<<std::endl;
    //scanf("%d %s",&num,name);
    std::cin>>num>>name;//200 Tom is OK
    //The input of different variables is separated by spaces, and the input can be directly connected without separation when shaping and character type
    std::cout<<"num= " <<num <<std::endl;
    std::cout<<"name is "<<name<<std::endl;
    return 0;
}

If there is a space in a string, the above method cannot obtain the complete input. The following method should be used:

int main()
{
    char name[32]=" ";
    //STD:: CIN > > name; / / if lucy Tom is input, only lucy will be obtained, and the space will be ended
    std::cin.getline(name,sizeof(name),'\n');
    //The third parameter can be left blank. If the third parameter is written, get the input at the end of the third parameter
    //Method of getline, with spaces in the middle of string
    //Read name-1 characters or stop reading when line break is encountered
    std::cout<<"name is "<<name<<std::endl;
    return 0;
}

Add using namespace std; before the main function to omit the above std:

cout can control the output format

int main()
{
    int num=100;
    cout<<"num= "<<dec<<num<<endl;//Output num in decimal
    cout<<"num= "<<hex<<num<<endl;//Output num with sixteen base
    cout<<"num= "<<oct<<num<<endl;//Output num in octal
    return 0;
}

3. Scope operator:

The scope of variables in a program is called scope, which is simply divided into global scope, local scope and statement scope;

If you want to use a global variable with the same name in the scope of a local variable, you can add ":", "":: "before the variable, which is called the scope operator

#include<iostream>
using namespace std;
int avar=10;
int main()
{
    int avar=20;
    cout<<"avar is : "<<avar<<endl;//Local scope is used
    avar=25;
    cout<<"avar is : "<<avar<<endl;
    cout<<"avar is : "<< ::avar <<endl;//Global scope is used
    ::avar = 30;
    cout<<"avar is : "<< ::avar <<endl;
    return 0;
}

4. Structure, common body and enumeration in C + +

The definition method is the same as C language, and there is no difference

But when defining the type variable, C + + can directly use the type name of structure, common body or enumeration to define the variable without adding corresponding keywords

enum BOOL{FALSE,TRUE};

struct STRING{

                         char *prt;

                         int length;

                        };

In c language
                          enum BOOL done;
                          struct STRING str;

In C++
                          BOOL done;
                          STRING str;

It's OK to add it in C + +, it won't be saved, but it's not customary to add it

5. const modifier

const modifier means read only

Ordinary variables decorated with const cannot be assigned again

Const int * p = & data1; / / you cannot change the content of a variable pointed to by p

/ / const modifies that p, p can change its own direction, but cannot change the spatial assignment of p direction through * p

Int * const p = & data1; / / const modifies p, which means p is a read-only pointer
/ / you cannot change the direction of p itself, but you can change the content of the space pointed to by p through * p

Const int * const p = & data1; / / you cannot change the direction of p itself or the content of the space pointed by p through * p

The above three points are the same in C language and C + +

The const decorated variable in C language can be modified by a pointer to the variable, which is prone to memory risk

But it is different in C + +:

#include<iostream>
using namespace std
int main()
{
    const int num=100;
    //Int * P = & num; / / type mismatch, no forced conversion, error will be reported in C + +
    int *p=(int *)&num;
    *p=300;
    cout<<"num= "<<num<<endl;
    //100 is the value of num in the sign constant table, not the value in the variable storage space. The value in the variable storage space is indeed changed to 300 
    cout<<"*p= "<<*p<<endl;//300
    cout<<&num<<endl;
    cout<<p<<endl;
    return 0;
}

 

Published 7 original articles, won praise 2, visited 179
Private letter follow

Tags: C Programming

Posted on Sat, 01 Feb 2020 03:20:14 -0800 by dunn_cann