SlideShare a Scribd company logo
3
Most read
4
Most read
11
Most read
DATA MEMBERS AND MEMBER
FUNCTIONS IN OBJECT
ORIENTED PROGRAMMING
Presented by :-
DC2016BTE0044 PUSPITA DAS
DC2016BTE0185 SHREYALAXMITALUKDAR
DC2016BTE0195 MARLOM BEY
 Introduction
 Static Data Members
 Accessing Data Members
 Defining Member Functions
 Types of Member Functions
 Conclusion
 References
Contents
Introduction
 Data Members:
O The variables declared inside the class are known as data members.
O Data members may be private or public.
 Member functions:
O The functions declared inside the class are known as member functions.
O Member functions are methods or functions that are defined inside of
objects.
O Generally used to manipulate data members and other object data.
Static Data Members
 Static Data Members are those which are declared by using the static
keyword in front of the data members.
 Static Data Members are always used in the static member function.
 The static data members are always assigned some values from the
outside of the class.
Syntax:
classWidget {
...
static unsigned int g_serial ;
...
} ;
Accessing Data Members
 Accessing Public Data Members
 Accessing Private Data Members
 Accessing Protected Data Members
Accessing Public Data Members
 Following is an example to show how to initialize and use the public data members
using the dot (.) operator and the respective object of class.
class Student
{
public:
int rollno;
string name;
};
int main()
{
StudentA;
Student B;
A.rollno=1;
A.name="Adam";
B.rollno=2;
B.name="Bella";
cout <<"Name and Roll no of A is :"<<A.name << A.rollno;
cout <<"Name and Roll no of B is :"<< B.name << B.rollno;
}
Accessing Private Data Members
 To access, use and initialize the private data member we need to create getter and setter
functions, to get and set the value of the data member.
class Student
{
private:
int rollno;
public: // public getter and setter functions
int getRollno()
{
return rollno;
}
void setRollno(int i)
{
rollno=i;
}
};
int main()
{
Student A;
A.rollono=1;
cout<< A.rollno;
A.setRollno(1);
cout<< A.getRollno(); //Output will be 1
}
Accessing Protected Data Members
 Protected data members, can be accessed directly using dot (.)
operator inside the subclass of the current class
 Protected data members can be accessed in the same way as
public data members f.rom friend functions or classes and from
derived classes.
Defining Member Function
 Inside the class definition
 Outside the class definition
InsideThe Class Definition
 A member function of a class can be defined inside the class. However,
when a member function is defined inside the class, the class name and
the scope resolution operator are not specified in the function header.
Example :
class book
{
char title[30];
float price;
public:
void getdata(char [],float); II declaration
void putdata()//definition inside the class
{
cout<<"nTitle of Book: "<<title;
cout<<"nPrice of Book: "<<price;
};
OutsideThe Class Definition
 Defining a member function outside a class requires the function declaration to be
provided inside the class definition.
Example:
Class book
{
// body of the class
} :
void book :: getdata(char a[],float b)
{
// defining member function outside the class
Strcpy(title,a):
price = b:
}
void book :: putdata ()
{
cout<<"nTitle of Book: "<<title;
cout<<"nPrice of Book: "<<price;
}
TYPES OF MEMBER FUNCTION
 Simple Function
 Static Function
 Const Function
 Inline Function
 Friend Function
Simple Function
 These are the basic member function, which don,t have any
special keyword like static etc. as prefix.
Example:
return_type functionName(parameter_list)
{
function body;
}
Static Member Function
 A function is made static by using static keyword with function name.
 It can be called using the object and the direct member access (.) operator.
But, its more typical to call a static member function by itself, using class
name and scope resolution (::) operator.
A function is made static by using static keyword with function name
Example:
class X
{
public:
static void f(){};
};
int main()
{
X::f(); // calling member function directly with class name
}
Const Member Functions
 Const keyword makes variables constant, that means once
defined, there values can't be changed.
 When used with member function, such member functions can
never modify the object or its related data members.
//Basic Syntax of const Member Function
void fun() const {}
Inline Member Functions
 All the member functions defined inside the class definition are by default
declared as Inline.
 Member functions containing a few lines of code are usually declared inline.
For example:
ClassY
{
char*a ;
public:
Char* f();{return a;}
};
Is equivalent to
Char Z
{
char*a;
Public:
char* f();
};
Inline char* Z::f()
{return a;}
Friend Member Functions
 Friend functions are made to give private access to non-class functions.You can
declare a global function as friend, or a member function of other class as friend.
Example:
class WithFriend
{
int i;
public:
friend void fun(); // Global function as friend
};
void fun()
{
withFriend wf;
wf.i=10; //Access to private data member
cout << wf.i;
}
int main()
{
fun(); //Can be called directly
}
Conclusion
 A static member function can only access static data members of the
class, it cannot access instance data members.
 A private member function can only be called by another function
that is a member class.
 Protected keywords are only used in the inheritance context.
 Objects created by object oriented programs can easily be reused in
other programs
References
■ Books
[1]. By E Balagurusamy “Object Oriented ProgrammingWith C++” ,TATA McGraw-
Hill Publishing Company Limited, 2008
■ Web Links
[2]. https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html
---OOP Basics
[3]. http://www.studytonight.com/cpp/accessing-data-
members.php
---Accessing Data Members
[4]. http://www.studytonight.com/cpp/member-functions-cpp.php
---Member Functions
[5]. http://www.studytonight.com/cpp/types-of-member-function.php
---Types of Member Function
THANK YOU

More Related Content

PPT
Data members and member functions
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Classes and objects
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPTX
Static Data Members and Member Functions
PPTX
[OOP - Lec 18] Static Data Member
PDF
Friend function in c++
PPTX
OOP Unit 2 - Classes and Object
Data members and member functions
[OOP - Lec 19] Static Member Functions
Classes and objects
[OOP - Lec 09,10,11] Class Members & their Accessing
Static Data Members and Member Functions
[OOP - Lec 18] Static Data Member
Friend function in c++
OOP Unit 2 - Classes and Object

What's hot (20)

PPT
Function overloading(c++)
PPTX
Abstract class in c++
PPTX
classes and objects in C++
PPTX
Inheritance in c++
PPT
08 c++ Operator Overloading.ppt
PPTX
Member Function in C++
PPTX
Access specifier
PPTX
Inheritance in java
PDF
Constructor and Destructor
PPTX
PPTX
Classes, objects in JAVA
PPT
structure and union
PPT
File handling in c
PPT
Class and object in C++
PPTX
Inline function
PPT
friend function(c++)
PPTX
Inheritance in c++
PPTX
[OOP - Lec 07] Access Specifiers
PPTX
Function overloading
PPTX
Functions in C
Function overloading(c++)
Abstract class in c++
classes and objects in C++
Inheritance in c++
08 c++ Operator Overloading.ppt
Member Function in C++
Access specifier
Inheritance in java
Constructor and Destructor
Classes, objects in JAVA
structure and union
File handling in c
Class and object in C++
Inline function
friend function(c++)
Inheritance in c++
[OOP - Lec 07] Access Specifiers
Function overloading
Functions in C
Ad

Similar to Data members and member functions (20)

PPT
static member and static member fumctions.ppt
PPT
Unit vi(dsc++)
PDF
Introduction to C++ Class & Objects. Book Notes
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PPTX
classes & objects in cpp overview
PPTX
classes & objects in cpp
PPTX
Class and object
PPT
DS Unit 6.ppt
PPT
classes data type for Btech students.ppt
PPT
APL-2-classes and objects.ppt
PPT
Classes in C++ computer language presentation.ppt
PPTX
Classes and objects
PPT
APL-2-classes and objects.ppt data structures using c++
PPTX
class c++
PPSX
Arre tari mano bhosko ka pikina tari ma no piko
PPTX
cse l 5.pptx
PDF
Implementation of oop concept in c++
PDF
Class object
PPTX
Presentation on class and object in Object Oriented programming.
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
static member and static member fumctions.ppt
Unit vi(dsc++)
Introduction to C++ Class & Objects. Book Notes
cpp class unitdfdsfasadfsdASsASass 4.ppt
classes & objects in cpp overview
classes & objects in cpp
Class and object
DS Unit 6.ppt
classes data type for Btech students.ppt
APL-2-classes and objects.ppt
Classes in C++ computer language presentation.ppt
Classes and objects
APL-2-classes and objects.ppt data structures using c++
class c++
Arre tari mano bhosko ka pikina tari ma no piko
cse l 5.pptx
Implementation of oop concept in c++
Class object
Presentation on class and object in Object Oriented programming.
classandobjectunit2-150824133722-lva1-app6891.ppt
Ad

Recently uploaded (20)

PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
PPT on Performance Review to get promotions
PPTX
Construction Project Organization Group 2.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Sustainable Sites - Green Building Construction
PDF
Digital Logic Computer Design lecture notes
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Welding lecture in detail for understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT on Performance Review to get promotions
Construction Project Organization Group 2.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Operating System & Kernel Study Guide-1 - converted.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
R24 SURVEYING LAB MANUAL for civil enggi
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Sustainable Sites - Green Building Construction
Digital Logic Computer Design lecture notes
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CH1 Production IntroductoryConcepts.pptx
Welding lecture in detail for understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

Data members and member functions

  • 1. DATA MEMBERS AND MEMBER FUNCTIONS IN OBJECT ORIENTED PROGRAMMING Presented by :- DC2016BTE0044 PUSPITA DAS DC2016BTE0185 SHREYALAXMITALUKDAR DC2016BTE0195 MARLOM BEY
  • 2.  Introduction  Static Data Members  Accessing Data Members  Defining Member Functions  Types of Member Functions  Conclusion  References Contents
  • 3. Introduction  Data Members: O The variables declared inside the class are known as data members. O Data members may be private or public.  Member functions: O The functions declared inside the class are known as member functions. O Member functions are methods or functions that are defined inside of objects. O Generally used to manipulate data members and other object data.
  • 4. Static Data Members  Static Data Members are those which are declared by using the static keyword in front of the data members.  Static Data Members are always used in the static member function.  The static data members are always assigned some values from the outside of the class. Syntax: classWidget { ... static unsigned int g_serial ; ... } ;
  • 5. Accessing Data Members  Accessing Public Data Members  Accessing Private Data Members  Accessing Protected Data Members
  • 6. Accessing Public Data Members  Following is an example to show how to initialize and use the public data members using the dot (.) operator and the respective object of class. class Student { public: int rollno; string name; }; int main() { StudentA; Student B; A.rollno=1; A.name="Adam"; B.rollno=2; B.name="Bella"; cout <<"Name and Roll no of A is :"<<A.name << A.rollno; cout <<"Name and Roll no of B is :"<< B.name << B.rollno; }
  • 7. Accessing Private Data Members  To access, use and initialize the private data member we need to create getter and setter functions, to get and set the value of the data member. class Student { private: int rollno; public: // public getter and setter functions int getRollno() { return rollno; } void setRollno(int i) { rollno=i; } }; int main() { Student A; A.rollono=1; cout<< A.rollno; A.setRollno(1); cout<< A.getRollno(); //Output will be 1 }
  • 8. Accessing Protected Data Members  Protected data members, can be accessed directly using dot (.) operator inside the subclass of the current class  Protected data members can be accessed in the same way as public data members f.rom friend functions or classes and from derived classes.
  • 9. Defining Member Function  Inside the class definition  Outside the class definition
  • 10. InsideThe Class Definition  A member function of a class can be defined inside the class. However, when a member function is defined inside the class, the class name and the scope resolution operator are not specified in the function header. Example : class book { char title[30]; float price; public: void getdata(char [],float); II declaration void putdata()//definition inside the class { cout<<"nTitle of Book: "<<title; cout<<"nPrice of Book: "<<price; };
  • 11. OutsideThe Class Definition  Defining a member function outside a class requires the function declaration to be provided inside the class definition. Example: Class book { // body of the class } : void book :: getdata(char a[],float b) { // defining member function outside the class Strcpy(title,a): price = b: } void book :: putdata () { cout<<"nTitle of Book: "<<title; cout<<"nPrice of Book: "<<price; }
  • 12. TYPES OF MEMBER FUNCTION  Simple Function  Static Function  Const Function  Inline Function  Friend Function
  • 13. Simple Function  These are the basic member function, which don,t have any special keyword like static etc. as prefix. Example: return_type functionName(parameter_list) { function body; }
  • 14. Static Member Function  A function is made static by using static keyword with function name.  It can be called using the object and the direct member access (.) operator. But, its more typical to call a static member function by itself, using class name and scope resolution (::) operator. A function is made static by using static keyword with function name Example: class X { public: static void f(){}; }; int main() { X::f(); // calling member function directly with class name }
  • 15. Const Member Functions  Const keyword makes variables constant, that means once defined, there values can't be changed.  When used with member function, such member functions can never modify the object or its related data members. //Basic Syntax of const Member Function void fun() const {}
  • 16. Inline Member Functions  All the member functions defined inside the class definition are by default declared as Inline.  Member functions containing a few lines of code are usually declared inline. For example: ClassY { char*a ; public: Char* f();{return a;} }; Is equivalent to Char Z { char*a; Public: char* f(); }; Inline char* Z::f() {return a;}
  • 17. Friend Member Functions  Friend functions are made to give private access to non-class functions.You can declare a global function as friend, or a member function of other class as friend. Example: class WithFriend { int i; public: friend void fun(); // Global function as friend }; void fun() { withFriend wf; wf.i=10; //Access to private data member cout << wf.i; } int main() { fun(); //Can be called directly }
  • 18. Conclusion  A static member function can only access static data members of the class, it cannot access instance data members.  A private member function can only be called by another function that is a member class.  Protected keywords are only used in the inheritance context.  Objects created by object oriented programs can easily be reused in other programs
  • 19. References ■ Books [1]. By E Balagurusamy “Object Oriented ProgrammingWith C++” ,TATA McGraw- Hill Publishing Company Limited, 2008 ■ Web Links [2]. https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html ---OOP Basics [3]. http://www.studytonight.com/cpp/accessing-data- members.php ---Accessing Data Members [4]. http://www.studytonight.com/cpp/member-functions-cpp.php ---Member Functions [5]. http://www.studytonight.com/cpp/types-of-member-function.php ---Types of Member Function