hits counter
  Create Free Blog | Random Blog »   Report Abuse | Login   

 

“this” pointer:(special pointer)

*“this” is a special type pointer variable which contains the address of the current enable instance.

*That is it represents an object that invoke a member function.

*For example,the functin calls “S.read();” will set he pointer “this” to the address of the object S.

*This unique pointer is automatically passed to a function when it is called.

*The pointer “this” act as an implicit argument to the entire member functions.

*A friend function does not have a “this” pointer.

Program1:

#include<iostream.h>

class TestThis

{

public:

void showThis()

{

cout<<”Address of this is:”<<this<<endl;

}
};

void main()

{
TestThis T1,T2;

cout<<”Address of Object T1 is:”<<&T1<<endl;

T1.showThis();

cout<<”Address of Object T2 is:”<<&T2<<endl;

T2.showThis();

}

Program 2:

#include<iostream.h>

class Money

{

int Rs,Ps;

public:

Money() { }

Money( int Rs,int Ps)

{
this ->Rs=Rs;

this->Ps=Ps;

}
void show()

{

cout<<”Rs:”<<Rs;

cout<<”Ps:”<<Ps<<endl;

}
~Money()

{
cout<<”Memory releasing”<<endl;

}
};

void main()

{

Money *M1; //Pointer Object.

M1=new Money (12,40);

M1->show();

delete M1;

}

Share SocialTwist Tell-a-Friend 

Array of objects

*We can also have array of variables that are type class.Such variables are called array of objects.

*The syntax for declaring an array of objects is exactly same as other type of variable.

*Arrays of objects are accessed just like arrays of other types of variables.

*General format: className ObjectName[size];

*Example: Student S[2];

*The identifier Student is a user defined data type.It can be used to create object.The statement Student S[2]; is an array of object.It contains two objects S[0] and s[1].

Program 1:

#include<iostream.h>

#include<conio.h>

class Student

{

private: int RNo;

char Name[20];

public:void read();

void show();

};

void Student::read()

{
cout<<”Enter the Roll Number”<<endl;

cin>>RNo;

cout<<”Enter the Name”<<endl;

cin>>Name;

}

void Student::show()

{

cout<<”Roll Number is:”<<RNo<<endl;

cout<<”Name is :”<<Name<<endl;

}
void main()

{

Student S[2];

cout<<”Enter the First Record”<<endl;

S[0].read();

cout<<”Enter the Second Record”<<endl;

S[1].read();

clrscr();

cout<<”The Records Are”<<endl;

S[0].show();

S[1].show();

}
Apply: Create a class “Employee” with data members EmpNo,EmpName,Basic Pay and Dept.Calculate the DA,HRA,LIC and PF(assume your own data &%).Using these calculate Gross Pay and Net Pay.

Program 2:
#include<iostream.h>

class demo

{

int A;

public: demo(int x) {A=x;}

void show(){cout<<”A:”<<A<<endl;

}
};

void main()

{

demo d[4]={4,-2,5,1};// or {demo(4),demo(-2),demo(5),demo(1)};

for(int i=0; i<4; i++)

d[i].show();

}

Program 3:

include<iostream.h>

class complex

{
int Rp,Ip;

public: complex(int r,int i)

{

Rp=r;

Ip=r;

}

void show()

{

if(Ip<0) cout<<Rp<<Ip<<”k”<<endl;

else cout<<Rp<<”+”<<Ip<<”k”<<endl;

}

};

void main()

{

complex C[2][2]={ complex(4,-2),complex (2,7),

complex(5,2),complex(3,-5)

};

cout<<”Complex numbers are \n”;

for (int i=0;i<2; i++)

for(int j=0; J<2;j++)

C[i][j].show();

}

Share SocialTwist Tell-a-Friend 

Object pointers

*In all the previous sections,you have been accessing members of an class by using the dot(.) operator.This is correct,when you are working with an object.

*We can also access a member of a class via a pointer object.When a pointer is used,the arrow operator(->) is used to call a member function.

*To declare a pointer object,specify its class name and then precede the object name with an asterisk(*).

*To obtain the address of an object,precede the object with & operator.

Example:

Complex C(3,4);

Compex *P;

P=&C;

P->show();

Copy Constructor

*One of the more important forms of an overloaded constructor is the copy constructor.

*A copy constructor take a reference to an object of the same class as itself as an argument.

*Once defined,the copy constructor is called whenever an object is to initialize another.

*The copy constructor only applies to initializations.It does not apply to assignments.

*The most form of copy constructor is:

className(className &obj)

{
…………..//body of constructor

…………..

}

Here obj is a reference to an object that is being used to initialize another object.

Program:

#include<iostream.h>

class Money

{

int Rs, Ps;

public:

Money( ){ }

Money( int R,int P)

{

Rs=R;

Ps=P;

}

Money(Money &M) //copy constructor function

{

Rs=M.Rs;

Ps=M.Ps;

}

void show()

{

cout<<”Rs:”<<Rs;

cout<<”Ps:”<<Ps<<endl;

}

};

void main()

{

Money M1(12,40);

Money M2(M1);  or Money M2=M1; //call the copy constructor

M1.show();

M2.show();

}

Note:

It is possible to creat copy constructors that take additional arguments,as long as the additional arguments have default values.

Example:

Money (Money &M,int x=0)

{

……………//body of constructor

…………..

}

is called as Money M2(M1,20).

Share SocialTwist Tell-a-Friend