Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //44_constObjects
- #include "NewClass.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- NewClass myObject;
- myObject.regularFunction();
- const NewClass constObj; // keyword 'const' before you create object type (class) and the name of the object
- constObj.constFunction();
- return 0;
- }
- //use const (keyword) whenever you create an object
- //whenever you have const object you can't call it with regular function
- //const object's needs const objects
- //------------------------------------------------------------------------------------
- #ifndef NEWCLASS_H
- #define NEWCLASS_H
- class NewClass
- {
- public:
- NewClass();
- void regularFunction();
- void constFunction () const;// keyword 'const' after ( ) braces
- };
- #endif // NEWCLASS_H
- //------------------------------------------------------------------------------------
- #include "NewClass.h"
- #include <iostream>
- using namespace std;
- NewClass::NewClass()
- {
- }
- void NewClass::regularFunction(){
- cout<<"This is regular function"<<endl;
- }
- void NewClass::constFunction () const{// keyword 'const' after ( ) braces and before curly
- cout<<"This is what const functiont print"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment