티스토리 뷰

배움/C++

Summary Note to C++

Ryan C++ 2016. 9. 3. 15:01


canonical header file(4)

default constructor, copy constructor, destructor, and assignment operator



* OO


Which of the options below is the best operator declaration to add and assign two objects of type Complex(Complex+=Complex)? 

  1.  void operator += (const Complex& c);
  2.  Complex operator += (const Complex& c);
  3.  Complex& operator += (const Complex& c);
  4.  Complex& operator += (const Complex& c) const;


* MEMORY


heap (free pile : dynamic memory) & stack (neat pile)

http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/


<heap>


int* pi; // declaration of a pointer variable of int type

pi = new int; // allocation of memory for the pointer variable of int, by using "new <type>"


OR

int* pi = new int;


delete pi; // deallocation



Point* pp;


pp = new Point;

pp->setX(10.0);

delete pp;


pp = new Point(10, 30);

delete pp;


pp = new Point[10];

delete[] pp;



Which of the following options declares and allocates an array of int pointers?

  1.  int*[] array=new int*[size];
  2.  int[] array=new int*[size];
  3.  int** array=new int*[size];
  4.  int* array=new int*[size];


Which statement is false about memory?

  The size of an array on the stack can only be determined at compile time.

  When using new to allocate memory, you need to pass the number of bytes to allocate.

  new and malloc allocate memory on the heap.

  Local variables are allocated on the stack.


    int size=3; 

    int* a=new int[size];

    for (int i=0; i<size; i++) { a[i]=10-i; }

    std::cout << a[1] << ", " << *a << ", " << (a+1)[0] << " , " << *a+1 << std::endl;

    delete[] a;


    9,10,9,11





    * namespace


    namespace NAMET

    {

        double pi = 3.141592653; 

        double func(double);  // declare

    }

    void foo(){}// The rest of the namespace, it is split


    double NAMET::func(double x){ return x * 2.0;} // define



    It's called an unnamed namespace / anonymous namespace. Its use is to make functions/objects/etc accessible only within that file. It's almost the same as static in C.




    Which of the options below is the best operator declaration to add a two objects of typeComplex (Complex+Complex)?

    1.  Complex operator + (const Complex& c);
    2.  Complex& operator + (const Complex& c1, const Complex& c2) const;
    3.  Complex& operator + (const Complex& c) const;
    4.  Complex operator + (const Complex& c) const;


    댓글
    공지사항
    최근에 올라온 글
    최근에 달린 댓글
    Total
    Today
    Yesterday
    TAG
    more
    «   2024/04   »
    1 2 3 4 5 6
    7 8 9 10 11 12 13
    14 15 16 17 18 19 20
    21 22 23 24 25 26 27
    28 29 30
    글 보관함