You are on page 1of 14

Lecture#06

Object Oriented Programming


Topic:
member function define outside the class

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;

Pass Objects to Function:


class Student {
public:
double marks;
Average Marks = 72
// constructor to initialize marks
Student(double m) {
marks = m;
}
};
// function that has objects as parameters
void calculateAverage(Student s1, Student s2) {

// calculate the average of marks of s1 and s2


double average = (s1.marks + s2.marks) / 2;

cout << "Average Marks = " << average << endl;


}
int main() {
Student student1(88.0), student2(56.0);

// pass the objects as arguments


calculateAverage(student1, student2);}
#include <iostream>
using namespace std;

Return Object from a Function:


class Student {
public:
int main() {
double marks1, marks2;
Student student1;
};
// Call function
// function that returns object of Student
student1 = createStudent();
Student createStudent() {
Student student;
return 0;
}
// Initialize member variables of Student
student.marks1 = 96.5;
student.marks2 = 75.0;
Marks 1 = 96.5
// print member variables of Student
cout << "Marks 1 = " << student.marks1 << endl; Marks 2 = 75
cout << "Marks 2 = " << student.marks2 << endl;

return student;
}
Member Functions Defined Outside the
#include <iostream>
using namespace std;

Class
class A {
public:
// Only declaration fun() called
void fun();
};

// Definition outside class using ::


void A::fun() { cout << "fun() called"; }

int main()
{
A a;
a.fun();
return 0;
}
Syntax
The const member function can be defined in three ways:

Const member functions:


1. For function declaration within a class.
return_type function_name() const; 2. For function definition within the class declaration.
return_type function_name () const { //function body }
3. For function definition outside the class.
• functions that are denied permission
return_type to change the values
class_name::function_name() of {the
const data members of their class.
//function
body }
• To make a member function constant, the keyword const is appended to the function prototype and also to
the function definition header.

• 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();

// const member function return 0;


int get_data() const;
};

// Function definition for setting the value of x.


void Demo::set_data(int a) { x = a; } 10

// Function definition for retrieving the value of x (const


// member function).
int Demo::get_data() const { return x; }
#include <iostream>
using namespace std;

class Test {
int value;
20
public:
Test(int v = 0) { value = v; }

// const member function


int getValue() const { return value; }
};

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;
}

You might also like