4 2 Static Const
4 2 Static Const
• static Variables
– Default Initialization: 0 or Null (for pointers)
– Initialization: user defined value
– Initialization is made just once, at compile time.
– Accessibility: Private or Public
Public static Class Variables
– Can be accessed using Class name:
cout<<Employee::count;
• Static functions:
– Can access: static data and static functions
– Cannot access: non-static data, non-static functions,
and this pointer
Public static Class Functions
–Can be invoked using class’s any object:
cout<<e1.getCount();
122
123 return 0;
count back to zero.
124 }
21. };
Object as Function Parameter (Passing and returning object)
1. int main(){
2.
3. Distance dist1, dist3; //define two lengths
4. Distance dist2(11, 6.25); //define and initialize dist2
5.
6. dist1.getdist(); //get dist1 from user
7.
8. dist3 = dist1.add_dist(dist2); //dist3 = dist1 + dist2
9.
10. //display all lengths
11. cout << "\ndist1 = "; dist1.showdist();
12. cout << "\ndist2 = "; dist2.showdist();
13. cout << "\ndist3 = "; dist3.showdist();
14. cout << endl;
15. return 0;
16. }
The this Pointer (Extra, Not included in outline/syllabus)
The this Pointer
this keyword is a special built-in pointer (constant
pointer) that references to the calling object.
– Executes t.setHour(1), returns *this (reference to object) and the expression becomes
t.setMinute(2).setSecond(3);
128
129 return 0;
130 }
2. Write a program with a class which has one Reference data member
and a constructor to initialize it. Also write main function.
3. Write a program with a class “Distance”, in the class only write data
members , Constructor , a function to add two object and return an
object. (Note: Here main function is optional, write or not it don’t
carry any marks)