Professional Documents
Culture Documents
ASMA JADOON
Member Functions Defined Outside the
Class // Member functions definitions
double Box::getVolume(void) {
return length * breadth * height;
int main() { }
Box Box1; // Declare Box1 of type Box
#include <iostream> Box Box2; // Declare Box2void Box::setLength(
of type Box double len ) {
using namespace std; length of
double volume = 0.0; // Store the volume = len;
a box here
class Box { Volume of Box1 : 210 }
Box1.setLength(6.0);
public: Box1.setBreadth(7.0); void Box::setBreadth( double bre ) {
double length; Volume
// Length of a of
boxBox2 : 1560 breadth = bre;
Box1.setHeight(5.0);
double breadth; // Breadth of aBox2.setLength(12.0);
box }
// box 2 specification
double height; // Height of a box
Box2.setBreadth(13.0); void Box::setHeight( double hei ) {
double getVolume(void); Box2.setHeight(10.0); height = hei;
void setLength( double len ); volume = Box1.getVolume(); }
void setBreadth( double bre ); cout << "Volume of Box1 : " << volume <<endl;
void setHeight( double hei ); volume = Box2.getVolume();
}; cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
#include <iostream>
using namespace std;
return student;
}
Member Functions Defined Outside the
#include <iostream>
using namespace std;
Class
class A {
public:
// Only declaration fun() called
void fun();
};
int main()
{
A a;
a.fun();
return 0;
}
Syntax
The const member function can be defined in three ways:
• Member functions and member function arguments, the objects of a class can also be declared as const.
• An object declared as const cannot be modified and hence, can invoke only const member functions as these
functions ensure not to modify the object.
• A const object can be created by prefixing the const keyword to the object declaration.
• Any attempt to change the data member of const objects results in a compile-time error.
Important Points:
• When a function is declared as const, it can be called on any type of object, const object as well as non-
const objects.
• Whenever an object is declared as const, it needs to be initialized at the time of declaration. however, the
object initialization while declaring is possible only with the help of constructors.
• A function becomes const when the const keyword is used in the function’s declaration. The idea of const
functions is not to allow them to modify the object on which they are called.
• It is recommended practice to make as many functions const as possible so that accidental changes to
objects are avoided.
#include <iostream>
using namespace std;
class Demo {
int x; 11
public:
void set_data(int a) { x = a; }
// non const member function
// data can be updated
int get_data()
{
++x;
return x;
}
};
main()
{
Demo d;
d.set_data(10);
cout << d.get_data();
return 0;
}
#include <iostream>
using namespace std;
class Demo { /tmp/LdMDXY9iKv.cpp: In member function 'int Demo::get_data() const':
int x; /tmp/LdMDXY9iKv.cpp:15:11: error: increment of member 'Demo::x' in read-
public: only object
void set_data(int a) { x = a; } 15 | ++x;
| ^
// constant member function /tmp/LdMDXY9iKv.cpp: At global scope:
int get_data() const /tmp/LdMDXY9iKv.cpp:28:1: error: expected declaration before '}' token
{ 28 | }
// Error while attempting to modify the data | ^
// member
++x;
return x;
}
};
int main()
{
Demo d;
d.set_data(10);
cout << endl << d.get_data();
return 0;
}
}
#include <iostream> main()
using namespace std; {
Demo d;
class Demo { // Set the value of x to 10 using the non-const member
int x; // function.
d.set_data(10);
public: // Print the value of x using the const member function.
void set_data(int); cout << d.get_data();
class Test {
int value;
20
public:
Test(int v = 0) { value = v; }
int main()
{
// non const object
Test t(20);
cout << t.getValue();
return 0;
}
#include <iostream>
using namespace std; int main()
class Demo { {
int value; // Constant object are initialised at the time of
// declaration using constructor
public: const Demo d1;
Demo(int v = 0) { value = v; } //d1.showMessage();Error occurred if uncomment.
void showMessage() d1.display();
{ return (0);
cout << "Hello World We are BEE-3C, " }
"Programming is a FUN"
" showMessage() Function"
<< endl; Hello world
}
I am in OOP class
// const member function
void display() const I am Inside display() Function
{
cout << "Hello world"<<endl<< "I am in OOP class
"<<endl<<"I am Inside display() Function"
<< endl;
}
};
#include <iostream>
using namespace std;
class Test {
int value;
./d869c7ba-f199-4a67-9449-3936b5db4c5b.cpp: In function 'int
public: main()': ./d869c7ba-f199-4a67-9449-3936b5db4c5b.cpp:14:24:
Test(int v = 0) { value = v; } error: passing 'const Test' as 'this' argument of 'int
Test::getValue()' discards qualifiers [-fpermissive] cout <<
// non const member function t.getValue();
int getValue() { return value; }
};
int main()
{
// const object
const Test t;
cout << t.getValue();
return 0;
}
#include<iostream>
using namespace std;
class Demo { The value using object d : 28 The value using object d1 : 8
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;
}