A member function perform 2 types of operation
-Modification or modifier
-Accessor
What is a Modifier in C++ ?
A modifier is a function which modify the statement of the object
A member function which modify values of the object is called modifier function mutator function
What is Accessor in C++ ?
An accessor is member function which does not modify values of the object.
Example:
WAP to find area of rectangle:
#include <iostream.h>
class triangle
{
float l,b;
public:
void read()
{
cout<<”\n Input l,b values”;
cin >>l >>b;
}
void find-area()
{
float a=l*b:
cout<<”\n Area is “<<a;
}
};
void Main()
{
rectangle reactangle1;
rectangle1.read();
rectangle1.find-area();
rectangle.rectangle 2;
rectangle2. read();
rectangle2.find_area();
}
o/p :
Input l,b values Input l,b values
1.2 2.5
3.5 3.5
area is 42 Area is 8.75
Dynamic objects -
Object created inside heap area called dynamic object.
An object created during run time called dynamic object
This object is created using new operator
New operator create object and return address this address, this address hold by pointer variable
Syntax
Declare pointer variable of type class (it holds address of object)
class-name * variable_name;
int *p;
int *q;
float *r;
double *d;
rectangle *r;
Creating object
pointer-variable-name=new class-name
// wap to find the area of circle
#includle<iostream.h>
#include<conio.h>
class circle
{
float r;
public:
void main()
{
cout<<”\n input r”;
ci>>r;
}
void find_area()
{
float a=3.147 * r *r;
cout<<”\n area is”<<a;
}
}
void main()
{
circle circle1;
circle1=new circle;
(*circle1).read();
(*circle1).find_area();
delete circle1;
circle1=NULL;
}
o/p : input r
r=1.5
Area is
Example
WAP to find result of student
#include<iostream.h>
class student
{
protected : (not access non members)
int no;
public :
void read_student()
{
cout<<”\n input r no”;
cin>>rno;
cout<<”\n input two subject marks”;
void find_result()
{
cout<<”\n rollno”<<rno;
if(sub<40 || sub<40)
cout<<”\n result fail”;
else
cout<<”\n result pass”;
}
};
void main()
{
student *stud1;
stud1=new student;
(*stud1).read_student();
(*stud1).find_result();
delete.stud1;
stud1=NULL;
}
Note : The class which doesn’t have any data member whose size 1 byte class is not empty data type
Defining Member Function
C++ allows to define member function inside class and outside class
- inside class
- outside class
Inside class
- IF member function definition is given within class if becomes inline.
- Inline functions are expanded during compile time.
- Q-15 is pos to declare a function as inline without using keyword inline ?
Ans : Yes, writing function inside class
eg:
class Alpha
{
public :
void fun1() void man()
{ {
} ; compile -> Alpha alpha
alpha;
}
void main()
{
alpha apha1;
alpha1.func1();
}
Outside class
Member function definition is given outside the class in order to avoid inline.
Syntax:
<returntype>
<class_name>::<function-name>(parameters)
{
statements;
}
eg:
# include<iostream.h>
class time
{
int hh,mm,ss;
public :
void read_time();
void print_time(); Declaration
};
void time:: read_time()
{
cin>>hh>>mm>>ss;
}
void time :: print_time()
{
cout<<endl<<hh<<”lt”<<MM<<”lt”<<ss;
}
void main()
{
time time_in;
time time_out;
time_in.read_time();
time_out.read_time();
time_in.print_time();
time_out.print_time();
}