Week10 11
Week10 11
A class can be made a friend of another class. If class C1 is made a friend of class
C2, it means that all the member functions of class C1 can access the private
members of class C2
As an example, let us create a class named as Data and create a class named as Result
as a friend of class Data.
Pointer to Objects
We can define pointers that can point to objects of a class.
As an example, let us create a pointer p to point to object of class Employee. As
the pointer is required to point to Employee object, the data type of the pointer
must also be Employee
We can now invoke the member functions of object e1 using a pointer p with an
arrow operator ->
will invoke the member function input() for object e1
Instead of creating a pointer to the full object, you can also create a pointer to a
specific member or a member function of the class
If the name of the class is A and the data type of the member for which a pointer
needs to be created is B, then the syntax to declare a pointer named as ptr,
You can also declare a pointer that has the ability to point to a member function
of a class. If the member function of class A has a return type R, then following
syntax can be used to declare a pointer named as pftr that has the ability to point
to the member function.
In this case, we can create the pointer pftr and bind it to member function f1()
Step 3: Access the member using object of class and a bindable pointer
After the pointer is attached to a specific member, you can use it with any
object. For example, let us assume that o1 and o2 are two objects of class A
You can also invoke a member function f1() with any of the objects and using a
pointer to a member function named as pftr, which we have created in step 2.
will invoke the function f1() using object o1 because pftr is attached to function
f1() as per step 2. We assume that v1 and v2 are the values of data types d1 and
d2, respectively, and the data type of variable x is R
will invoke function f1() for object o2 by passing v1 and v2 as arguments and will
store return value in x.
this Pointer
This is a built-in pointer in C++ that points to the object using which the function is
invoked.
• The keyword this is generally used inside the body of the member function
to determine which object has invoked the function.
• We know that a class can have multiple objects, and a member function can
be invoked using any one of the objects of the class.
• You may come across a situation wherein you have already entered into the
function and need to determine the exact object that invoked the function
within the function body.
As this is a pointer, we must use arrow operator (->) when accessing the
members of the object using this keyword. The syntax to access any member
variable of the invoking object using this keyword :
You can also, invoke another member function inside a body of particular member
function over the same object using this keyword. The syntax to invoke a member
function:
As an example to understand the syntax of this keyword, we create a class Abc
with a single integer member variable a and input() and output() functions to take
the value of a as input from the user and print it on the computer screen,
Note the presence of arrow operator -> inside the input() function, which is used
to access the member a of the invoking object.
Let O1 and O2 be two different objects of class Abc. The pointer this will always
refer to the object that has invoked the member function of the class.
This means that when the object O1 invokes the member function input(), this->a
means member a of object O1, and hence, the statement
will accept the value of member a of object O1 as input from the user
The following is the full source code that shows the usage of this keyword in C++
by creating two objects O1 and O2 of the class Abc
Resolving Ambiguity using this Pointer
this keyword can be used to resolve the ambiguity that may occur when the formal
argument of the member function has the same name as that of the member variable
defined in the class
• For example, consider a class Abc with public functions set() and output() and a
private member variable named a.
• The function set() takes a formal argument which is also named as a. Hence,
compiler will not be able to differentiate between an access made to the member
variable a and the access made to the formal argument a within the scope of set()
function.
The function clone() is defined such that it returns the object that invoked it to
the caller
returns the object that is pointed by the this pointer (which is the invoking object
itself). indirection operator * is used to ensure that the target object is returned and
not the memory address.
will create a copy of object o1 in object o2. In other words, object o2 will be created
as a cloned version of object o1,
Dynamic Memory Allocation of Objects
The following is the syntax for dynamically creating an object of a class in the program:
The statement will create an object of a class at run time and store the address of the
object inside a pointer
As an example, let us create a class Employee with private members id, name,
and salary and public functions input() and output()
As this object is dynamically created, it will be unnamed in the memory.
Therefore, we create a pointer p as shown below
will invoke member function input() using pointer p
Composition and Aggregation between Classes
• Given two classes C1 and C2 such that object of C1 is a part of object of C2; and
if object of C1 is automatically destroyed when object of C2 is destroyed, then
the relationship between C1 and C2 is called composition.
The mark sheet is required to contain the marks of three subjects and let us
name them as m1, m2, and m3.
For every Student object, we are required to store the following information:
To solve this problem, we create a class named as MarkSheet with three members
m1, m2, and m3. With this approach, each object of class MarkSheet will represent
one mark sheet in the real world
Composition of objects
Composition between classes is represented by an association link with a filled
diamond on the whole part as shown in Figure .This notation is called a UML
notation used to model class diagrams in large projects
UML stands for unfilled modelling language. It is a language which gives set of notations
to represent relationships between classes.
The following is the full source code, which shows the creation of composition
between classes Student and MarkSheet:
The first statement in main()
creates two objects of class Student named as s1 and s2.
invokes the inputStud() function over object s1.
Objects s1 and s2
Converting the Relationship to Aggregation
• To attain this result, the MarkSheet object must be stored external to Student
object and should not be embedded within the Student object.
• This can be achieved by creating a pointer in the Student object that points
externally
• Marks is created as a pointer of type MarkSheet rather than its direct object
The pointer marks points to the externally present Marksheet object, and hence the
MarkSheet object can still be retained in the memory even if the Student object goes
out of scope.
makes the pointer marks of object s2 to point to info because the object info
now stores the marks obtained by s2
Note that return type of the function is not included in its signature. This
means that two functions with same signature cannot exist in the program
even if their return types are different.
NOTES Two functions with the same signature cannot coexist in the same program.
Given below are some example prototypes showing valid/invalid coexistence of the
functions.
Although there are two function with same name in the program, the compiler
determines the correct function to call based on the function signature.
This process is called compile time binding. Binding is actually a process to attach
a ‘function call’ with a ‘function definition’, in this case binding is performed by
the compiler and hence it is called compile time binding or Early binding or static
binding.
The function name area() takes two forms: one form is to calculate the area of
circle, whereas the other is to calculate the area of rectangle.
It is possible to define a class within a function. Such a class can only be accessed
within the scope of the function and nowhere else. This is called a local class.
For example, consider a function add() which creates a class Number to perform
addition of two integers.
Observe that class Number is defined within the body of function add(), and
hence the class cannot be accessed outside the scope of add()
Nested Classes
It is possible to define a class inside another class. This design is called nesting of
classes. For example, consider the following code: