0% found this document useful (0 votes)
14 views

Class Item (Int Code Float Price Public: Void Getdata (Int A, Float B) (Code A Price B ) Void Show (Cout Code Price ) )

This document contains examples of pointers to objects, derived objects, data members, member functions, and memory leaks in C++. It demonstrates how to: 1) Create a pointer to an object array and access members through those pointers. 2) Create a base class pointer that can point to both base and derived class objects, but access only base members through the base pointer. 3) Declare and access members through pointers to data members and member functions. 4) Cause a memory leak by allocating memory with new that is not later freed.

Uploaded by

Er Ashish Baheti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Class Item (Int Code Float Price Public: Void Getdata (Int A, Float B) (Code A Price B ) Void Show (Cout Code Price ) )

This document contains examples of pointers to objects, derived objects, data members, member functions, and memory leaks in C++. It demonstrates how to: 1) Create a pointer to an object array and access members through those pointers. 2) Create a base class pointer that can point to both base and derived class objects, but access only base members through the base pointer. 3) Declare and access members through pointers to data members and member functions. 4) Cause a memory leak by allocating memory with new that is not later freed.

Uploaded by

Er Ashish Baheti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Pointer to object

Class item { int code; float price; public: void getdata(int a,float b) { code=a; price=b; } void show() { cout<<code<<price; } };
const int size=2; 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; cin>>x>>y; p->getdata(x,y); p++; } for(i=0;i<size;i++) { d ->show(); d ++; } }

class BC { public: int b; void show() { cout<<b; } }; class DC:public BC { public: int d; void show() { cout<<b; cout<<d; } };

Pointer to derived objects


main() { BC *bptr; BC base; bptr=&base; bptr->b=100; bptr->show(); DC derive; bptr=&derive; bptr->b=200; // bptr->d=300; bptr->show(); DC *dptr; dptr=&derive; dptr->d=300; dptr->show(); getch(); } //base pointer //base address //acess via base pointer

//address of derived object //wont work //bptr points to derived object

Pointer to data member


It is possible to take the address of a member of a class and assign it to the pointer. The address of a member can be obtained by applying the operator & to class member name. Class A { Private: Int m; Public: Void show(); }; //We can define a pointer to the member m as follows: int A::*ip=&A::m; Cout<<a*ip; //a is object

Ip pointer is created thus acts like a class member in that it must be invoked with a class object A::* means pointer to member of A class &A::m means the address of the m member of A class Int *p=&m//wont work Bcz m is not simply int type data.it has meaning only when it is associated with class

Pointer to member function


Syntax <Function return type><class name followed by :: pointer name> <function parameter list> Class A { Public: Int func(); }; Int(A::*pn)();

Class x { public: int a; void f(int b) { cout<<b; } }; main() { int x::*p=&x::a; //declare to pointer member void(x::*pi)(int)=&x::f; //declare pointer to member function x c; //create object of class c.*p=10; cout<<value of a is<<c.*p; (c.*pi)(20); //call member function } o/p: Value of a is 10 20

Memory leak
A memory leak occurs when a piece of memory that was previously allocated by a programmer is not properly deallocated by the programmer Even though that memory is no longer in use by the program,it is still reserved and the piece of memory cannot be used by the programm until it is properly deallocated by the programmer Char *p,*q p=new char[10000]; q=new char[50000];

You might also like