Felanpro

Const Objects

Apr 1st, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. Constant objects are objects that can only call constant functions from a class.
  2.  
  3. Here is one example:
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. class Sally
  10. {
  11.     public:
  12.     void printCrap(); //Prototyping the function.
  13.     void printCrap2() const; //Prototyping the constant function.
  14.  
  15. };
  16.  
  17. int main()
  18. {
  19.     Sally sallyobject; //Creating an object.
  20.     sallyobject.printCrap(); //Calling a function from the class "Sally"
  21.  
  22.     const Sally constsallyobject; //Creating a const object.
  23.     constsallyobject.printCrap2(); //Calling a const function.
  24.  
  25.     return 0;
  26. }
  27.  
  28. void Sally::printCrap() //Creating a function to the class "Sally"
  29. {
  30.     cout << "I am a function!" << endl;
  31. }
  32.  
  33. void Sally::printCrap2() const //Creating a const function.
  34. {
  35.     cout << "I am a constant function!" << endl;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment