0% found this document useful (0 votes)
6 views29 pages

PPT Lecture 1.2.2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

PPT Lecture 1.2.2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNITS


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Basic Data Structure Using C ++
Code:24CSH-103

Topic : Defining Member functions DISCOVER . LEARN . EMPOWER


Basic Data Structure
Using C++

Course Objectives

• To enable the students to understand


various stages and constructs of C++
programming language.
• To improve their ability to analyze and
address variety of problems in C++.
• To understand the concept of data
structures and various operations on
them.
• To understand the properties of
various data structures and able to
identify the strengths and weaknesses
of different data structures.
• To analyze and compare the efficiency
of algorithms and learn to design
2
efficient algorithms for solving
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 various concepts of object-oriented
programming language.
CO3 Analyse and explain the behaviour of linear data structure
operations using 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

Direct Evaluation Weightage of actual Final Weightage in Mapping with SIs Remarks
Sl No. Frequency of Task BT Levels CO Mapping Mapping with PIs
Instruments conduct Internal Assessment (ABET) (Graded/Non-Graded)

SO1 1a,1b,1c
10 marks for each
1 Assignment One per unit 10 Hard CO4,CO5 Graded
assignment

SO6
2 Exam 20 marks for one MST 2 per semester 20 Medium CO1,CO2,CO3,CO4 6a,6b Graded

3 Case Study 8 marks 1 per unit 8 Easy CO2 SO1, SO6 1c,6b Graded
NA NA NA NA
One per lecture
4 Homework NA topic (of 2 NA Non-Graded
questions)

NA NA NA NA
5 Discussion Forum NA One per unit NA Non-Graded

NA NA NA NA
6 Presentation NA NA NA Non-Graded

7 Attendance NA NA 2 NA NA NA NA Graded

Remarks
Direct Evaluation Final Weightage in Mapping with SIs
S No. Weightage of actual conduct Frequency of Task BT Levels CO Mapping Mapping with PIs (Graded/Non-
Instruments Internal Assessment (ABET)
Graded)
Unit wise Practical 1a, 1b, 1c, 6a, 6b SO1, SO6
1 15 marks 3 45 Medium 1,2,3,4,5 Graded
Evaluation
1a, 1b, 1c, 6a, 6b
2 Exam 15 marks for one MST 1 per semester 15 Medium 1,2,3 SO1, SO6 Graded
3 Attendance NA NA 2 NA NA NA NA Graded

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…

int main()
{
sample temp;
temp.get();
temp.disp();
temp.sum();
temp.sub();
temp.mul();
temp.div();
}
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: • protected:
int x; • int x;
• int x; // // Data Member // Data Member
Data Member Declaration Declaration
Declaration • void display(); • void display();
• void display(); // // Member • // Member
Member Function Function decaration Function decaration
decaration • } • }
• }

21/01/2025 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

You might also like