120 likes | 243 Vues
This resource provides an in-depth examination of member functions in C++, detailing inline functions, static member functions, const member functions, nested classes, and constant data members. Each section includes code examples that illustrate the concepts of implicit and explicit inline functions, the use of nested classes, and the principle of least privilege concerning constant members and functions. It is designed for students and developers seeking to enhance their understanding of C++ class structures and member functionalities.
E N D
Department of Computer and Information Science,School of Science, IUPUI ClassesMember Qualifiers CSCI 240 Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu
Member Functions -- Qualifiers • inline Member Functions • Function Bodies are Substituted for the Function Call At Compile Time • All the Member Functions Defined within the Class Definition are Implicitly Inline • If Defined Outside the Class Definition, inline Qualifier is Required • Faster Execution • static Member Functions • Exist Before Any Class Instantiation -- Access Only the Static Class Members • const Member Functions • CANNOT Change Data Members • The ONLY ONE that can Process Constant Objects • Can also Process Non-Constant Objects
inline Member Functions -- Example • class student{ int ss, credits; public: int get_ss(){return ss;} //Implicitly "inline" inline int get_credits(); //Explicitly "inline" }; • //Explicitly "inline" int student::get_credits() {return credits;}
Nested Classes • A Class Contained in Another Class • Multiple Levels of Nesting Are Allowed • Usual Scoping Rules Apply
Nested Classes -- Example • class one{ //Class "two" is nested inside the class "one" int a; public: class two{ int b; public: two(){b = 10;} void print_b() {cout << "two's b: " << b << endl;} }; two one_two; • one(){a = 100;} void print_a() {cout << "one's a: " << a << endl;} };
Nested Classes -- Example • main(){ one s1; //s1 is an object of "one" s1.print_a(); //a = 100 s1.one_two.print_b(); //b = 10 • //Object of class "two" defined inside "one" one::two s2; s2.print_b(); //b = 10 }
Constant Data Members • Principle of least privilege • Only give objects permissions they need, no more • Keyword const • Specify that an object is not modifiable • Any attempt to modify the object is a syntax error • Example const Time noon( 12, 0, 0 ); • Declares a const object noon of class Time and initializes it to 12
Constant Member Functions • const objects require const functions • Member functions declared const cannot modify their object • const must be specified in function prototype and definition • Prototype: ReturnType FunctionName(param1,param2…) const; • Definition: ReturnType FunctionName(param1,param2…) const { …} • Example: int A::getValue() const { return privateDataMember }; • Returns the value of a data member but doesn’t modify anything so is declared const • Constructors / Destructors cannot be const • They need to initialize variables, therefore modifying them
Constant Members and Functions • Member initializer syntax • All data members can be initialized using member initializer syntax • constructor for Increment is modified as follows: Increment::Increment( int c, int i ) : increment( i ) { count = c; } • : increment( i ) initializes increment to i • consts and references must be initialized using member initializer syntax • Multiple member initializers • Use comma-separated list after the colon
const -- Example • class student{ int ss, credits; public: student():ss(0), credits(0){} //const member function cannot modify the data member int get_ss() const {return ss;} int get_credits(){return credits;} }; • main(){ student s1; const student s2; s1.get_ss(); s1.get_credits(); s2.get_ss(); //constant object s2.get_credits(); //ERROR!!! Cannot call a non-constant function }
Acknowledgements • These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.