C++ Cheat Sheet

1) If a class has a virtual function and we create an instance of the derived class without the pointer then there will be unresolved dependencies.

2) We have to use the:

shape *s = new shape();
In the above statement shape() calls the constructor.
If the values are defined by the constructor then the values should be given the constructor shape(10,10), etc.

3) If the virtual function is not defined in the derived class then the base class's virtual function is used. 


4) If the inheritance is used then pointer type as well as normal variable type both should be used.


5) If the constructor with parameters is used then the default constructor should also be used.


6) If the constructor is declared in the private section then it cannot be accessed. 


7) If you try to create an instance of the abstract class then the program will not compile.


8) Creating abstract class

 class A
{
     public:
     virtual void display() = 0;
};


9) If the derived class doesn't define the virtual function then it will become abstract class too.

If we try to instantiate it then it will give compile time error.

10) String class of C++
In C++ you don't need to implement default functions like finding its length, finding a specific character, replacing characters etc. String class is a library that comes with a variety of functions. Following is some code. Each function is followed by its description. 
string s("BB");
string r = "R";
//copy, cbegin, cend, crbegin, crend, c_str, 
//get_allocator, max_size, npos, rbegin, rend, 
//replace, reserve, shrink_to_find, substr, swap
      
s.append(1,'a'); //1 specifies that 'a' to be appended once
      
s.assign(1,'b'); //Replaces entire string "BB" to B. 1 over here specifies that it will only be assigned only once.
      
char ch = s.at(2); //It specifies which character is availabe at location 2
      
ch  = s.back(); //It specifies that the character present at the last location
string::iterator it; //Iterator
it = s.begin(); //begin and end function both are used with the iterator
it = s.end();
      

int i = s.capacity(); //It specifies the characters that can fit into that string. Remember capacity concept of vector or dynamic array
      
s.clear(); //It will delete all the characters from the string but not the string.
      
i = s.compare(s); //It compares 2 strings. If equal then it returns 0 and o/w returns -1
      
string s1 = s.data(); //It will copy one string to another
      
i = s.empty(); //It will tell you if the string is empty or not. If the string is empty it will return 1 else 0.
      
s.erase(); //It is brother of clear. It will erase all the characters from the string.
      
i = s.find('a'); //It will find the location of a specific character in the string.
      
i = s.find_first_not_of('B'); //It will find the location where the character is not 'B' after the first occurence of 'B'
//In the string the above function will return 1. Since 'h' is first character where 'B' is not appearing

i = s.find_first_of('B');

i = s.find_last_not_of('B'); //It starts from the end

i = s.find_last_of('B');

ch = s.front(); //It gives the first character appearing the string

s.insert(1,1,'b'); //It will insert the character b once in the string at location 1
      
i = s.length(); //Doesn't count the NULL as the strings of C++ are not null-terminated

s.pop_back(); //It will pop the last character from the string and decrese the string length

s.push_back('a'); //Adds a character at the end of the string

s.resize(20); //It will increase its length and simultaneoulsy the capacity will be increased too
      
i = s.rfind('B'); //Finds the string but starts from the end.
      
i = s.size(); //It gives the size of the string

11) Deque: It is another data structure of C++ that has some defined functions. It is a double ended queue where insertion and deletion are allowed from both the sides. Following code snippet explains different functions.

deque d;
deque::iterator it;d.assign(1,10); //It will allocate a deque of size 1 and value 10
 
d.get_allocator().allocate(5); //It will allocate the space of 5 variables
 
d.erase(d.begin() + 1); //It will erase the data present at location 1
 
d.insert(d.begin()+1, 100); //It will insert the data at the location v.begin()+1
 
d.assign(2,20); //It will increase the size to 2 and both places will have value 20s
d.assign(3,30);
 
int i = d.at(0); //Outputs the number present at that location
 
i = d.back(); //Gives the last number as output
 
//d.clear(); //Cleares the contents of the deque
 
i = d.empty(); //It returns 0 if the deque is empty or else 1
 
i  = d.front(); //It will outputs the value present at the front
 
i = d.max_size(); //Gives the max size that the deque could occupy
 
d.pop_back(); //deletes the last element
 
d.pop_front(); //deletes the front element
 
d.push_back(10); //Adds an element to the front of the queue
 
d.push_front(10); //Adds an element to the end of the queue
 
d.resize(10);//Resizes the deque
 
i = d.size(); //Gives the size of the queue
 
12) Void Pointer: It is a generic pointer that can point to an object or memory location of any data type. 
void *p;
char c = 'c';
int i = 10;

p = &i; //void pointer p pointer to an int type now. We can’t just deference it. We need another pointer that will point to it and then that pointer can be dereferenced.
int *p1; //Some other int pointer
p1 = (int*)p; //Another int pointer now points to the address the pointed by void pointer
 
p = &c; //void pointer now points to the char type
 
char *c1; //Some other char pointer
c1 = (char*)p; //Another char pointer points to the address pointed by the void pointer
//Hence we can use the void pointer to point to different data types now.
 
cout << *p1 << endl;
cout << *c1 << endl;

13) List class in C++. List class is internally implemented as DLL. It allows insertion and deletion from both sides of the list in O(n) time. 


list l; //Creating a list
    
 list l; //Creating a list
list::iterator it; //Iterator for the list
 
it = l.begin(); //Initializing the iterator
 
for(i = l.begin();i!=l.end();i++) //Traversing the list
       cout << *i << endl;

      
//Opertions: pop_back, pop_front, push_back, push_front, insert, 
//assign, back, empty, end, erase, front, resize, reverse, 
//sort, swap, unique, splice.

14) Vector class in C++. It is similar to arrays in C++ but they can change size dynamically. Arrays are constant in size that doesn't change size once its capacity is reached. 
vector v;
vector v; //Vector
 v.push_back(20); //Adding data to the vector
 v.push_back(10);
 v.push_back(30);

 vector::iterator it; //Iterator for the vector

 for(it=v.begin();it!=v.end();it++) //Moving from start to end for the vector
 {
  cout << *it << endl;
 }


No comments:

Post a Comment

NoSQL

This one is reviewed but I need to delete its copy from hubpages or somewhere NoSQL Data models: key-value  Aggregate model.  key or i...