more on classes and objects

Post on 19-May-2015

653 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

MORE ON CLASSES AND OBJECTS

Chapter 4

Data Members

Types of data member :

Constant data members. Mutable data members. Static data members.

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.

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;

Example:

#include<iostream>void main(){const int x = 10;x++; // errorcout<< x<<endl;}

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’.

Example:

#include<iostream>class x{

int a ;mutable int b;

public:void xyz() const

{a++; // errorb++; // legal}};void main(){X x2;X2.xyz();}

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

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.

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

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

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

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:

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

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

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

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

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

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;

}

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;

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

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

Example:

Class Employee{Char name[20];Float salary;Public:Void getdata();Void display();};Employee e[5];

Passing object to functions

By value By reference By pointer

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.

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.

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.

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

Nested class

Class within a class Example:

class A

{

// class body

class B

{

// inner class body

};

}

top related