19 C++ hints

Default featured post

This post probably is one of the longest post in this blog. The truth is that I wrote this document for me to remind my C++ knowledge and in the middle of writing this document I thought about add it in blog. It is not completed yet since I have not finished my review. I have tried (hard) to find 20th hint but I was tired so I just posted it like this. I try to complete it and add more hints in another post.

In C++ you cannot initial variables when you define and inside of the class like below,

class Test {
int x=10; //Compile Error
}
view raw test.cpp hosted with ❤ by GitHub

Therefore the best way to initial value is either inside constructor or in the method, like,

class Test {
protected:
private:
public:
int x;
Test() {
x = 10;
}
MyMethod() {
x = 10;
}
}
view raw test.cpp hosted with ❤ by GitHub

It is also possible to define the constructor and method outside of the class like,

class Test {
protected:
private:
public:
int x;
Test();
MyMethod();
};
Test::Test() : x(10) { }
// or
Test::Test() {
x = 10;
}
int Test::MyMethod() {
x = 10;
}
// or
int Test::MyMethod(): x(10) { }
view raw test.cpp hosted with ❤ by GitHub

If we have static member we can initialize it as follow:

class Test {
protected:
private:
public:
static int x;
};
Test::x = 10;
view raw test.cpp hosted with ❤ by GitHub

Making instance of the class in the main can be like follow:

int main(int argc, char* argv[]) {
Test t1;
return 0;
}
view raw test.cpp hosted with ❤ by GitHub

Some hints are important to know:

Hint1: After defining class you have to put semicolon.

Hint2: For private, protected, and public methods plus member you have to define them like public: after method or member definition.

Hint3: Initializing member should be either in the method or in front of defining method like : x(10).

Hint4: Defining method outside of class should be like return type class name ::method name()".

Hint5: Static member can be directly initialize outside of the class like type class name::member=value;.

Hint6: Making instance of class does not have new or parentheses it is just like class name new name;".

Hint7: If class has more than one constructor and some get value then those which get value from users can have parentheses like Test t1(10);.

Hint8: In C++ we have destructor which is defined with ~class name();. It cannot have arguments as well.

Use of types like,

void some_func() {
Point p1 = Point(0, 0);
// p1 is automatically freed
}
void some_func2() {
Point *p1 = new Point(0, 0);
delete p1;
// there's a memory leak unless p1 gets deleted
}
view raw test.cpp hosted with ❤ by GitHub

First one destructed automatically and it’s created on stack which means that just it can be accessed inside function. But using new causes the type won’t be destroys until we use delete word and it’s created on heap and can be accessed in whole application.

If we want to use new for the class we can define it as follow:

int main(int argc, char *argv[]) {
Test *t1 = new Test();
t1 -> print();
delete t1;
return 0;
}
view raw test.cpp hosted with ❤ by GitHub

Hint9: Defining new class can be done with the help of pointers.

Hint10: Defining a class with use of new is exactly like java means class name *newname=new class name();.

Hint11: When we define a class with the use of new we cannot access to method or type with the use of ., instead we can use ->.

Hint12: Never forget to delete object which is created with using new (delete t1;).

Even variables can be defined with using new like,

https://gist.github.com/

Hint13: In C++ 1999 and 2003 we cannot initialize variable in the body of class but in C++ 2011 or 11 it’s possible.

Pointer can be defined as follow:

int main(int argc, char *argv[]) {
int *p;
int x = 20;
int y = 10;
p = x; // compile error
p = &x;
int *pp = &y; // pp points to y, *pp value is equal to 10 and pp holds y address
int **ppp; // making pointer which points to another pointer
cout << *p << endl; // prints 20
ppp = &p;
cout << *ppp << endl; // prints 20
cout << p << endl; // prints x address
cout << &p << endl; // prints p address
p++; // increases current p address 2 bytes (ints)
*p++; // increases the value of x
++*p; // increases the value of x
*p = y; // value of y will be copied to p, so x will be changed to 10
*p = &y; // address of y will be copied to p, so x will be changed to y address
p = &y; // p points to y
return 0;
}
view raw test.cpp hosted with ❤ by GitHub

Pointer to a class can be created as following,

int main(int argc, char *argv[]) {
Test t1;
Test *t2;
t2 = &t1;
t2 -> x = 100;
cout << t2 -> x << endl; // prints 100
cout << t1.x << endl; // prints 100
return 0;
}
view raw test.cpp hosted with ❤ by GitHub

Hint14: It’s highly recommended used pointer(variable)=0 (my_int = 0) after deleting objects which are created with using new.

Hint15: Using this is the same as java with one difference and it’s that in C++ this should be used with -> instead of ., like this->x=x;.

Hint16: Classes also can be defined in the form of array like, Test t1[10];.

We can access array through pointer like:

int main(int argc, char *argv[]) {
int x[3] = {1, 2, 3};
int *p;
p=x;
cout << *p; //prints x[0];
cout << ++*p; //prints x[1];
cout << p[1]; //prints x[1];
cout << *x; //prints x[0];
cout << ++*x; //prints x[1];
cout << *++x; //Compile error;
cout << *x++; //Compile error;
p = &x; //Compile error;
p = &x[0]; //p points to first element of x;
cout << *p; //prints x[0];
cout << ++*p; //prints x[1];
cout << p[1]; //prints x[1];
cout << *++p; //prints x[1];
cout << *(++p); //prints x[1];
cout << *(p++); //prints x[0];
return 0;
}
view raw test.cpp hosted with ❤ by GitHub

Creating dynamic array,

void test() {
int x = 5;
int *y = new int[x];
int *z = new int[10];
delete[] y;
delete[] z;
}
view raw test.cpp hosted with ❤ by GitHub

Hint17: For getting total memory which an array uses we can utilize sizeof(array variable). If it is used on char *x = "hello";, it returns the length of x with including /0 character, in this example means six. This function also can return the size of struct or variable.

Hint18: For acquiring size of string, is better to use strlen(string name);. The following code consists some common string functions.

#include<cstring> //In C++ with C++ style;
#include<cstdlib> //Includes atoi function;
int main(int argc, char *argv[]) {
string *s1;
string *s2;
int z;
strlen(s1); // returns length of s1
strcat(s1,s2); // concatenate s2 to end of s1, s1 then will hold s1+s2
strcpy(s1,s2); // copy content of s2 into s1, then s2==s1
strncpy(s1,s2,X); // copy X first character of s2 into s1
z=atoi(s1); // converts string s1 to int z, atof is used for converting to double and atol is used for converting to long. If string does not have any number then it returns 0
return 0;
}
view raw test.cpp hosted with ❤ by GitHub

Hint19: If we want one variable has more than one name then we can make reference to it like below,

void test() {
int x;
int& rx=x; // makes reference to x
rx++; // increases the value of x
}
view raw test.cpp hosted with ❤ by GitHub

Referencing can be another way of changing the value of variables when we call function instead of using pointer. Following examples compare referencing and pointers.

// referencing example
void decrement(int& x) {
x–;
}
view raw test.cpp hosted with ❤ by GitHub

You can then use this function as follows:

void test() {
int a = 5;
decrement(a);
cout << a << endl; // prints 4;
}
//////////////////////////////////
// pointer example
void decrement(int *x) {
x–;
}
view raw test.cpp hosted with ❤ by GitHub

You can then use this function as follows:

void test() {
int a = 5;
decrement(&a);
cout << a << endl; // prints 4
}
view raw test.cpp hosted with ❤ by GitHub