C++第六章理论题整理1

Separation of interface from implementation introduce the interface of classes in a run-time polymorphic hierarchy.

A class that expresses functionality rather than its primary design role is called a mixin.

clog is an object of class ostream that represents the standard logging stream. It is associated with the cstdio stream stderr, like cerr.

We are allowed to overload constructor but in this case as both the constructor have no parameters which implies that both the constructor have same signature which is not allowed i.e. constructors can be overloaded but two overloaded constructors can not have same function signature.

cerr is an object of class ostream that represents the standard error stream. It is associated with the cstdio stream stderr.

The benefit of dynamic binding and polymorphism is that they help making the code easier to extend but by multiple inheritance it makes harder to track.

If a class is derived privately from a base class then all the members are inherited by the class but are hidden and cannot be accessible

C++ does not allows programmers to make constructor a virtual function.

A function cannot be made virtual and static at the same time.

#include <iostream>
#include <string>
using namespace std;
class A
{
    int a;
   public: 
    A(){
        a = 0;
    }
    void show(){
        a++;
        cout<<"a: "<<a<<endl;
    }
};
 
class B: private A
{
   public:
    void show(){
        show();
    }
};
 
int main(int argc, char const *argv[])
{
    B b;
    b.show();
    return 0;
}

Program will compile successfully

Mutable keyword allows assigning values to a data member belonging to a class defined as “Const” or constant.

We cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.

The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.

type_info is used to hold the type information returned by the typeid operator.

Interface is the best design choice for using pointer to member function.

The binary operator .* combines its first operand, which must be an object of class type, with its second operand, which must be a pointer-to-member type.

static is a type of class member is operator new.

void* object can be allocated outside the object lifetime.

Best coding for the standard library for c++ is:
-> No trailing underscores on names
-> Complex objects are returned by value
-> It should have a member-swap().

Pick out parameter for rehash method in unordered_set in c++? count is used to return the new number of buckets.

There are three sets of requirements. They are container interface requirements, Allocator interface requirements and iterator requirements.

The basic interface of an allocator class consists of a set of typedefs, a pair of allocation functions, allocate() and deallocate() and a pair of construction/destruction members, construct() and destroy().

Vector & deque container provides random access iterators.

Every container must define an iterator type. Iterators allow algorithms to iterate over the container’s contents.

List containers are implemented as doubly-linked lists. Doubly linked lists can store each of the elements they contain in different and unrelated storage locations.

A container adapter provides a restricted interface to a container.In particular, adapters do not provide iterators; they are intended to be used only through their specialized interfaces.

Sequence adaptor provides interface to sequence container.

Associative containers refer to a group of class templates in the standard library of the C++ programming language that implements ordered associative arrays.

 Bi-directional iterator are used to move in both direction from any element i.e. both forward and backward movements are allowed.

There are three main iterators needed for designing a container. They are const iterator, Reverse iterator and Iterator traits.

There are two type of container classes in c++. They are value containers and reference containers.

Heterogeneous container is the name of the container which contains group of multiple objects.

    #include <vector> 
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    using namespace std;
    int square(int i) { return i * i; }
    int main()
    {
        vector<int> V, V2;
        V.push_back(0);
        V.push_back(1);
        V.push_back(2);
        transform(V.begin(), V.end(), back_inserter(V2), square);
        copy(V2.begin(), V2.end(), ostream_iterator<int>(cout, " "));
        cout << endl;
    }

In this program, We formed an algorithm to find the square of the given number.

Sequence Containers arrays are an alternative for C-like arrays. It is a static continuous array that uses template classes with extended features for array implementation.

Both at() and get() function are used to access the elements stored at i’th position of the array.

v.rbegin() returns itertor of reverse iterator therefore cannot be stored in const_iterator(type mismatch). Similarly v.begin() returns normal iterator therefore cannot be stored in reverse_iterator and v.cbegin() returns the const_iterator therefore cannot be stored in normal iterator.

Once the vector is full i.e. number of elements in the vector becomes equal to the capacity of the vector then vector doubles its capacity i.e. if previous capacity was 2 then new capacity becomes 2 * 2 = 4 or 2 + 2 = 4.

resize() function is used to resize a vector container. It updates the size of vector and removes all the elements after n if new size(n) is less than previous size. Hence in the program initially the vector has 5 elements but after resizing the vector to 4 it has only 4 elements as 5 is removed.

Initially we have 5 elements in the vector therefore the capacity of the vector is 8(one can observe that as capacity doubles after vector is full). Now the function shrink_to_fit() makes the capacity of vector equal to its size hence removing the extra space occupied by the vector. Therefore as only 5 elements were there in the vectore therefore the capacity becomes 8.

In this program reserve(n) function is used which is used to reserve the space for n elements in vector. Hence when the reserve(50) function is called for vector v then the we are trying to reserve memory for 50 elements, hence the capacity of vector v becomes 50.

原文地址:https://www.cnblogs.com/hhlys/p/13503409.html