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

T2U4_Pointer_to_class_and_object

The document explains the concept of pointers in relation to classes and objects in programming. It describes how a pointer can hold the address of a class and access its member variables, providing examples with a class named 'book' and an object of class 'man'. A sample program is included to demonstrate the declaration of an object and a pointer, along with the usage of pointer syntax to access member variables.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

T2U4_Pointer_to_class_and_object

The document explains the concept of pointers in relation to classes and objects in programming. It describes how a pointer can hold the address of a class and access its member variables, providing examples with a class named 'book' and an object of class 'man'. A sample program is included to demonstrate the declaration of an object and a pointer, along with the usage of pointer syntax to access member variables.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Pointer to Class, Object

· We know that a pointer is a variable that holds the address of another data variable.

· The variable may be of any data type , that is int , float m or double .

· In the same way , we can also define pointer to class . Here , the starting address of the member
variable can be accessed , thus such pointer are called class pointers

Example

class book

char name[30];

int pages;

};

(a) class book *ptr;

(or)

struct book *ptr;

in the above example , *ptr is a pointer to a class book , both statements (a) and (b) are alid the syntax
for using pointes with members is as given below

1) ptr --> name 2) ptr --> author 3) ptr --> pages

by executing thses three statements , the starting address of each member can be estimated.

Pointer to Object

Similar to variable , objects also have on address . A pointe can point to a specified object

Write a program to delcare an object and pointer and class

#include<iostream.h>

#include<conio.h>

class man

public:
char name[20];

int age;

};

void main()

clrscr();

man m={"krish",34};

man *ptr;

ptr=&m;

cout<<"\n name:"<<m.name;

cout<<"\n age is:"<<m.age;

cout<<"\n name is:"<<ptr->name;

cout<<"\n age is:"<<ptr->age;

getch();

You might also like