0% found this document useful (0 votes)
30 views19 pages

Session 11

The document discusses access specifiers in C++ classes: - Public members can be accessed from anywhere within a program. - Private members can only be accessed within the class itself. - Protected members can be accessed within the class and subclasses. Three examples are provided to demonstrate public, private, and protected access specifiers using classes with member variables and functions. The examples output the values accessed via different access levels.

Uploaded by

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

Session 11

The document discusses access specifiers in C++ classes: - Public members can be accessed from anywhere within a program. - Private members can only be accessed within the class itself. - Protected members can be accessed within the class and subclasses. Three examples are provided to demonstrate public, private, and protected access specifiers using classes with member variables and functions. The examples output the values accessed via different access levels.

Uploaded by

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

18CSC202J - OBJECT ORIENTED DESIGN AND

PROGRAMMING

ACCESS SPECIFIERS
Access control in classes
•public : A public member is accessible from anywhere outside the class but within a program.

•private : A private member variable or function cannot be accessed, or even viewed from
outside the class. Only the class and friend functions can access private members.

•protected
• : A protected member variable or function is very similar to a private member but it
provided one additional benefit that they can be accessed in child classes which are called derived
classes

2
Example 1: C++ public Access Specifier

#include <iostream> int main()


using namespace std; {
class Sample // define a class Sample obj1; // declare a class object
{ cout << "Enter your age: ";
public: // public // store input in age of the obj1 object
elements cin >> obj1.age;
int age;
void displayAge() obj1.displayAge(); // call class function
{
return 0;
cout •<< "Age = " << age << endl; }
}
};
OUTPUT

Enter your age: 30


Age = 30
5
8/24/2022 C ,C++ and UML Basics
Access Specifiers Example 1
#include <iostream>
using namespace std; // Main function for the program

class Line int main( )


{ {
public: Line line;
double length;
void setLength( double len ); // set line length
double getLength( void ); line.setLength(6.0);
}; cout << "Length of line : " << line.getLength() <<endl;

double Line::getLength(void) // set line length without member function


{
return length ; line.length = 10.0; // OK: because length is public
} cout << "Length of line : " << line.length <<endl; return
0;
void Line::setLength( double len ) }
{ Length of line : 6
length = len;
} Length of line :
10

6
Example 2: C++ private Access Specifier

#include <iostream>
using namespace std; int main()
class Sample
{ {
private: // private elements int ageInput;
int age; Sample obj1;
cin >> obj1.age; // displays error
public: // public elements cout << "Enter your age: ";
void displayAge(int a)
cin >> ageInput;
{
age = a; obj1.displayAge(ageInput);

cout << "Age = " << age << endl; return 0;
} }
};

OUTPUT

Enter your age: 30


Age = 30
7
8/24/2022 C ,C++ and UML Basics
Access Specifiers Example 2
#include <iostream> // Main function for the program
using namespace std; int main( )
class Box {
{ Box box;
public:
double length; // set box length without member function
void setWidth( double wid ); box.length = 10.0; // OK: because length is public
double getWidth( void );
cout << "Length of box : " << box.length <<endl;
private:
double width; // set box width without member function
};
// box.width = 10.0; // Error: because width is private
// Member functions definitions double box.setWidth(10.0); // Use member function to set it.
Box::getWidth(void)
{ cout << "Width of box : " << box.getWidth() <<endl;
return width ;
} return 0;
}
void Box::setWidth( double wid )
{
width = wid; Length of box : 10
}
Width of box : 10
8
Example 3 : C++ protected Access Specifier

#include <iostream> int main()


using namespace std; {
class Sample // declare parent class int ageInput;
{
protected: // protected elements // declare object of child class
int age; SampleChild child;
};
cout << "Enter your age: ";
cin >> ageInput;
// declare child class
class SampleChild : public Sample // call child class function
{ child.displayAge(ageInput);
public:

void displayAge(int a) return 0;
{ }
age = a;
cout << "Age = " << age << endl;
}
OUTPUT
};
Enter your age: 30
Age = 30
9
8/24/2022 C ,C++ and UML Basics
Access Specifiers Example 3
#include <iostream> using void SmallBox::setSmallWidth( double wid )
namespace std; {
width = wid;
class Box }
{
protected: double width; // Main function for the program
}; int main( )
{
class SmallBox:Box SmallBox box;
{
public: // set box width using member function
void setSmallWidth( double wid ); box.setSmallWidth(5.0);
double getSmallWidth( void ); cout << "Width of box : "<< box.getSmallWidth();
};
return 0;
// Member functions of child class double }
SmallBox::getSmallWidth(void)
{
return width ; Width of box : 5
}

10
Revision Exercises

Write a C++ program to:

• Write a program using public and private class


members. Declare a data member c,d as private in a
class and use it in the functions (getinput() and
showoutput()) derived in public and call the function

using object of the class

• Write a program using protected access specifier. Use


a variable s,t declared in the protected11 section of the
class in the subclass of that class.Use getinput() and
showoutput() as functions.
8/24/2022 C ,C++ and UML Basics
Friend function
•A friend function of a class is defined outside that class' scope but it has the right to access all private
and protected members of the class.

•Even though the prototypes for friend functions appear in the class definition, friends are not member
functions.

•A friend can be a function, function template, or member function, or a class or class template, in
which case the entire class and all of its members are friends.

12
Friend function
• In C++, a friend function is a function that is declared using the friend keyword to achieve the

encapsulation feature and can access the private and protected data members easily by using

functions.

• To access the data in C++, friend functions are declared inside the body of the class or inside the

private and public section of the class.

13
Friend function Example

#include <iostream> //Global Function


using namespace std; void display (XYZ obj)
class XYZ {
{ cout<<obj.num<<endl;
private: cout<<obj.ch<<endl;
int num=100; }
char ch=‘a’; int main()
public:
{ OUTPUT
friend void display (XYZ obj);
XYZ obj;
}; 100
display(obj); a
return 0;
} 15

8/24/2022 C ,C++ and UML Basics


Inline Function
• C++ provides an inline functions to reduce the function call overhead.

• Inline function is a function that is expanded in line when it is called.

• When the inline function is called whole code of the inline function gets inserted or substituted
at the point of inline function call. This substitution is performed by the C++ compiler at compile
time.

• Inline function may increase efficiency if it is small.


Inline Function - Example
Inline Function - Example
include <iostream>
using namespace std; Output:
inline int Max(int x, int y)
{ Max (20,10): 20
return (x > y)? x : y; Max (0,200): 200
} Max (100,1010): 1010
// Main function for the program
int main() {
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}

You might also like