Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constant Member Functions

Similar presentations


Presentation on theme: "Constant Member Functions"— Presentation transcript:

1 Constant Member Functions
Does not modify its calling object Declared with keyword const When const appears in the parameter list, e.g., int setNum (const int num) the function is prevented from modifying the parameter. The parameter is read-only. When const follows the parameter list, e.g., int getX()const the function is prevented from modifying the object.

2 Static Members Static member variable:
One instance of variable for the entire class Shared by all objects of the class Static member function: Can be used to access static member variables Can be called before any class objects are created

3 Static Member Variables
Must be declared in the class with keyword static: class IntVal { public: intVal(int val = 0) { value = val; valCount++ } int getVal(); void setVal(int); private: int value; static int valCount; };

4 Static Member Variables
2) Must be defined outside of the class: class IntVal { //In-class declaration static int valCount; //Other members not shown }; //Definition outside of class int IntVal::valCount = 0;

5 Static Member Variables
3) Can be accessed or modified by any object of the class: Modifications by one object are visible to all objects of the class: IntVal val1, val2; valCount val1 val2 2 See pr11-02.cpp and budget.h

6 Static Member Functions
Declared with static before return type: class IntVal { public: static int getValCount() return valCount; } private: int value; static int valCount; };

7 Static Member Functions
Can be called independently of class objects, through the class name: cout << IntVal::getValCount(); Because of item 2 above, the this pointer cannot be used Can be called before any objects of the class have been created Used primarily to manipulate static member variables of the class See pr11-03.cpp, budget2.cpp, and budget2.h

8 Friends of Classes Friend function: a function that is not a member of a class, but has access to private members of the class A friend function can be a stand-alone function or a member function of another class It is declared a friend of a class with the friend keyword in the function prototype

9 Friend Function Declarations
Friend function may be a stand-alone function: class Sample { private: int x; friend void fSet(Sample &s, int a); }; void fSet(Sample &s, int a) s.x = a; } See pr11-04.cpp, auxil.h, budget3.h, auxil.cpp, and budget3.cpp

10 Friend Function Declarations
Friend function may be a member of another class: class aClass { private: int x; friend void OtherClass::fSet(aClass &c, int a); }; class OtherClass public: void fSet(aClass &c, int a) c.x = a; }

11 Friend Class Declaration
An entire class can be declared a friend of a class: class aClass { private: int x; friend class frClass; }; class frClass { public: void fSet(aClass &c,int a) { c.x = a; } int fGet(aClass c){ return c.x;

12 Friend Class Declaration
If FriendClass is a friend of ClassA, then all member functions of FriendClass have unrestricted access to all members of ClassA, including the private members. In general, restrict the property of Friendship to only those functions that must have access to the private members of a class.

13 Aggregation and Composition
Class aggregation: An object of one class owns an object of another class Class composition: A form of aggregation where the enclosing class controls the lifetime of the objects of the enclosed class Supports the modeling of ‘has-a’ relationship between classes – enclosing class ‘has a(n)’ instance of the enclosed class

14 Object Composition Occurs when an object is a member variable of another object. It is often used to design complex objects whose members are simpler objects Example: Define a Course class. Then, define a Transcript class and use a Course object as a member of a Transcript object. See pr7-11.cpp

15 Object Composition Example: Define a Name class, a Date class, and an Address, then define a Person class and use a Name class as a member of the class, a Date object for the dob member, an Address object for the address member of the Person class. See pr7-11.cpp

16 Example of Object Composition
class MyName { private: string firstName; string middle; string lastName; } class MyDate int month; int day; int year; ... };

17 Example of Object Composition
class MyAddress { private: string street; string city; string state; string zip; ... };

18 Example of Object Composition
class Person { private: MyName name; MyDate birthday; MyAddress address; ... };

19 Another Example of Object Composition
class StudentInfo { private: string firstName, LastName; string address, city, state, zip; ... }; class Student StudentInfo personalData;

20 Member Initialization Lists
Used in constructors for classes involved in aggregation. Allows constructor for enclosing class to pass arguments to the constructor of the enclosed class Notation: owner_class(parameters):owned_class(parameters);

21 Member Initialization Lists
Use: class StudentInfo { ... }; class Student private: StudentInfo personalData; public: Student(string fname, lname): StudentInfo(fname, lname);

22 Member Initialization Lists
Member Initialization lists can be used to simplify the coding of constructors Should keep the entries in the initialization list in the same order as they are declared in the class

23 Aggregation Through Pointers
A ‘has-a’ relationship can be implemented by owning a pointer to an object Can be used when multiple objects of a class may ‘have’ the same attribute for a member ex: students who may have the same city/state/ zipcode Using pointers minimizes data duplication and saves space

24 Aggregation, Composition, and Object Lifetimes
Aggregation represents the owner/owned relationship between objects. Composition is a form of aggregation in which the lifetime of the owned object is the same as that of the owner object Owned object is usually created as part of the owning object’s constructor, destroyed as part of owning object’s destructor See pr11-17.cpp


Download ppt "Constant Member Functions"

Similar presentations


Ads by Google