2. Separation of interface and
implementation
Public member function exposed by
a class is called interface
Separation of implementation from
the interface is good software
engineering
3. Complex Number
There are two representations of
complex number
Euler form
z = x + i y
Phasor form
z = |z| (cos + i sin )
z is known as the complex modulus and
is known as the complex argument or
phase
4. Example
float getX()
float getY()
void setNumber
(float i, float j)
…
float x
float y
Complex
Old implementation
float getX()
float getY()
void setNumber
(float i, float j)
…
float z
float theta
Complex
New
implementation
7. Advantages
User is only concerned about ways
of accessing data (interface)
User has no concern about the
internal representation and
implementation of the class
8. Separation of interface and
implementation
Usually functions are defined in
implementation files (.cpp) while the
class definition is given in header file
(.h)
Some authors also consider this as
separation of interface and
implementation
12. There are functions that are
meant to be read only
There must exist a mechanism
to detect error if such functions
accidentally change the data
member
const Member Functions
13. Keyword const is placed at
the end of the parameter list
const Member Functions
16. const Functions
Constant member functions cannot
modify the state of any object
They are just “read-only”
Errors due to typing are also
caught at compile time
22. const Function
Constant member function
cannot change data member
Constant member function
cannot access non-constant
member functions
23. Example
class Student{
char * name;
public:
char *getName();
void setName(char * aName);
int ConstFunc() const{
name = getName();//error
setName(“Ahmad”);//error
}
};
24. this Pointer and const Member
Function
this pointer is passed as constant
pointer to const data in case of
constant member functions
const Student *const this;
instead of
Student * const this;
25. Problem
Change the class Student such
that a student is given a roll
number when the object is
created and cannot be changed
afterwards
30. Member Initializer List
A member initializer list is a
mechanism to initialize data members
It is given after closing parenthesis of
parameter list of constructor
In case of more then one member use
comma separated list
31. Example
class Student{
const int rollNo;
char *name;
float GPA;
public:
Student(int aRollNo)
: rollNo(aRollNo), name(Null), GPA(0.0){
…
}
…
};
32. Order of Initialization
Data member are initialized in order they are declared
Order in member initializer list is not significant at all
42. Constant data members
Make all functions that don’t change the state of the object constant
This will enable constant objects to access more member functions
43. Static Variables
Lifetime of static variable is
throughout the program life
If static variables are not explicitly
initialized then they are initialized
to 0 of appropriate type
45. Static Data Member
Definition
“A variable that is part of a
class, yet is not part of an
object of that class, is called
static data member”
46. Static Data Member
They are shared by all
instances of the class
They do not belong to any
particular instance of a class
47. Class vs. Instance Variable
Student s1, s2, s3;
Class Space
s1(rollNo,…)
s2(rollNo,…)
s3(rollNo,…)
Instance Variable
Class Variable
48. Static Data Member (Syntax)
Keyword static is used to make a
data member static
class ClassName{
…
static DataType VariableName;
};
49. Defining Static Data Member
Static data member is declared
inside the class
But they are defined outside the
class
50. Defining Static Data Member
class ClassName{
…
static DataType VariableName;
};
DataType ClassName::VariableName;
51. Initializing Static Data Member
Static data members should be
initialized once at file scope
They are initialized at the time
of definition
52. Example
class Student{
private:
static int noOfStudents;
public:
…
};
int Student::noOfStudents = 0;
/*private static member cannot be accessed
outside the class except for
initialization*/
53. Initializing Static Data Member
If static data members are not
explicitly initialized at the time
of definition then they are
initialized to 0