1 / 11

Classes Member Qualifiers

Department of Computer and Information Science, School of Science, IUPUI. Classes Member Qualifiers. CSCI 240. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Member Functions -- Qualifiers . inline Member Functions

toyah
Download Presentation

Classes Member Qualifiers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  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

  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;}

  4. Nested Classes  • A Class Contained in Another Class • Multiple Levels of Nesting Are Allowed • Usual Scoping Rules Apply

  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;} };

  6. 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 }

  7. 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

  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

  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

  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 }

  11. Acknowledgements • These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.

More Related