C++ Classes & Member Functions: inline, static, const, Nested Classes, Constant Data, Slides of Computer Science

An overview of various member qualifiers in c++ classes, including inline functions, static functions, const functions, and nested classes. It also covers constant data members and their relationship with const functions. Examples are given to illustrate the concepts.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaraaj
dharmaraaj 🇮🇳

4.4

(68)

145 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Classes
Member Qualifiers
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Classes & Member Functions: inline, static, const, Nested Classes, Constant Data and more Slides Computer Science in PDF only on Docsity!

Classes

Member Qualifiers

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

Nested Classes

• A Class Contained in Another Class

• Multiple Levels of Nesting Are Allowed

• Usual Scoping Rules Apply

• class one{ //Class "two" is nested inside the class "one"Nested Classes -- Example

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

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

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 }