0% found this document useful (0 votes)
16 views

Week10 11

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Week10 11

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

friend class

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

Let us now create two objects of class


Employee named as e1 and e2,
Let us store the address of object e1 inside a pointer p,

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

Similarly, the statement

will invoke the member function output() for object e1


Binding of Pointers with Individual Members of the Class

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

Step 1:Declare a pointer to a member of a 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.

Step 2: Bind the pointer to a specific member of a class


After the pointer is declared, you must bind the pointer to a specific member.
If delta is a member variable within class A and has a data type as B, you can
bind the pointer ptr
you must also bind pointer to member functions. Let us assume that f1() is the
member function of class A taking 2 arguments of data types d1 and d2, and
having a return type as R.

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,

The member function input() of class Abc is defined as

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.

• When a variable is referred using a this pointer, it will be considered as an access


the value of the member variable of the object, whereas if the variable is referred
without using a this pointer it will be considered as access the formal argument of
the function.
Cloning Objects using this Pointer: An Example

As another example of this keyword, we will create a member function clone()


to create a copy of the invoking object. Consider class Data which has one private
member named as a and public functions set() , clone(), and output().

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.

• However, if the design is such that the object of C1 can be independently


retained in memory even after object of C2 is destroyed, then the relationship
between C1 and C2 is called aggregation.

• Composition between classes is also called HAS-A relationship or containership


because one object is contained inside another.

• We will show the implementation of composition between classes first and


later suggest modifications to change the relationship into aggregation.
For example, let us assume that we need to store a detailed mark sheet for each
student along with the roll number, name, and the total marks.

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:

1. Roll number of a student


2. Name of the student
3. Total of the marks
4. Mark sheet giving the marks of individual subjects
The mark sheet of the student is itself a composite attribute that is required to
contain the marks of each of the three subjects. Hence, every mark sheet must
contain three members m1, m2, and m3

Members of mark sheet


The data requirement is that every student has one mark sheet giving the marks
obtained by the student in each of the subjects.

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

So every object of class Student will have object of class MarkSheet as a


part of it.

This relationship between a Student object and a MarkSheet object is called


containership or composition
It is clear that the object of
class MarkSheet is a sub
part of the Student object,
and hence, if the Student
object is destroyed, the
corresponding MarkSheet
object will also be
destroyed. Hence, this is
composition between
classes.

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 create an aggregation between class Student and MarkSheet, the


requirement is that the object of MarkSheet must not be destroyed even if the
Student object goes out of scope.

• 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.

Objects of class Student and MarkSheet linked using a pointer


The relationship of aggregation between Student and MarkSheet classes is
represented using an UML notation shown in Figure

UML notation of aggregation


The following is the full source code, which creates aggregation between classes
Student and MarkSheet
When the first inner scope ends, object s1 goes out of scope but the object info
still remains in memory. The main() function opens another scope and creates
Student object s2.
NOTES The value of the members of an object is called the state of the object.
info was previously storing marks obtained by Student object s1, but it now stores
the marks obtained by another Student object s2, thereby changing its state

Change of state of object info


The statement

makes the pointer marks of object s2 to point to info because the object info
now stores the marks obtained by s2

Objects s2 and info


Defining the Member Functions Outside Class using Scope Resolution Operator

• Whenever a member function is defined inside the class, by default, it is


considered as an ‘inline’ member function.

• So to make a member function as non-inline member function, we will have


to define the member function of the class outside the body of the class.

• C++ supports definition of member functions outside class using a ‘scope


resolution operator’ [: :].

To create a member function that is non-inline, we must perform following steps:

1. Prototype the function inside the body of the class


2. Define the function outside the class using a “scope resolution” operator
Given below is an example that creates a class Employee with two non-inline
member functions input() and output(), so as to take the members of each Employee
object as input from the user and print them on the screen, respectively.
Function Overloading and Compile Time Binding
When a function is called by the programmer, C++ compiler correctly identifies
which function to call based on the signature of the function.

The function signature of any function includes the following:

1. Name of the function


2. Number of arguments the function takes
3. Data type of each of the arguments
4. Order in which arguments are passed to the function

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.

The ability of a single name to take multiple forms is called polymorphism,


Local Classes

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:

You might also like