Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Dog
- {
- int age;
- string name;
- public:
- Dog() { age = 3; name = "dummy"; }
- // const parameters
- void setAge(const int& a) { age = a; cout << "Set age const" << endl; } // when the parater is const
- void setAge(int &a) { age = a; cout << "Set age non-const" << endl; /// when the parameters non-const
- }
- // const return value
- const string& getName() { return name; }
- // const function
- void printDogName() const // works when the object is non const
- {
- cout << name << " const " << endl;
- // age++; this doesn't work cause of const
- }
- void printDogName() // works when the object is non const
- {
- cout << name << " non-const " << endl;
- // age++; this doesn't work cause of const
- }
- };
- int main()
- {
- Dog d;
- const int age = 10;
- int age2 = 20;
- d.setAge(age);
- d.setAge(age2);
- d.printDogName();
- const Dog d2;
- d2.printDogName();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement