INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Object Oriented Programming using C++
Code:21CSH-103
Topic : Defining Member functions DISCOVER . LEARN . EMPOWER
Object Oriented
Programming
using C++
Course Objectives
To enable the students to understand various
stages and constructs of C++ programming
language and relate them to engineering
programming problems.
To improve their ability to analyze and address
variety of problems in programming domains.
2
Course Outcomes
CO Course Outcome
Number
CO1 Understand the concepts of object-oriented programming
including programming process and compilation process.
CO2 Apply different techniques to decompose a problem and
programmed a solution with its sub modules.
CO3 Analyze and explain the behavior of simple programs involving the
programming addressed in the course.
CO4 Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5 Design the solution of real-world problems in order to determine
that the program performs as expected.
3
Scheme of Evaluation
4
What
. A function declared as a
member of a class is called
as a member function. Why
Member functions are Member functions and functions are
mostly given the attributes names used interchangeably in
of public because they reference classes. Function prototypes
have to be called outside are declared within the class definition.
the class either in a These prototypes can take the form of
non-class functions as well as class
program or in the function.
suitable prototypes. Functions can be
declared and defined within the class
definition.
5
CONTENTS
• Defining a Member functions inside
and outside class
• Access specifiers
6
Defining a Member function
There are two ways:
1. Member functions defined inside class
• Do not need scope resolution operator, class name.
2. Member functions defined outside class
• Using Binary scope resolution operator (::)
• “Ties” member name to class name
• Uniquely identify functions of particular class
• Different classes can have member functions with same name
Format for defining member functions outside the class is:
returnType ClassName::MemberFunctionName( )
{
…
}
7
Defining a Member function Inside class
We define the function inside class then we don't not need to declare it first, we can
directly define the function,the class name and the scope resolution operator are
not specified in the function header.Moreover, the member functions defined inside a class
definition are by default inline functions.
class Cube
{
public:
int side;
int getVolume()
{
return side*side*side; //returns volume of cube
}
};
8
Defining a Member function Inside class(Example) Contd…
class sample
{
private:
float x;
float y;
public:
void get()
{
cout<<"enter any two number"<<endl;
cin >>x>>y;
}
9
Defining a Member function Inside class(Example) Contd…
void disp()
{
cout<<"First value = " <<x<<endl;
cout<<"Second value = "<<y<<endl;
cout<<"sum = "<<sum()<<endl;
cout<<"sub = "<<sub()<<endl;
cout<<"mul = "<<mul()<<endl;
cout<<"div = "<<div()<<endl;
}
float sum()
{ 10
Defining a Member function Inside class(Example) Contd…
float sub()
{
return(x-y);
}
float mul()
{
return(x*y);
}
float div()
{
return (x/y);
}
}; 11
Defining a Member function Inside class(Example) Contd…
void main()
{
clrscr();
sample temp;
temp.get();
temp.disp();
temp.sum();
temp.sub();
temp.mul();
temp.div();
getch();
}
12
Defining a Member function outside the class
But if we define the member function outside the class definition then we must declare
the function inside class definition and then define it outside.
The definition of member function outside the class differs from normal function
definition, as the function name in the function header is preceded by the class name and
the scope resolution operator (: :). The scope resolution operator informs the compiler
what class the member belongs to. The syntax for defining a member function outside the
class is
return_type class_name :: function_name (parameter_list)
{
// body of the member function
}
13
Defining a Member function outside the class
class Cube
{
public:
int side;
int getVolume();
} // member function defined outside class definition
int Cube :: getVolume()
{
return side*side*side;
}
14
Defining a Member function outside the class(Example) Contd…
class sam
{
private:
float x;
float y;
public:
void get();
void disp();
float sum();
float diff();
float mul();
float div();
}; //End of the class
15
Defining a Member function outside the class(Example) Contd…
void sam::get()
{
cout<<"Enter any two numbers"<<endl;
cin>>x>>y;
}
void sam::disp()
{
cout<<"First value="<<x<<endl;
cout<<"Second value="<<y<<endl;
cout<<"Sum="<<sum()<<endl;
16
Defining a Member function outside the class(Example) Contd…
cout<<"Diff="<<diff()<<endl;
cout<<"Mul="<<mul()<<endl;
cout<<"Div="<<div()<<endl;
}
float sam::sum()
{
return(x+y);
}
float sam::diff()
{
return(x-y);
}
17
Defining a Member function outside the class(Example) Contd…
float sam::mul()
{
return(x*y);
}
float sam::div()
{
return(x/y);
}
18
Defining a Member function outside the class(Example) Contd…
void main()
{
clrscr();
sam temp;
temp.get();
temp.disp();
temp.sum();
temp.diff();
temp.mul();
temp.div();
getch();
}
19
Access Specifiers
Private
• A member data can only be accessed by the member functions and friends of this
class.
• The member functions and friends of this class can always read or write private
data members.
• Private data members is not accessible to the outside world.
Public
• Can be accessed by any function in the outside world.
• Public implementation operations are also called member functions or methods
or interfaces to out of the class.
Protected
• Members can only be accessed by member functions and friends of this class.
• Not accessible to the outside world
20
A Access Specifiersccess Specifiers
PUBLIC PRIVATE PROTECTED
• Data members and • No one can access the • Makes class member
member functions class members declared inaccessible outside the
declared public can be private outside that class. class.
accessed by other classes • Compile time error • But they can be accessed
too by any subclass of that
class.
• class PublicAccess { • class PrivateAccess { • class ProtectedAccess {
• public: • private: int x; • protected:
// Data • int x; //
• int x; // Member Declaration Data Member
Data Member • void display(); Declaration
Declaration // Member • void display();
• void display(); // Function decaration • // Member
Member Function • } Function decaration
decaration • }
• }
22/02/2022 19
Summary
In this lecture we have We have discussed about how
discussed about Member to define member functions
functions. inside a class.
Discussed about about how to
define member functions Discussed about Access
outside the class. Specifiers.
22
Frequently Asked question
Q1 How to define member functions outside the class?
Defining a member function outside a class requires the function declaration (function
prototype) to be provided inside the class definition. The member function is declared inside
the class like a normal function. The definition of member function outside the class differs
from normal function definition, as the function name in the function header is preceded by
the class name and the scope resolution operator (: :).
Q2 How to define member functions inside the class?
• 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.whenthe member function is defined inside the class
definition it can be defined directly.
23
Q3 What are Access Specifiers?
There are three access specifiers.
• Public - The members declared as Public are accessible from outside
the Class through an object of the class.
• Protected - The members declared as Protected are accessible from
outside the class but only in a class derived from it.
• Private - These members are only accessible from within the class.
24
Assessment Questions:
1. Which among the following best describes member functions?
a) Functions which are defined within the class
b) Functions belonging a class
c) Functions in public access of a class
d) Functions which are private to class
2. A member function can _______________ of the same class.
a) Call other member functions
b) Call only private member functions
c) Call only static member functions
d) Call only const member functions
25
Assessment Questions:
3.Which among the following can be used together in a single class?
a) Only private
b)Private and Protected together
c)Private and Public together
d)All three together
26
Discussion forum.
How to Define inside and outside the class?
https://youtu.be/7VdrwIvu21c
27
REFERENCES
Reference Books:
[1] Programming in C++ by Reema Thareja.
[2] Programming in ANSI C++ by E. Balaguruswamy, Tata McGraw Hill.
[3] Programming with C++ (Schaum's Outline Series) by Byron
Gottfried Jitender Chhabra, Tata McGraw Hill.
Websites:
• https://www.studytonight.com/cpp/member-functions-cpp.php
• https://ecomputernotes.com/cpp/classes-in-c/defining-member-functi
ons
YouTube Links:
https://youtu.be/7VdrwIvu21c
28
THANK YOU