Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo

1

Data Member
and
Member Function
MADE BY:
HARSH PATEL :140950107057
DRASHTI PATEL :140950107056
CSE-B

2

Class Specification
• Syntax:
class class_name
{
};
Data members
Members functions

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)

13

Thank youThank you

More Related Content

Data members and member functions

  • 1. Data Member and Member Function MADE BY: HARSH PATEL :140950107057 DRASHTI PATEL :140950107056 CSE-B
  • 2. Class Specification • Syntax: class class_name { }; Data members Members functions
  • 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)