Week 9
Week 9
• Class is a template that wraps data (variables) and related functions together into a
single capsule
• This process of wrapping data and related functions together is called encapsulation
• An object preserves all the properties of the class for which it is instantiated.
• Hence, the class is a ‘schema’ for all its objects, and an object is defined as an
‘instance’ or a ‘snapshot’ of the class.
• Assume we want to write a C++ program to calculate the slope of a line.
• To calculate the slope, we must store the start point (x1, y1) from where the
line begins and end point (x2, y2) where the line ends.
• It would have been very easy if C++ could have offered a data type named as
Point, just like it offers other built-in data types such as int, float, char etc.
• Then, each variable of type Point has x and y values to specify the location of
the point graphically.
• If this approach would have been supported, we could have created two
variables of type Point, named as p1 and p2 such that the line starts at point p1
and ends at a point p2
• This will give programmers the ability to create variables of type Point just
like we create variables of any other basic types such as int, float, char etc.
• Once the template for Point is defined by the class, we will be able to
create variables of type Point just like we create variables of any other data
type
Creating class Point and its objects
• The variables p1 and p2 of type Point are called objects of class Point.
• Objects will preserve all the properties defined by the class Point.
• This means that all the member variables defined in class Point will be
incorporated by every object of Point.
• Each point object requires a variable x that stores x-coordinate value of the
point and a variable y that stores y-coordinate value of the point.
• Once the class is defined, we can create any number of objects of that class.
• For example, we can create three variables (objects) of type Point using a C++
statement below:
Objects p1, p2, and p3
• To show the inclusion of member functions in the class, we have written two
member such that the function input() takes the values of id, name and salary as
input from the user and the function output() prints the details of a particular
employee.
NOTES
♦ Variables defined in the class are called as Member variables ♦ Functions defined in
the class are called as Member functions or Methods or Operations of that class.
Creating Objects of a Class
We can create variables which are of the type defined by the class. These variables
are called objects of the class.
For example, we can create three objects of a class Employee using the following
statement:
The variables e1, e2, and e3 have a data type as Employee, and hence they are
called objects of class Employee.
The members of the class are contained in the object memory of each of the
objects, and they will store junk values unless they are initialized
Objects share a common memory for member functions
For example, if the names of the three employees are “John”, “Jack”, and “Jill”,
then we can individually initialize a member variable name for each of the
objects e1, e2, and e3 as shown below
For example, the member function input() can be invoked using object e1 by using the
following statement:
written inside input() will execute on behalf of object e1, thereby taking to members id,
name, and salary of object e1 as input from the user.
Also, we can invoke a member function output() to print the details of each of the
employee using the statements:
Access Specifiers in C++
Access to each of the member variables and member functions of the class can be
controlled using the following keywords:
• private
• public
• protected
Private access
• This means that, when a member variable is defined as private, its value cannot
be accessed from any place outside class,
• All members/member functions of the class are by default private, you can also
use the keyword private to explicitly mention private members.
Syntax for creating private members
Making members and member functions of class Employee as private
Accessing any function or a variable of class Employee from the outside world will
give an error
Public access
• The public member variables and member functions of the class can be accessed
from any place in the program
• They can be accessed from any place inside or outside the class.
• Note that all the members and member functions written after the keyword
public:
will be marked as public members, and hence they can now be accessed from
anyplace in the program.
Syntax for creating public members
Making members and member functions of class Employee as public
We can now access any function or a variable of class Employee from the outside world
Data Hiding and Encapsulation
• We know that private member of the class cannot be accessed from the
outside world.
• Hence, we say that the private members are hidden from the outside
world.
• We usually create ‘public functions’ inside the class for the outside world
to access ‘private members’ with certain regulations imposed by public
functions.
Data Hiding and Encapsulation
• ‘Public functions’ are inside the class and hence they can access the ‘private
members’ defined in the class, and since these functions are public, they can be
called by the outside world.
• Therefore, using this design the outside world can access the value of ‘private
members’ through respective ‘public functions’
• The ‘public functions’ act as a gateway for the outside world to access the value
of ‘private variables’ defined by the class
Accessing the private members using the public functions
• The advantage of this design is that the public function can now implement the
set of rules that are to be followed by every outsider before an access to the
‘private member’ can be allowed.
• The programmer will not be able to access the value of ‘private member’ without
following the rules defined by the ‘public function’.
• As an example, let us develop a C++ program that stores the data about three
employees in computer memory.
• For each employee, we will store the id, name and the salary information.
• We will also implement an integrity constraint that the salary of each employee
should be always a positive number.
• We will include two public member functions input() and output() to take the
employee details as input from the user and to print the details on the
computer screen, respectively
State of objects e1, e2, and e3
Size of Employee object
Let us assume that the user enters the id, name, and salary of each of the
Employee objects as shown in the table below:
Statements e1.input(), e2.input()
State of objects e1, e2, and e3
Can assignment operator be used with objects? For example, can we write a
statement e1=e2; in the code ?
Yes. Assignment operator can be used with objects. If you write a statement
e1=e2; all the values of members of object e2 will be copied to corresponding
members of object e1.
Calculating Slope of the Line: An Example
• input(): To take x and y values of each point as input from the user.
• output(): To print the x and y values of each point on the computer screen
• slope(): To calculate the slope of the line defined between two point objects
The slope (represented by s) of a line from (x1, y1) to (x2, y2)
is defined as
s
In this case, as the line is defined between two Point
objects p1 and p2, the equation for slope can be
re-written as follows:
s
As slope() is a member function, it must be invoked using object of a class.
This means that the member function slope() must be invoked using object p1 or
using object p2. If we invoke the member function slope() using object p1, the
values of members x and y for object p1 can be directly accessed inside the function
Since the function is invoked using object p1;
p2’s members cannot be directly accessed inside
it. To solve this problem, we will have to pass
object p2 as an argument to the function so that
the complete object p2 will now be available in
the formal argument t
Hence, a call to the slope() function must be made as
The member function slope() is invoked by object p1, and object p2 is passed as
an argument to it.
This means that x and y values of p1 can be directly accessed inside the function,
whereas the x and y values of p2 must be accessed using object t as t.x and t.y,
respectively
The statement
Following this we take the x and y values of each of the Point objects as input
from the user by invoking the input() function over each of the objects as
shown
The statement
s=p1.slope(p2);
The member function slope creates another object named as t as a formal argument
and hence p2’s members x and y can be accessed using object t as t.x and t.y
respectively.
It is important to understand that p2 and t are two different objects in memory but
the member values of object p2 will be same as that of the member values of object
t because p2 is a actual argument and t is the formal argument.
Addition of Complex Numbers: An Example
Just like each Point object is defined by specifying its x value and y value, every
complex number also is defined by specifying its real part and imaginary part.
Hence, we create two private variables real and img as members of class Complex
.We need following member functions in class Complex:
• input(): To take real and img values of each Complex object as input from the
user.
• output(): To print the real and img values of each Complex object on the
computer screen.
• add(): To perform the addition of two Complex objects and return the result of
addition
We start the explanation of this example from the member function add()
If C1 and C2 are two objects of class Complex and if we need to store the result of
addition of C1 and C2 in another complex number C3
The member function add() must be invoked using object C1 or using object C2.
If we invoke the member function add() using object C1, then the values of
members real and img for object C1 can be directly accessed inside the add()
function
Accessing members of object C1 inside add() function
As noted, the real and img values of the complex object C2 will also be required
inside the add() function to calculate the result of addition of two complex numbers
This means that real and img values of C1 can be directly accessed inside the add()
function,
whereas the real and img values of C2 must be accessed using object t as t.real and
t.img, respectively.
Just like any other array, we can also create an array of objects in C++. Given below
is the syntax to create an array of class objects:
As an example to create array of objects, let us consider the class Employee with
private members id, name, and salary and public functions input() and output().
The below C++ statement creates an array of Employee objects named as e with
a size of 3.
Note that each element of the array e will be an object of class Employee because
the data type of the full array is Employee.
Similarly, the statements
e[0].input();
e[1].input();
e[2].input();
will invoke the member function input() using objects e[1] and e[2], respectively, to
take the member values of each of the objects as input from the user
Similarly, the statements
e[0].output();
e[1].output();
e[2].output();
will invoke a output() function and execute a cout statement which will print
the member values of each of the respective Employee objects.
Employee Management System: Revisited
• Until this point, we learned that the private members of a class can only be
accessed inside the class.
• To create a friend function, a class must prototype that function using a keyword
friend.
• Here, we create function f1() as a friend of class Pqr by prototyping the function
f1() inside the class Pqr. The complete function definition of a friend function is
always present outside the class because a friend function is not the member of a
class.
Accessing members of a class using friend function
Therefore, a friend function is never invoked using object of a class like member
functions.
The friend function must be invoked directly just like any other non-member
function
The members of the object should be accessed by explicitly giving the object name
followed by membership operator to determine the exact object whose member is
to be accessed
Similarly, if a friend function needs to call any member function from its
definition, you must explicitly specify the name of the object using which the
member function needs to be invoked.
Hence, the syntax to invoke the member function from the friend function
is as follows:
You can also access the members and member functions of a class from friend
function by creating a pointer to object.
Addition of Point Objects using friend Function: An Example
As an example, let us perform addition of Point objects using a friend function add()
The function add() is now will created as a friend function of class Point
Since the function add() is created as a friend function, we do not need an object
to invoke it. Hence, we have invoked the function add() using the following
statement:
The members of no object can be directly accessed inside the function add()
because it is not invoked using any of the objects.
Hence, we pass both the objects p1 and p2 as an argument to the function such
that the actual argument p1 gets initialized to the formal object t and the actual
argument p2 gets initialized to the formal object p.
This means that, members of object p1 and p2 can be accessed using formal
arguments t and p, respectively