You are on page 1of 11

OBJECT-ORIENTED Lecture 5

PARADIGM Instructor : Syeda Hira Fatima


CLASSES CONT…
Classes have the same format as plain data structures, except that they can also
include functions and have these new things called access specifiers. An access
specifier is one of the following three keywords:
•Private
•Public
•Protected
By default, all members of a class declared with the CLASS keyword have private
access for all its members. Therefore, any member that is declared before any
other access specifier has private access automatically.
What would be the access specifier for width and height
ACCESS SPECIFIERS
In C++, there are three access specifiers:
•public - members are accessible from outside the class
•private - members cannot be accessed (or viewed) from outside the
class
•protected - members cannot be accessed from outside the class,
however, they can be accessed in inherited classes.
•SCOPE OF CLASS
•All member functions are in class scope even if they are defined outside their class declaration.

•scope operator (::, two colons)


•In this outside definition, the scope operator (::) is used to specify that the function being
defined is a member of the class Rectangle and not a regular non-member function.
How to define Member Functions in C++?
There are mainly two ways to define Member Function in C++.
1.Definition within the Class
2.Definition outside the Class
Defining Member Function within the Class
We can give a definition of the function within the class to define Member Function. This
method of defining Member Function is generally used when a definition of function is short.
Member functions in C++ has all the access to data members (Public, Private, Protected)and
other member functions of the same class.
Defining Member Function outside the Class
In this method, we can declare a function within the class and we define the function outside
the class to achieve the Member Function in C++. While using this method we need to ensure
that the function is declared within the class. The problem with this method is when we have
more than one class having the same function name so it will be confusing which function’s
definition it is.
To avoid this problem we can use the scope
resolution operator (::). The syntax of using the
scope resolution operator to achieve Member
Function in C++ is given below.

return_type class_name :: member_function_name


(arguments){ // definition of the function }

void Circle :: printSize(int s)


{ cout<< s <<endl; }

void Square :: printSize(int s)


{ count<< s <<endl; }
The most important property of a class is that it is a type, and as such, we can declare
multiple objects of it.
CONSTRUCTORS
What would happen in the previous example if we called the member
function area before
having called set_values? An undetermined result, since the
members width and height had
never been assigned a value.

In order to avoid that, a class can include a special function called its constructor, which
is automatically called whenever a new object of this class is created, allowing the class
to initialize member variables or allocate storage.
This constructor function is declared just like a regular member function, but with a
name that matches the class name and without any return type; not even void.
Constructors cannot be called explicitly as if they were regular member functions. They are only executed once,
when a new object of that class is created.
• It is always public.
• Constructors never return
• values, they simply initialize
• the object.
• Constructors can also take parameters
(just like regular functions), which can be
useful for setting initial values for attributes.
Just like functions.
Constructors can also be
defined outside the class. First, declare the
constructor inside the class, and then define it
outside of the class by specifying the name of
the class, followed by the scope resolution

You might also like