JD의 블로그

[C++ summary ] 본문

-프로그래밍 언어/C++ language

[C++ summary ]

GDong 2019. 9. 16. 00:19

Arrays : a collection of similar data elements 

 

The main memory is divided into three sections. 

1. Code section

2. Stack

3. Heap

 

The array will be created inside the stack. 

Tht will be directly accessible to the main function.

ex)

int a[5];

 

 

 

 

 

 

 

 

[Initializer of array] 

int b[5] = {2, 4, 6, 8, 10};

 

Structures : a collection of data members 

 

Pointer : an address variable stroing address of data 

pointer takes sizeof(int) as memory size. 

program can directly access "code section" and "stack", but not automatically access heap. 

Therefore, heap memory is external to the program.

1.One of the reason of using pointer is accessing heap memory.

if the files are on the hard disk, for accessing a file,be program cannot access this hard disk files directly 

because hard disk is external or files are external to a program, so far that, it needs a pointer for accessing and that pointer should be a file. 

 

2.Another main reason to use pointer is accessing the resources which are outside the program like I/O.

3. Parameter passing.

 

Whenever you declare it will be inside stack.

To get into heap, in C language you have to use malloc().

malloc take the size, how much memory you want to use.

#include <stdlib.h>

int *p; 

p= (int *)malloc(5 * sizeof(int));  # the size of memory is decided by the compiler. 

malloc() function returns void pointer, so we have to type-caste it 

 

In C++, we have to say "new"

p = new int[5];

 

Reference (only in C++) : a nickname given to a variable or arraies. 

useful in parameter passing 

 

Pointer to Structure

(*p).length = 20; 

because higher recedence is for dot operation 

or p->length = 20;

 

Function : a piece of code which performs a specific task

function is a group of related instructions which perform a specific task.

 

z = add(x. y) -> actual parameter

int add(int a, int b) => formal parameter

 

Parameter passing method

1. Pass by value (call by value)

2. Call by address : using pointers 

3. Call by reference (only in C++)

- References doesn't take any memory!

 

Array as Parameter

we should also pass what is the size of the array

Arrays can be passed only by address.

int p[] fun(int n)  =>it means it is returning an array

int * fun(int n) => this is also possible form

 

Structure as Parameter

 

Structures and Functions

The main function is not having any instructions of its own but only calling funtions 

 

Template

Generic functions are template functions

Generic classes are template classes 

C++ says that you can use the same class for multiple data types.

so for any type of data it is called as generic class that is defined as a template 

 

template<class T>

class Arithmetic

{

private:

T a;

T b;

public:

 Arithmetic(T a , T b);

T add();

T sub();

};

 

template<class T>

Arithmetic<T>::Arithmetic(T a, T b)

{

this->a=a;

this->b=b;

}