Download presentation
Presentation is loading. Please wait.
1
Classes Member Qualifiers
Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Classes Member Qualifiers Dale Roberts, Lecturer Computer Science, IUPUI 8/15/2019
2
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 8/15/2019
3
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;} 8/15/2019
4
Nested Classes A Class Contained in Another Class
Multiple Levels of Nesting Are Allowed Usual Scoping Rules Apply 8/15/2019
5
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;} }; 8/15/2019
6
Nested Classes -- Example
main(){ one s1; //s1 is an object of "one" s1.print_a(); //a = s1.one_two.print_b(); //b = 10 //Object of class "two" defined inside "one" one::two s2; s2.print_b(); //b = 10 } 8/15/2019
7
Constant Data Members Principle of least privilege Keyword const
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 8/15/2019
8
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 8/15/2019
9
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 8/15/2019
10
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 } 8/15/2019
11
Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts. 8/15/2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.