Explain pointers to objects, C/C++ Programming

Assignment Help:

Pointers to Objects

Passing and returning of objects is, though, not very efficient since it includes passing and returning a copy of the data members. This problem can be eliminated using pointers. Like other variables, objects of class can also have pointers. Declaring a pointer to an object of a certain class is same as declaring a pointer to a variable of any other data type. A pointer variable having the address of an object is said to be pointing to that object. Pointers to objects can be used to make a call by address or for dynamic memory allocation. Just like structure pointer, a pointer to an object also uses the arrow operator to access its members. Like pointers to other data types, a pointer to an object also has only one word of memory. It has to be made to point to an already existing object or allocated memory using the keyword 'new'.

e.g.

                string str;             // Object

                string *sp;           // Pointer to an object

                sp = &str;             // Assigns address of an existing object

                sp = new string // Allocates memory with new.

A simple example of using pointers to objects is given below.

                class player

                 {

                  public :

                                                void getstats(void);

                                                void showstats(void);

                  private :                                                            

                                                char  name[40];

                                                int   age;

                                                int   runs;

                                                int   tests;

                                                float average;

                                     float calcaverage(void);

                };

 void main()

                  {

                    player Sachin;

                                Sachin.getstats();

                                Sachin.showstats();

                  player  *Dravid;

                                Dravid = new player;

 

                                Dravid->getstats();

                                Dravid->showstats();

                  }

 


Related Discussions:- Explain pointers to objects

Explain nested classes, Nested Classes Many a times, it becomes essenti...

Nested Classes Many a times, it becomes essential to have a class contain properties of two other classes. One way is to explain a class within another - that is a class with m

Explain the standard input/output iostream library, Question 1 Implemen...

Question 1 Implement a class stack which simulates the operations of the stack allowing LIFO operations. Also implement push and pop operations for the stack 2 Explain the c

C program to print fibonacci series, C program to print fibonacci series: ...

C program to print fibonacci series: int fibo(long int); void main() {                 long int a=0,n;                 printf ("how many terms\n");

Algorithm, make a marksheet of 2 student with 5 subject

make a marksheet of 2 student with 5 subject

Pointer to an array, Write a method (belonging to the TermStructure class) ...

Write a method (belonging to the TermStructure class) that takes a pointer to an array of bonds and an integer, representing the number of bonds in the array as arguments, and esti

Explain static variables, Static Variables Static variables have the si...

Static Variables Static variables have the similar scope s automatic variables, but, unlike automatic variables, static variables retain their values over number of function ca

Explain the switch construct, The Switch Construct The switch statement...

The Switch Construct The switch statement is a multi-way decision-making construct that tests an expression matches one of a number of constant values, and branches accordingly

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd