0% found this document useful (0 votes)
29 views

Encapsulation Const Non Const Function

Const member functions in C++ allow calling an object's functions without modifying the object. They are declared with the const keyword. A const member function can be called on both const and non-const objects, while non-const functions can only be called on non-const objects. Encapsulation in C++ binds an object's data and functions that manipulate that data, keeping them safe from outside interference. It is achieved by declaring data as private and functions that access that data as public. This hides implementation details and exposes a public interface. The Adder class example demonstrates encapsulation by having a private total data member accessed by public addNum() and getTotal() functions.

Uploaded by

Aqsa
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)
29 views

Encapsulation Const Non Const Function

Const member functions in C++ allow calling an object's functions without modifying the object. They are declared with the const keyword. A const member function can be called on both const and non-const objects, while non-const functions can only be called on non-const objects. Encapsulation in C++ binds an object's data and functions that manipulate that data, keeping them safe from outside interference. It is achieved by declaring data as private and functions that access that data as public. This hides implementation details and exposes a public interface. The Adder class example demonstrates encapsulation by having a private total data member accessed by public addNum() and getTotal() functions.

Uploaded by

Aqsa
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/ 3

Const and Non-Const member Functions

The const member functions are the functions which are declared as constant in the
program. The object called by these functions cannot be modified. It is recommended to
use const keyword so that accidental changes to object are avoided.
A const member function can be called by any type of object. Non-const functions can be
called by non-const objects only.
Here is the syntax of const member function in C++ language, datatype function_name
const();
Here is an example of const member function in C++,

Example

#include<iostream>
using namespace std;
class Demo {
int val;
public:
Demo(int x = 0) {
val = x;
}
int getValue() const {
return val;
}
};
int main() {
const Demo d(28);
Demo d1(8);
cout << "The value using object d : " << d.getValue();
cout << "\nThe value using object d1 : " << d1.getValue();
return 0;
}
Output
The value using object d : 28
The value using object d1 : 8
Encapsulation in C++

All C++ programs are composed of the following two fundamental elements −
Program statements (code) − This is the part of a program that performs actions and they
are called functions.
Program data − The data is the information of the program which gets affected by the
program functions.
Encapsulation is an Object Oriented Programming concept that binds together the data
and functions that manipulate the data, and that keeps both safe from outside
interference and misuse. Data encapsulation led to the important OOP concept of data
hiding.
Data encapsulation is a mechanism of bundling the data, and the functions that use them
and data abstraction is a mechanism of exposing only the interfaces and hiding the
implementation details from the user.
C++ supports the properties of encapsulation and data hiding through the creation of
user-defined types, called classes. We already have studied that a class can contain
private, protected and public members. By default, all items defined in a class are private.
For example −
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The variables length, breadth, and height are private. This means that they can be
accessed only by other members of the Box class, and not by any other part of your
program. This is one way encapsulation is achieved.
To make parts of a class public (i.e., accessible to other parts of your program), you must
declare them after the public keyword. All variables or functions defined after the public
specifier are accessible by all other functions in your program.
Making one class a friend of another exposes the implementation details and reduces
encapsulation. The ideal is to keep as many of the details of each class hidden from all
other classes as possible.
Data Encapsulation Example
Any C++ program where you implement a class with public and private members is an
example of data encapsulation and data abstraction. Consider the following example −
#include <iostream>
using namespace std;
class Adder {
public:
// constructor
Adder(int i = 0) {
total = i;
}
// interface to outside world
void addNum(int number) {
total += number;
}

// interface to outside world


int getTotal() {
return total;
};
private:
// hidden data from outside world
int total;
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() <<endl;
return 0;
}

When the above code is compiled and executed, it produces the following result −
Total 60
Above class adds numbers together, and returns the sum. The public members addNum
and getTotal are the interfaces to the outside world and a user needs to know them to
use the class. The private member total is something that is hidden from the outside
world, but is needed for the class to operate properly.
Designing Strategy
Most of us have learnt to make class members private by default unless we really need to
expose them. That's just good encapsulation.

This is applied most frequently to data members, but it applies equally to all members,
including virtual functions.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy