more on classes and objects
TRANSCRIPT
![Page 1: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/1.jpg)
MORE ON CLASSES AND OBJECTS
Chapter 4
![Page 2: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/2.jpg)
Data Members
Types of data member :
Constant data members. Mutable data members. Static data members.
![Page 3: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/3.jpg)
Member Functions
Different types of member functions: Nested Member functions. Overloaded member functions. Constant member function. Member functions with default
arguments. Inline member functions. Static member functions.
![Page 4: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/4.jpg)
Constant data members
The data members whose value cannot be changed throughout the execution of the program.
They are declared by preceding the qualifier const.
Example:const int x = 10;
![Page 5: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/5.jpg)
Example:
#include<iostream>void main(){const int x = 10;x++; // errorcout<< x<<endl;}
![Page 6: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/6.jpg)
Mutable data members
If the need arises such that the constant member functions has to modify the value of the data members then the data member has to be declared by prefixing the keyword ‘mutable’.
![Page 7: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/7.jpg)
Example:
#include<iostream>class x{
int a ;mutable int b;
public:void xyz() const
{a++; // errorb++; // legal}};void main(){X x2;X2.xyz();}
![Page 8: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/8.jpg)
Constant member function
#include<iostream.h>
class x
{
int a;
public:
void getdata(int x)
{
a=x;
}
int setdata() const
{
a++; // error
return a;
}
};
void main(){x x1;x1.getdata(56);Cout<< x1.setdata()<<endl;}
![Page 9: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/9.jpg)
Static data member
Those members whose members are accessed by all the objects of a class.
It is not own by any object of a class. Only one copy of a static data member is
created for a class which can be accessed by all the objects of that class.
![Page 10: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/10.jpg)
Example:
#include<iostream.h>
class x
{
static int a;
int b;
public:
void getdata(int x)
{
b=x;
a++;
}
void display(void)
{
cout<< a<< endl;
}
};
int x :: a;
void main(){x x1,x2;x1.display();x2.display();x1.getdata(1);x2.getdata(2);x1.display();x2.display();}
Output:0022
![Page 11: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/11.jpg)
Static member function
#include<iostream.h>
class sample
{
static int a;
public:
Static void getdata(int x)
{
a=x;
}
void display(void)
{
cout<< a<< endl;
}
};
Int sample :: a;
void main(){sample s1,s2;Sample :: getdata(1)//invoking static member functions1.display();s2.getdata(2);// invoking static member function using objects2.display();}Output:12
![Page 12: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/12.jpg)
Nested member function
#include<iostream>
class sample
{
int x;
public:
void get_data(int);
void message(char *);
};
int sample :: get_data(int a)
{
x=a;
message(“Nested member function”);
return x;
}
void sample :: message(char *s)
{
cout<< s<< endl;
}
void main(){sample e;t =e.get_data (34);cout<< t << endl; }Output:Nested member function34
![Page 13: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/13.jpg)
Overloaded member function
Class A{Public:void display(void);void display(int);};void a :: display(void){cout<< “Hello”<< endl;}void a :: display(int d){cout<<d<< endl;}
Void main(){A a1;a1.display(void);a1.display(20);}Output:Hello20
With single class:
![Page 14: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/14.jpg)
Overloaded member function
Two different classes.Class A{Public:void display(void);};Class B{Public:void display(void);};
void A :: display(void){cout<< “Hello”<< endl;}void B :: display(void){cout<<“World”<< endl;}
Void main(){A a1;B b1;a1.display(void);b1.display(void);}Output:HelloWorld
![Page 15: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/15.jpg)
Member functions with default arguments
#include<iostream>class addition{Public:
void add(int, int = 2);};void addition :: add(int a, int b){return(a+b);}Void main(){addition a;a.add(5,6);a.add(6);}Output118
![Page 16: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/16.jpg)
Inline function
Class test{private :
int a;int b;
public:void set_data(int , int )int big() // automatic inline
function {if (a > b)return a;elsereturn b;}
};inline void test :: set_data(int x, int y)
{a=x;b=y;}
void main(){test t;int a,b;cout<<“enter the two numbers” << endl;cin>> a >> b;t.set_data(a,b);cout<<“the largest number is ” << t.big() << endl;}
![Page 17: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/17.jpg)
Friend function
To provide non-member function to access private data member of a class, c++ allow the non-member to be made friend of that class.
Syntax:friend<data_type> <func_name>();
![Page 18: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/18.jpg)
Example
Friend non-member function:
Class sample{Int a;Public:Friend void change(sample &);};Void change(sample &x){x.a= 5;}Void main(){Sample A;Change(A);}
![Page 19: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/19.jpg)
Example
Friend member function:
Class sample; // forward declaration.
Class test
{
Public:
Void set_data(sample &, int);
};
Class sample
{
Private:
Int x;
Public:
Int get_data(void);
Friend void test :: set_data(sample &, int);
};
Void test :: set_data(sample &a, int b){a.x= b;}Int sample :: get_data(void){Return x;}Void main(){Sample e;Test f;f.set_data(5);Cout<< e.get_data()<< endl;
}
![Page 20: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/20.jpg)
Friend class
A class is made friend of another class. For example,If a class X is a friend of class Y then all the member function of class X can access the private data member of class Y.
Declaration:friend class X;
![Page 21: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/21.jpg)
Example of friend class
# include<iostream>
Class Y;
Class X
{
Int x,y;
Public:
Void show_data();
friend class Y;
};
Class Y
{
Public:
void change_data( X &, int, int);
};
void Y :: change_data(X &c, int p, int q){c.X = p;c.Y = q;}void X :: show_data(){Cout<< x << y << endl;}Int main(){X x1;Y y1;Y1.change_data(x1,5,6); x1.show_data();return 0;}
![Page 22: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/22.jpg)
Array of class objects
Array of class objects is similar to the array of structures.
Syntax:Class <class_name>{// class body};<class_name><object_name[size]>;
![Page 23: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/23.jpg)
Example:
Class Employee{Char name[20];Float salary;Public:Void getdata();Void display();};Employee e[5];
![Page 24: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/24.jpg)
Passing object to functions
By value By reference By pointer
![Page 25: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/25.jpg)
By value
Here only the copy of the object is passed to the function definition.
The modification on objects made in the called function will not be reflected in the calling function.
![Page 26: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/26.jpg)
By reference
Here when the object is passed to the function definition, the formal argument shares the memory location of the actual arguments.
Hence the modification on objects made in the called function will be reflected in the calling function.
![Page 27: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/27.jpg)
By pointer
Here pointer to the object is passed. The member of the objects passed are accessed by the arrow operator(->).
The modification on objects made in the called function will be reflected in the calling function.
![Page 28: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/28.jpg)
Example
#include<iostream>
class A
{
int a;
public:
void set(A, int); // call by value
void set(int, A &); // call by reference
void set(A *, int); // call by pointer
};
void set(A x,int p)
{
x.a = p;
}
void set(int q, A &y)
{
y.a = q;
}
void set(A *z,int t){z->a = t;}
int main(){A a1;a1.a = 10;cout<<a;a1.set(a1,5);// by valuecout<<a;a1.set(20,a1);// by referencecout<<a;a1.set(&a1,30);// by pointercout<<a;}
Output:10102030
![Page 29: More on Classes and Objects](https://reader036.vdocument.in/reader036/viewer/2022062405/555ad897d8b42a024a8b4dcf/html5/thumbnails/29.jpg)
Nested class
Class within a class Example:
class A
{
// class body
class B
{
// inner class body
};
}