SlideShare uma empresa Scribd logo
1 de 20
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

Mais conteúdo relacionado

Mais procurados

Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Hitesh Kumar
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 

Mais procurados (20)

Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Class and object
Class and objectClass and object
Class and object
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
file handling c++
file handling c++file handling c++
file handling c++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 

Semelhante a Data members and member functions

chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxrayanbabur
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopmShweta Shah
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objectskhaliledapal
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 

Semelhante a Data members and member functions (20)

chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Class and object
Class and objectClass and object
Class and object
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
class c++
class c++class c++
class c++
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
ccc
cccccc
ccc
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 

Último

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 

Último (20)

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 

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