Class Declaration Definition class STack l/class declaration lf class definition body public: laccess control bool push(const string&); //member function declaration bool pop(string &) bool peek(string &) bool empty(; bool fullO: line automatically int size(i return m_stack size(; ) //declaration definition(in Class) private: ∥/ access control vector<string> m stack; //data member ; Class scope resolution /never to forget the semicolon bool STack; empty /member function definition (out i return m stack. empty 0; y of Class)
Class Declaration & Definition class CStack { public: bool push(const string&); bool pop(string &); bool peek(string &); bool empty(); bool full(); int size() { return m_stack.size(); } private: vector<string> m_stack; }; bool CStack::empty() { return m_stack.empty(); } //class declaration //{} class definition body //access control //member function declaration //declaration & definition (in Class) // access control //data member //never to forget the semicolon //member function definition (out of Class) inline automatically Class scope resolution
Class Declaration Definition(cont,) u Question: If class a has a pointer data member that pointing to class B, and vice versa, how to define class a and B? Answer: Using Forward Declaration class A. class B: class A public B* m pb; } class B public A m_ pa
Class Declaration & Definition (cont.) ◼ Question: If class A has a pointer data member that pointing to class B, and vice versa, how to define class A and B? ◼ Answer: Using Forward Declaration class A; class B; class A { public: B* m_pb; }; class B { public: A* m_pa; };
Class Declaration Definition(cont,) Forward Declaration only telling the name to the compiler Try your best to minimize the compilation dependencies between files The rules is, if the class declaration could work never to indlude the class definition For pointers, references, and using objects as parameters and eturn value in function declaration, we only need the class declaration STack pstack =0 void funCA(const STack&) a STack funcB(CStack) We could define a real class object or use the class members only after we know the class definition a STack stack int s=stack, size The declaration version of <iostream>--<iosfwd>
Class Declaration & Definition (cont.) ◼ Forward Declaration only telling the name to the compiler ◼ Try your best to minimize the compilation dependencies between files ◼ The rules is, if the class declaration could work, never to include the class definition ◼ For pointers, references, and using objects as parameters and return value in function declaration, we only need the class declaration ◼ CStack* pstack = 0; ◼ void funcA(const CStack&); ◼ CStack funcB(CStack); ◼ We could define a real class object or use the class members, only after we know the class definition ◼ CStack stack; ◼ int s = stack.size(); ◼ The declaration version of <iostream> -- <iosfwd>
Access control u Controlling how the member functions and data members could be accessed Public- could be accessed outside the class Protected-could only be accessed by those classes deriving from it, or by friend classes and functions Private- could only be accessed inner this class or by friend classes and functions Suggestion: Avoid exposing the data members in the public interface, Use accessing functions instead
Access control ◼ Controlling how the member functions and data members could be accessed ◼ Public – could be accessed outside the class ◼ Protected – could only be accessed by those classes deriving from it, or by friend classes and functions ◼ Private – could only be accessed inner this class, or by friend classes and functions ◼ Suggestion: Avoid exposing the data members in the public interface, Use accessing functions instead
Access control(cont class X class y public public int mf10 int m1. int mf20[ return mf30; 1 int get m20i return m2; 1 private: void set m2(int xim2=X;1 int mf30 private: int m2. }x; }y; void main( void maino x.mf10; llyes y. m1++; //Not suggested! Direct Xmf30;∥Eror! Private access data members member can't be accessed out yset_m2(1);/Suggested! The of the class data mem ber is accessed under control
Access control (cont.) class X { public: int mf1(); int mf2() { return mf3(); } private: int mf3(); } x; void main() { x.mf1(); //yes x.mf3(); //Error! Private member can’t be accessed out of the class } class Y { public: int _m1; int get_m2() { return _m2;} void set_m2(int x) { _m2 = x;} private: int _m2; } y; void main() { y._m1++; //Not suggested! Direct access data members y.set_m2(1); //Suggested! The data member is accessed under control }