Presentation

Preview:

DESCRIPTION

OOPS concpets in c++

Citation preview

Achariya School of Business & Technology

PRESENTATION

ON

POINTERS TO OBJECT,THIS POINTER

& DERIVED CLASS

Pointer to objectPointer to object

A pointer can point to an object created A pointer can point to an object created by a class.by a class.

Item x;Item x;

where where itemitem -- is a class -- is a class

xx -- is an object defined to be of -- is an object defined to be of type item type item

Similarly Similarly it_ptrit_ptr of type of type itemitem

Item *it_ptr;Item *it_ptr;

Object pointers are useful in creating Object pointers are useful in creating

object at run time. object at run time.

We can also use an object pointer to We can also use an object pointer to

access the public members of an object.access the public members of an object.

Consider a classConsider a class item item defined as follows: defined as follows:

Class item {

Item code;Float price;

Public:Void getdata (int a, float b) {

Code =a;Price=b;

}Void show (void)

{Cout<<”code :”<< price<<”\n”Cout<<”price :”<< price<<\n;

}};

Let us consider item variableLet us consider item variable x x and a pointer and a pointer ptrptr to to xx as follows: as follows:

Item x;Item x;

Item *ptr = &x;Item *ptr = &x;

The pointer ptr is initialized with the The pointer ptr is initialized with the address of xaddress of x

To refer member function of item in two waysTo refer member function of item in two ways Dot operator and the object Dot operator and the object

x.getdata (100, 75.50);x.getdata (100, 75.50);

x.show ();x.show ();

the arrow operator and the object pointer.the arrow operator and the object pointer.

ptr->getdata(100, 75.50);ptr->getdata(100, 75.50);

ptr-> show();ptr-> show();

We can also create the object using pointers We can also create the object using pointers and new operator as follows:and new operator as follows:

Item *ptr = new item;Item *ptr = new item; This statement allocates enough memory for This statement allocates enough memory for

data members in the object structure and data members in the object structure and assigns the address of the memory space to assigns the address of the memory space to ptrptr..

We can also create an array of objects using We can also create an array of objects using pointers. For example,pointers. For example,

Item *ptr = new item[10];Item *ptr = new item[10];

It creates memory space for an array of It creates memory space for an array of 10 object of 10 object of item.item.

Program for pointer to object #include<iostream.h> Using name space std; Class item { Int code; Float price; Public: Void getdata(int a, float b) { Code=a; Price=b; } Void show(void) { Cout<<”code:”<<code<<”\n”; Cout<<”price:”<<price<<”\n”; } };

Const int size = 2;Int main(){

Item *p=new item[size];Item *d=p;Int x, i;Float y;For (i=0; i<size; i++)

{Cout<<”Input code and price for item”<<i+1;Cin>>x>>y;p->getdata(x.y);p++;

}

For (i=0; i<size; i++)

{

Cout<<”Item:”<<i+1<<”\n”;

d->show();

d++;

}

Return 0;

}

Output Input code and price for item1 40 500 Input code and price for item 50 500 Item 1 Code: 40 Price: 500 Item 2 Code: 50 Price: 600

This PointerThis Pointer C++ uses a unique keyword calledC++ uses a unique keyword called

thisthis to represent an object that to represent an object that invokes a member function.invokes a member function.

thisthis is a pointer that points to the is a pointer that points to the object for which object for which thisthis function was function was called.called.

The starting address is the same The starting address is the same as the address of the first variable as the address of the first variable in the class structurein the class structure

The unique pointer is automatically The unique pointer is automatically passed to a member function when it passed to a member function when it is called.is called.

The pointer The pointer thisthis acts as an implicit acts as an implicit argument to all the member functionargument to all the member functionClass abcClass abc{{Int a;Int a;--------------------};};

The private variable ‘a’ can be use The private variable ‘a’ can be use directly inside a member directly inside a member function,likefunction,like

a=123;a=123; The following statement to do the The following statement to do the

same job:same job:

this ->a=123;this ->a=123; However, we have been implicitly However, we have been implicitly

using the pointer this when using the pointer this when overloading the operators using overloading the operators using member function member function

The other arguments is implicitly The other arguments is implicitly passed using the pointerpassed using the pointer this this. One . One important application of the pointer this important application of the pointer this is to return the object it points to. For is to return the object it points to. For exampleexample

Return *this;Return *this; Inside a member function will return Inside a member function will return

the object that invokes the function the object that invokes the function This statement assumes importance This statement assumes importance

when we want to compare two or more when we want to compare two or more objects inside a member function and objects inside a member function and return the return the invoking objectinvoking object as a result. as a result. For exampleFor example

Person & person :: greater(person & x)Person & person :: greater(person & x)

{{

If x.. age > ageIf x.. age > age

Return x;Return x; //argument object//argument object

ElseElse

Return *this; Return *this; //invoking object //invoking object

}} Suppose we invokes this function by the callSuppose we invokes this function by the call

Max=A. greater (B);Max=A. greater (B);

The function will return the object B (arguments

object) if the age of the person B is greater than that of

A, otherwise it will return the object A ( invoking

object) using the pointer this.

Program for this pointer:

#include<iostream.h>

#include<cstring.h>

Using namespace std;

Class person

{

Char name[20];Float age;Public:Person(char *s, float a){

Strcpy(name, s);age=a;

}Person & person :: greater(person & x){

If (x..age >= age)Return x;

else Return *this;

}

Void display(void)

{

Cout<<”Name :”<<name<<”\n”;

Cout<<”Age:”<<age<<”\n”;

}

};

Int main()

{

Person p1(“john”,37.50),

p2(“ahmed”,29.0),

p3(“hebber”,40.25);

person p= p1.greater (p3); //p3.greater(p1)

cout<<”Elder person is :\n”;

p.display ();

p=p1.greater(p2); //p2.greater(p1)

cout<<”Elder person is:\n”;

p.display();

Return 0;

}

Output:

Elder person is: Name: hebber Age: 40.25 Elder person is: Name: john Age: 37.5

Pointer to derived classPointer to derived class

We can use pointers not only to the base objects but also to the objects of derived classes.

Pointers to objects of base class are type-compatible with pointers to objects of a derived class.

Therefore , a single pointer variable can made to point to objects belonging to different classes

• For example B is a base class and D is derived class from B, then a pointer declared as a pointer to B can also be a pointer to D. consider following declarations

B *cptr //pointer to class B type variable

B b; //base object D d; //derived class Cptr = &b //cptr points to object bCptr = &d; //cptr points to object d

• Although c++ permits a base pointer to point to any object derived from that base, the pointer cannot be directly used to access all the member of the derived class. We may have to use another pointer declared as pointer to the derived class.

Program for pointers to derived class

#include<iostream.h> Using namespace std; Class BC {

Public:Int b;Void show()

{Cout<<”b=”<<b<<”\n”;

}};

Class DC : public BC

{

Public:

Int d;

Void show()

{

Cout<<”b=”<<b<<”\n”;

Cout<<”d=”<<d<<”\n”;

}

};

Int main()

{

BC *bptr; //base pointer

BC base;

bptr= &base; //base address

bptr->b = 100; //access BC via base address

cout<<”bptr points to base object \n”;

bptr->show();

//derived class

DC derived;

Bptr= &derived; //address of derived class

Bptr-> b=200; //access DC via base class

Cout<<”bptr now points to derived object \n”;

Bptr->show();

DC *dptr ;

dptr= &derived; //derived type pointer

dptr->d= 300;

cout<<”dptr is derived type pointer \n”;

dptr->show();

cout<<”using ((DC *)bptr) \n”;

((DC *)bptr)-> d= 400;

((DC *)bptr)-> show();

Return 0;

}

Output bptr points base object b=100 bptr now points to derived object b=200 dptr is derived type pointer b=200 d=300 Using ((dc *)bptr) b=200 d=400

Recommended