Created
July 5, 2015 11:04
-
-
Save krysseltillada/dd69238705598153e14a to your computer and use it in GitHub Desktop.
non_const && const member functions
This file contains hidden or 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
| #include <iostream> /// std::cout; std::endl; | |
| #include <string> /// std::string; | |
| struct race | |
| { | |
| void non_const_display() /// for non const just for to differentiate the two functions to understand | |
| { | |
| std::cout << "class: " << this->Class << std::endl | |
| << "job: " << this->job << std::endl | |
| << "level: " << this->level << std::endl | |
| << "str: " << this->str << std::endl | |
| << "agi: " << this->agi << std::endl | |
| << "Int: " << this->Int << std::endl; | |
| } | |
| void const_display() const /// we used a const member function to call a const object | |
| { | |
| std::cout << "class: " << this->Class << std::endl | |
| << "job: " << this->job << std::endl | |
| << "level: " << this->level << std::endl | |
| << "str: " << this->str << std::endl | |
| << "agi: " << this->agi << std::endl | |
| << "Int: " << this->Int << std::endl; | |
| } | |
| race (std::string cl = "human", std::string j = "swordsman", int l = 23, int s = 22, int ag = 100, int i = 22) | |
| : Class(cl), job(j), level(l), str(s), agi(ag), Int(i) { /// shortest way to initialize -> like class = cl; | |
| } | |
| std::string Class, job; | |
| int level, str, agi, Int; | |
| }; | |
| int main() | |
| { | |
| const race minu; /// we declared a const race minu | |
| minu.const_display(); /// we cant either change it because it is pointer to const but we can read it | |
| std::cout << std::endl; | |
| race mina("fairy", "mage", 300, 22, 21, 1000); /// we declared and defined non const race mina | |
| mina.non_const_display(); /// here we can modify the data members inside the object mina and read it | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment