• C++第四章经验整理2


    We have to declare the operator function by using the operator, operator sign. Example “operator +” where the operator is a keyword and + is the symbol need to be overloaded.

    The overloaded operator must not have at least one operand of its class type.

    Operator overloading is the way adding operation to the existing operators.

        #include <iostream>
        using namespace std;
        ostream & operator<<(ostream & i, int n)
        {
            return i;
        }
        int main()
        {
            cout << 5 << endl;
            cin.get();
            return 0;
        }

    In this program, there will arise an ambiguous overload for 5.

    Operator overloading helps programmer to give his/her own meaning to an operator for user defined data types(eg, classes).

    here are 3 different approaches used for operator overloading:
    i. Overloading unary operator.
    ii. Overloading binary operator.
    iii. Overloading binary operator using a friend function.

    When an operator overlaoded function is declared as friend function then [], () and -> cannot be overloaded.

    In the case of non-static member functions unary operator overloaded function should not take any object argument.

    Both arithmetic and non-arithmetic operators can be overloaded. The precedence and associativity of operators remains the same after and before operator overloading.

    Arity means a number of operands an operator requires to perform its action and operator overloading does not changes the arity of any operator.

    In this question we are asked to give the function prototypr not the function definition so the answer should not contain {} braces.

    <complex> header file is used for declaring a complex number in C++.

    There are three real types in complex numbers. They are float complex, double complex, long double complex.

    Real is used for returning real part, imag for imaginary part and norm for calculating norm of a complex number. There is no such function Cartesian in complex header file.

    There are two types of user-defined conversions. They are conversion by the constructor, Conversion functions.

    The keyword friend is placed only in the function declaration of the friend function and not in the function definition because it is used toaccess the member of a class.

    Friend functions are not in the scope of a class and hence cannot be called through a class object. A friend function can access all types of members of the class. They can be invoked as a normal function.

    Friend function can be declared either in private or public part of the class. A friend function cannot access the members of the class directly. They use the dot membership operator with a member name.

    Memory heap will store the large objects in c++ if it extends its allocated memory.

    Because the vector is mainly used to store large objects for the game programming and other operations etc.

    Some of the ways to stop the program by consuming more ram. They are
    i) Find a way to work with the data one at a time
    ii) Declare it in program memory, instead of on the stack
    iii) Use the hard drive, instead of RAM

    Virtual destructor means is that the object is destructed in reverse order in which it was constructed and the smart pointer will delete the object from memory when the object goes out of scope.

    setrlimit() is used to unlimit the size of the stack.

    Scope resolution operator is having the highest precedence in c++.

    Overloading the objects is the use of function call operator.

    Virtual functions does not give the ability to write a templated function.

    What will happen when the function call operator is overloaded?  It will modifies how the operator is to be interpreted when applied to objects of a given type.

    In non-static member function, the function call operator can be overloaded.

    To access any element of an array we use following syntax array[i], where i is called subscript representing the ith element of an array, whereas no such cases in char and strings.

    The subscript operator overload takes only one argument, but it can be of any type.

    If you have a pointer to an object of some class type that overloads the subscript operator, you have to dereference that pointer in order to free the memory.

    Dereferencing is using a pointer with asterix. For example, *(abc).

    References automatically dereference without needing an extra character.

    What does the dereference operator will return? It operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address.

    The null pointer dereference occurs where a pointer that is expected to be a valid address but instead is equal to null.

    Pre Increment is faster than post-increment

    C++ provides the following two types of string representations. They are C-style character string and string class type with Standard C++.

    both size & length will return the number of characters that conform to the string’s content.

    String is defined as streams of characters, not necessarily terminated by . Also, a string can contain characters other than alphabets.

    To concatenate two string objects we are provided with either direct addition or append() function in string class but strcat() is char* function hence they cannot be used to concatenate two string objects.

    The string class provides string(string s) as a constructor not the string(char) as a constructor therefore this assignment is not valid.

    Destructors are called at the following time:
    i) at the end of the program to destroy objects declared in the main() or global scope.
    ii) at the end of a function to destroy objects declared at that function scope.
    iii) when user by himself tries to delete an object using the delete operator.
    iv) at the end of a block to destroy objects declared at that block scope.

    The destructors for an object is called before the destructor of its data members or bases.

    Here as ‘a’ is a static member of class B and as all static members should be initialized separately as no object creation initializes static member and as ‘a’ is not initialized, hence no call will be made to the constructor of class A.

    Explicit call represents the programmer by himself mentioning the type name. So A a = A(5); is the correct explicit call as we are mentioning typename A(5) from our side, whereas A a = A(); is not the correct call because no such constructor is there in class A.

    #include <iostream>  
    using namespace std;
    class A{
    public:
        int a;
    };
    int main(int argc, char const *argv[])
    {
        A a1 = {10};
        A a2 = a1;
        cout<<a1.a<<a2.a;
        return 0;
    }

    Although the declaration of object a1 looks erroneous but actually it is acceptable by the C++ compiler to take values for class objects as mentioned above. Next value of a1 is copied to a2 hence 1010 is printed.

    Unlike new malloc never calls the constructor of a class hence when we are assigning memory to an object of class A using malloc constructor is not called hence nothing is printed.

  • 相关阅读:
    Log4Net 全方位跟踪程序运行
    ASP.NET MVC 3和Razor中的@helper 语法
    C# 4.0四大新特性代码示例与解读
    程序员必读
    重学算法(1)--遍历二叉树
    重学算法-目录
    Epplus使用技巧
    JQuery 获取URL中传递的参数
    Epplus 使用案例
    .net调用存储过程详解(转载)
  • 原文地址:https://www.cnblogs.com/hhlys/p/13477295.html
Copyright © 2020-2023  润新知