C classes objects more concepts More C concepts
C++ classes & objects more concepts
More C++ concepts • • • Const data member. Const member function. Const objects. Friend function. Static data member. Static member function.
Const data member • Achieved using const keyword -const int a; • Attribute not modifiable -cmopiler error if attempt to modify attribute. -not even in constructor body. • Must be initialized using the data member initializer list.
Member initializer list • Can be used for any data member. • Requirednfor initializing -const data members. • Syntax -appears between a constructor’s parameter list and the left prace; separated from the parameter list with a colon (; ). -each member initializer consists of data member name followed by parentheses containing initial value. -multiple member initializers are separated by commas. -excutes before the body of the constructor excutes.
Member initializer list • The constructor of the class myclass causes error: class myclass{ const int a; int b; public: myclass(int i, int j) { a=I; b=j; } • Should be written as: class myclass{ const int a; int b; public: myclass(int i, int j) : a(i) { b=j; }
Const objects • Object not modifiable -compilation error if attempt to modify the object. Only const member function can be called for const objects. • Achieved using const keyword -const time noon (12, 0 0);
Const member functions • Cannot modify class data members. • Specify const in both its prototype and definition. Prototype, after parameter list int gethour () const; . Definition, before beginning left brace int time : : gethour() const {return hour; }
Const member functions • Constructors and destructors cannot be constructor initializes objects. . destructor performs termination housekeeping. • Const member function can be overloaded with a non-const version. compiler chooses which overloaded member function to use based on the object -if the object is const, the compiler uses the const version. -if the object is not const, the compiler uses the non-const version.
Friend functions • A non-member function that can access the private members of a class. • To make a function a friend function: . include its prototype in the class. . precede it with the friend keyword. . member access specifiers are not relevant to friend declaration, so they can be anywhere in a class definition. • Defined outside class’s scope. • Access two or more classes with less overhead.
Friend classes • Friend functions may be members of other classes. to declare such friend function, use scope resolution friend void otheclass: : func(int i); • We can make all member functions of classtwo friends of classone. in classone definition, place declaration of form friend classtwo;
Friend classes • Friendship is established, not taken -for class B to be a friend of class A, class A must expictly declare that class B is its friend. • Friendship relatio is not symmetric -if class A is a friend of class B, it does not imply that B is a friend of A. • Friendship relation is not trasitive -if class A is a friend of class B, and class B is a friend of class C, it does not imply that class A is a friend of class C.
Static data members A data member that is declared static has the following properties: 1. It is initialized to zero when the first object of its class is created. 2. Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how may objects are created. 3. It is visible only within the class, but its lifetime is the entire program.
Note: int item: : count; // definition of static data member Ø The type and scope of each static member variable must be defined outside the class definition. Ø The static data members are stored separately rathe than as a part of an object. Ø They are associated with the class itself rather tha with any class object, they are also known as class variables. Ø While defining a static variable, some initial value can also be assigned int item : : count=0;
Static member functions A member function that is declared static has the following properties: o A static function can have access to only other static members (functions or variables) declared in the same class. o A static member function can be called using the class name (instead of its objects) as follows: class-name: : function-name;
- Slides: 14