Title | More to Class - Lecture notes 5 |
---|---|
Course | Programming Technique II |
Institution | Universiti Teknologi Malaysia |
Pages | 3 |
File Size | 105.3 KB |
File Type | |
Total Downloads | 570 |
Total Views | 684 |
Programming Technique 2: More to Classes Static Member Variable • Static member variable: shared by all the data in the class, it is NOT stored in an object data • o it is stored before any data is created o must declare outside the class o private: o static int objectCount; // inside class (no init...
Programming Technique 2: More to Classes Static Member Variable •
Static member variable: shared by all the data in the class, it is NOT stored in an object data
•
o
it is stored before any data is created
o
must declare outside the class
o
private:
o
static int objectCount; // inside class (no initialization)
o
int Tree::objectCount = 0; //outside class (initialize to 0)
Normally static member variable is used to count the number of functions being called
•
we can set static variable to be incremented in constructor so that every time an object is created, the number will increase by 1
•
Static member function cannot access any non-static member data in class
•
When static function is created, it is called before any instances of classes are defined
•
Friend= function / class that is not member of the class but can access the private members of that class
•
Friend can be stand-alone function or member function of other class / entire another class o
•
Format :: friend ReturnType FunctionName ( ParameterTypeList )
Copy Constructor : called when an object is initialized with another object’s data. o
format almost same as normal constructor but it has reference object
o
ex:
o
StudentTestScores( const StudentTestScores &obj)
o
{ studentName = obj.studentName; }
o
put const because copy constructor copy argument data only, cannot change data
o
put reference to prevent the copy constructor from calling itself infinite time
o
copy constructor is done so that the data in obj.studentName wont destroy/ corrupt the data in copy constructor studentName
•
•
overloaded operator: o
=
o
+=
o
-+
o
/=
postfix ++ operator has dummy parameter : o
FeetInches FeetInches::operator++(int)
o
the one in the bracket is dummy parameter, computer will know this is postfix when they see it
•
For prefix, the bracket won’t have variable : o
•
•
FeetInches FeetInches::operator++( )
prefix : n=++m o
increment m first, then assign
o
if initially n and m =0, then this code will produce n=1, m=1
postfix : n=m++ o
assign m first then increment m
o
this code will produce n=0, m=1
•
To convert an int type to double or etc. (double d=int c)
•
Aggregation: used when a class need to access another class's instance
•
default constructor → initialize the data to 0
•
constructor→ set each data to its type
•
Diagram o
aggregation =white diamond shape
o
composition =black diamond shape
•
Composition cannot live on its own, once the composed class collapse, it dies too
•
Aggregation can live on its own, once the aggregated class collapse, it still can function
•
Aggregation use pointer * object, call function by '→ ' o
at the main function need to dynamically allocate the pointer
•
ex: Student *prefect=new Student ("Ali"); //Ali is stored in prefect data
•
Composition didn’t use pointer, just use object straight away call function
•
Association use pointer *object also but just normal '=' assign, didn’t use ' → '...