Tutorial - C / C++ - Class structure |
|
| Author | Ian - Tutorial Posts = 62 |
| This is a general tutorial about the class structure of programming languages. The class is basically an object that allows public methods (accessed outside that class object) and private methods (the internal class logic) and protected with is the same as private apart from an inherited class can access the protected objects. Basically the 3 main types Public : Access from outside the class, and internal of course, with inherited/friends etc. Protected : Access from inherited/friends of the class, but not called from the class object. Private : Access from only the class and friends of the class. For example within C++ coding ... #include <iostream> using namespace std; class example1 { public: // all are of type public below void example1(); // constructor ~example1(); // destructor int addTwo(int a, int b) { value1 = a; // set the internal values; value2 = b; value3 = a + b; return (value1+value2); } int returnValue1() { return value1; // return the internal value 1 } protected : // all the types are protected int value3; private : // all the types are private int value1; // these values can only be access from internal functions int value2; }; // the inherited class class example2 : public example1 { public: int returnValue2() { return value2; // error cannot access the private from example1 } int returnValue3() { return value3; } }; int main(void) { example1 ex = example1(); cout << ex.value1; // error no accessible. example2 ex2 = example2(); // of course the values returned will be 0 because example2 class only inherits from class1 // and is not connected, only for demo'ing cout << ex2.returnValue3(); // access to the value3 in example is valid cout << ex2.returnValue2(); // access to the value2 is not. return 0; } |
|
| Copyright@CodingFriends, 2005-2006. All Rights Reserved. | |
| Home | Forums | Tutorials | Users | |
