Person을 Student로 상속하여 함수를 재정의한 코드
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 | #include <iostream> using namespace std; #define NAME_SIZE 50 class Person { int id; char name[NAME_SIZE]; public : void aboutMe(){ cout << "I am a person."; } }; class Student : public Person { public: void aboutMe(){ cout << "I am a student."; } }; int main(){ Student * p = new Student(); p->aboutMe(); delete p; return 0; } | cs |
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 31 32 33 34 35 36 37 | #include <iostream> using namespace std; #define NAME_SIZE 50 class Person { // abstract class int id; char name[NAME_SIZE]; public : virtual ~Person(){ cout << "Deleting a person." << endl; } virtual void aboutMe(){ cout << "I am a person." << endl; } virtual bool addCourse(string s) = 0; }; class Student : public Person { public: ~Student(){ cout << "Deleting a student." << endl; } void aboutMe(){ cout << "I am a student." << endl; } bool addCourse(string s){ cout << "added course " << 5 << " to student." << endl; return true; } }; int main(){ Student * p = new Student(); p->aboutMe(); p->addCourse("History"); delete p; return 0; } | cs |
Default Value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int func(int a, int b=3){ int x = a; int y = b; return a + b; } int main(){ int w = func(4); int z = func(4, 5); return 0; } | cs |
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main(){ int a = 5; int & b = a; b = 7; cout << a; return 0; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; int main(){ int * p = new int[2]; p[0] = 0; p[1] = 1; p++; cout << *p; // 1 return 0; } | cs |
CCI Interview Questions
12.1 Last K Lines : Write a method to print the last K lines of an input file using C++.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <fstream> #include <iostream> #include <string> using namespace std; void printFileLineK(int k){ string str[100]; int cntStr = 0; int cnt = 0; ifstream myfile ("myfile.txt"); if (myfile.is_open()) { while ( getline (myfile,str[cntStr++]) ){} while(cnt <k){ cout << str[(--cntStr) - 1] << endl; cnt++; } myfile.close(); } else cout << "Unable to open file"; } void writetFile(){ FILE * pf; pf = fopen ("myfile.txt", "wb"); int n; char name[100]; for (n=0 ; n<5 ; n++) { puts ("please, enter a name: "); gets (name); fprintf (pf, "Name %d [%s]\n",n+1,name); } fclose (pf); } int main () { int k = 3; writetFile(); printFileLineK(k); return 0; } | cs |
12.2 Reverse String : Implement a function void reverse(char * str) in C or C++ which reverses a null-terminated string.
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 | #include <stdio.h> #include <iostream> #include <string> using namespace std; void reverse(char * str){ char * reStr; int length = 0; int cnt = 0; char tmp; while(str[length] != NULL){length++;} reStr = new char[length - 1]; while(cnt < (length - cnt - 1)){ reStr[length - cnt - 1] = str[cnt]; reStr[cnt++] = str[length - cnt - 1]; } reStr[length] = NULL; cout << reStr << endl; } int main () { char * str = "soicem"; cout << str << endl; reverse(str); return 0; } | cs |
12.3 Hash Table vs STL Map : Compare and contrast a hash table and an STL map. How is a hash table implemented? If the number of inputs is small, which data structure options can be used instead of hash table?
STL Map inserts the key/value pairs into a binary search tree based on the keys. There is no need to handle collisions, and, since the tree is balanced, the insert and lookup time is guaranteed to be O(log n).
-What can be used instead of a hash table, if the number of inputs is small?
You can use an STL map or a binary tree. Although this takes O(log(n)) time, the number of inputs may be small enough to make this time negligible.
12.4 Virtual Functions: How do virtual functions work in C++?
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 | #include <stdio.h> #include <iostream> #include <string> using namespace std; class Shape { public: int edge_length; virtual int circumference(){ cout << "Circumference of Base Class\n"; return 0; } }; class Triangle: public Shape { public: int circumference(){ cout << "Circumference of Triangle Class\n"; return 3 * edge_length; } }; int main () { Shape * x = new Shape(); // static bind x->circumference(); Shape * y = new Triangle(); //dynamic bind y->circumference(); return 0; } | cs |
12.5 Shallow vs Deep Copy: What is the difference between deep copy and swallow copy? Explain how you would use each.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <stdio.h> #include <iostream> #include <string> using namespace std; struct Test { char * ptr; }; void shallow_copy(Test & src, Test & dest){ dest.ptr = src.ptr; } void deep_copy(Test & src, Test & dest){ dest.ptr = (char *)malloc(strlen(src.ptr) + 1); strcpy(dest.ptr, src.ptr); } int main () { return 0; } | cs |
Shallow_cpy may cause a lot of programming runtime error, especially with the creation and deletion of objects. Shallow copy should be used very carefully and only when a programmer really understands what he wants to do.
12.6 Volatile: What is the significance of the keyword "volatile" in C?
변수를 선언할 때 앞에 volatile을 붙이면 컴파일러는 해당 변수를 최적화에서 제외하여 항상 메모리에 접근하도록 만든다.
volatile int a = 10;
int volatile a = 10;
12.7 Virtual Base Class: Why does a destructor in base class need to be declared virtual?
12.8 Copy Node: Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed in data structure. The Node Data structure contains two pointers to other Nodes.
12.9 Smart Pointer: Write a smart pointer class. A smart pointer is a data type, usually implemented with templates, that simulates a pointer while also providing automatic garbage collection. It automatically counts the number of references to a SmartPointer<T*> object and frees the object of type T when the reference count hits zero.
12.10 Malloc: Write an aligned malloc and free function that supports allocating memory such that the memory address returned iis divisible by a specific power of two.
EXAMPLE
align_malloc(1000, 128) will return a memory address that is a multiple of 128 and that points to memory of size 1000 bytes.
aligned_free() will free memory allocated by align_malloc
12.11 2D Alloc: Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the number of calls to malloc and make sure that the memory is accessible by the notation arr[i][j].
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 31 | #include <stdio.h> #include <stdlib.h> int ** my2DAlloc(int rows, int cols){ int **rowPtr; int i; rowPtr = (int **) malloc(rows * sizeof(int *)); for(i = 0; i < rows; i++){ rowPtr[i] = (int *) malloc (cols * sizeof(int)); } return rowPtr; } void free2DAlloc(int ** rowPtr, int rows){ int i; for(i = 0; i < rows; i++) free(rowPtr[i]); free(rowPtr); } int main(){ int ** rowPtr; int row = 5; int col = 5; rowPtr = my2DAlloc(row, col); rowPtr[0][0] = 10; printf("%d\n", rowPtr[0][0]); free2DAlloc(rowPtr, row); return 0; } | cs |
'* Computer Science > C++' 카테고리의 다른 글
c++로 스플릿하기 (0) | 2018.09.27 |
---|---|
stack with template (0) | 2018.05.28 |
Combination & memset (0) | 2018.04.04 |
2차원 벡터 생성 (0) | 2018.03.30 |
vector pair 사용 (0) | 2018.03.22 |