This document discusses data members and member functions in C++ classes. It defines data members as variables declared inside a class that can be of any type. Member functions are functions declared inside a class that can access and perform operations on the class's data members. The document outlines how data members and member functions can be defined with public, private, or protected visibility and how they can be accessed from within and outside the class. It also provides syntax examples for defining member functions both inside and outside the class definition.
3. Class Specification
• Visibility of Data members & Member functions
public - accessed by member functions and all
other non-member functions in the
program.
private - accessed by only member functions of the
class.
protected - similar to private, but accessed by
all the member functions of
immediate derived class
default - all items defined in the class are private.
4. Data Members
• The variables declared inside the class are
known as data members.
• The private data of a class can be accessed
only through the member functions of that
class.
• Data members Can be of any type, built-in or
user-defined
5. Accessing Data Members
(inside the class)
• Syntax: (single object)
data_member;
ex: st_id;
• Syntax:(array of objects)
data_member;
ex: st_id;
6. Accessing Data Members
(outside the class)
• Syntax: (single object)
obj_name . datamember;
ex: st.st_id;
• Syntax:(array of objects)
obj_name[i] . datamember;
ex: st[i].st_id;
7. Member Function
• The functions declared inside the class are
known as member functions.
• Member functions access the values of the
data members and perform operations on the
data members .
• Their definition can be placed inside the class
body, or outside the class body.
• Can access both public and private members
of the class
8. Defining Member Functions
• Member functions can be defined in two
places:
– Inside the class definition.
– Outside the class definition.
9. Defining Member Functions
• Inside the Class Definition
– Replace the function declaration with the
definition of the function inside the class.
– When a function is defined inside a class, it is
treated as an inline function.
– All the restrictions and limitations that apply
to an inline function are also applicable to the
functions defined inside a class.
10. Defining Member Functions
• Syntax :(Inside the class definition)
ret_type fun_name(formal parameters)
{
function body;
}
11. Defining Member Functions
• Outside the Class Definition
– Member functions that are declared inside a class
have to be defined separately outside the class.
– Their definitions are very much like the normal
functions.
– They should have a function header and a function
body.
– An important difference between a member
function and a normal function is that a member
function incorporates a membership “identity
label” in the header.
…
This label tells the compiler which class
the function belongs to.
12. Defining Member Functions
• Syntax:(Outside the class definition)
ret_type class_name::fun_name(formal parameters)
{
function body;
}
(scope resolution operator)