ya_zay4eg

//44_constObjects

Jun 26th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. //44_constObjects
  2. #include "NewClass.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. NewClass myObject;
  10. myObject.regularFunction();
  11.  
  12. const NewClass constObj; // keyword 'const' before you create object type (class) and the name of the object
  13. constObj.constFunction();
  14. return 0;
  15. }
  16.  
  17. //use const (keyword) whenever you create an object
  18. //whenever you have const object you can't call it with regular function
  19. //const object's needs const objects
  20. //------------------------------------------------------------------------------------
  21. #ifndef NEWCLASS_H
  22. #define NEWCLASS_H
  23.  
  24.  
  25. class NewClass
  26. {
  27.     public:
  28.         NewClass();
  29.         void regularFunction();
  30.         void constFunction () const;// keyword 'const' after  ( ) braces
  31. };
  32.  
  33. #endif // NEWCLASS_H
  34.  
  35. //------------------------------------------------------------------------------------
  36.  
  37. #include "NewClass.h"
  38. #include <iostream>
  39. using namespace std;
  40.  
  41. NewClass::NewClass()
  42. {
  43. }
  44. void NewClass::regularFunction(){
  45. cout<<"This is regular function"<<endl;
  46. }
  47.  
  48. void NewClass::constFunction () const{// keyword 'const' after  ( ) braces and before curly
  49. cout<<"This is what const functiont print"<<endl;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment