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,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Therefore the best way to initial value is either inside constructor or in the method, like,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It is also possible to define the constructor and method outside of the class like,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If we have static member we can initialize it as follow:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Making instance of the class in the main can be like follow:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Hint19: If we want one variable has more than one name then we can make reference to it like below,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We use cookies to optimize our website and our service.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.