Classes member qualifiers
Download
1 / 11

Classes Member Qualifiers - PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Classes Member Qualifiers' - chaim


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Classes member qualifiers l.jpg

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 l.jpg
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 l.jpg
    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 l.jpg
    Nested Classes 

    • A Class Contained in Another Class

    • Multiple Levels of Nesting Are Allowed

    • Usual Scoping Rules Apply


    Nested classes example l.jpg
    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 example6 l.jpg
    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 l.jpg
    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 l.jpg
    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 l.jpg
    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 l.jpg
    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 l.jpg
    Acknowledgements

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