1. "iostream.h" or "iostream"?
"iostream.h" library was deprecated. "iostream" components are declared in namespace std. "iostream" contains a set of templatized I/O classes which support both narrow and wide characters.
"iostream.h" or "iostream"?
enumerator operator++(int){
enumerator tmp = *this; // Temporary object (copyctor is called here)
go_up();
return tmp; // Good chances of another copyctor call
}
Pre-increment is much simpler and always faster:
enumerator& operator++() {
go_up();
return *this;
}
3. const and pointers
What's the difference between
"const Fred* p",
"Fred* const p" and
"const Fred* const p"?
NOTE: You have to read pointer declarations right-to-left.
const Fred* p means "p points to a Fred that is const" � that is, the Fred object can't be changed via p.
Fred* const p means "p is a const pointer to a Fred" � that is, you can change the Fred object via p, but you can't change the pointer p itself.
const Fred* const p means "p is a const pointer to a const Fred" � that is, you can't change the pointer p itself, nor can you change the Fred object via p.
4. A class defines a data type, much like a struct would be in C. In a computer science sense, a type consists of both a set of states and a set of operations which transition between those states.
5. What is encapsulation? Preventing unauthorized access to some piece of information or functionality.
6. A square bracket indicates inclusion of the endpoint, a round
bracket indicates exclusion of the endpoint.
7. Handle self-assignment in assignment operators.
Fred& Fred::operator= (const Fred& f){
if (this == &f) return *this; // Gracefully handle self assignment
// Put the normal assignment duties here...
return *this;
}
Fred operator+ (const Fred& x, const Fred& y);
Fred operator* (const Fred& x, const Fred& y);
8.By overloading standard operators on a class, you can exploit the intuition of the users of that class.
9. How can I get std::cin to skip invalid input characters? Use std::cin.clear() and std::cin.ignore().
10. Using std::endl flushes the output buffer. If you don't need to flush the buffer, the code will run faster if you use '\n'.
11. Funcation template
#include"iostream"
/*
When the compiler detects a printArray fn invocation in the code, the type of printArray's first argument
is substituted for T throughout the template defination and C++ creates a complete template function for
printing an array of the specified data type. Then, the newly created function is compiled.
*/
template < class T >
void printArray(const T* array, const int iSize)
{
std::cout<<"\nArray content:\n";
for(int i=0; i < iSize; i++)
std::cout << array[i]<<" ";
}
int main()
{
const int aCount=3, bCount=4, cCount=3;
int a[aCount] = {1,2,3};
double b[bCount] = {1.1, 2.2, 3.3, 4.4};
char c[cCount] = "Go";
printArray(a, aCount);
printArray(b, bCount);
printArray(c, cCount);
return 0;
}
14. When have references or const member variables, the compiler can not generate copy c'tor or assignment operator. Gives compiler error.
std::string& m_str;
const int m_i;
error C2582: 'ConstStringMember' : 'operator =' function is unavailable
15. mutable
This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.
16. Reverse linked list:
void reverseList() {
cout<<"\nrevList:";
node* p1=NULL;
node* cur=m_head;
node* next;
while(cur!=NULL) {
next=cur->m_next; //save the link to the next element, as p2's link is going to be broken
cur->m_next = p1;
p1=cur;
cur=next;
}
m_head=p1;
}
17. delete this: is valid, but nothing should be used of the object after that. When object want to commit suicide.
class B{};
class C{
int ivar2;
};
void main(){
B b;
int isizeb= sizeof(b); //Ans: 1 byte
C c1;
printf("\n%d",sizeof(c1)); //Ans: 4 bytes
}
The member functions themselves are never stored in objects; objects
only store data members, either the ones you declared or the vptr (if
the class has virtual member functions). An empty class must occupy at
least 1 byte for a good reason: unique address for every object.
C c[100];
another link
But if you derive from class empty, then the size of class empty within the
entire class may be zero. This is called the empty base optimization .
struct S : public empty {
int i;
};
cout << sizeof(int); // prints 4 on my platform
cout << sizeof(empty); // prints 1 on my platform
cout << sizeof(S); // prints 4 on my platform, not 5 or 8