Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //45_MemberInitializers
- #include "SimpleClass.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- SimpleClass myObject (404, 808);
- myObject.print();
- }
- //------------------------------------------------------------------------------------
- #ifndef SIMPLECLASS_H
- #define SIMPLECLASS_H
- class SimpleClass
- {
- public:
- SimpleClass(int a, int b);//constructor that takes two values
- //assign one to a reg variable and
- //one to a constant variable
- void print ();//function to print it out
- protected:
- private:
- int regVar;
- const int constVar;
- };
- #endif // SIMPLECLASS_H
- //------------------------------------------------------------------------------------
- #include "SimpleClass.h"
- #include <iostream>
- using namespace std;
- //constant members need member syntax to be initialized
- SimpleClass::SimpleClass(int a, int b) //
- : regVar (a), //here is the special syntax (begins with ':') !!!member initialize syntax!!!
- constVar(b)//and it lays between ')' and '{' !!!don't put ';' after
- {
- /*
- you don't write any of your code when initializing variables in curly braces
- */
- }
- void SimpleClass::print(){//i forgot to place '::' for the first time
- cout<<"Reg_var is: "<<regVar<<" and const var is "<<constVar<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment