C++ How to access class members

We can access the class members with the help of the objects using ‘.’ dot. We can assign the values to these instance variables through the calling methods.

e.g:      student  obj = new  student();

obj.add(4,5);

In first line we have created an object then in next line we are calling the add method through the object and sending the values for instance variable.

We can assign the values to the instance variable directly with the help of objects.

Syntax:            object-name.variable-name;

e.g:      obj.a = 5;

 

Program:

/*PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS PALINDROME OR NOT*/

#include<conio.h>

#include<iostream.h>

class palindrome

{

int n,temp,rev,rem;

public:

void input();

void output();

};

void palindrome::input()

{

cout<<“Enter any number: “;

cin>>n;

}

void palindrome::output()

{

temp=n;

rev=0;

while(temp>0)

{

rem=temp%10;

temp=temp/10;

rev=rev*10+rem;

}

if(rev==n)

cout<<n<<” is palindrome”;

else

cout<<n<<” is not palindrome”;

}

void main()

{

palindrome p1;

clrscr();

p1.input();

p1.output();

getch();

}

Leave a Reply

Your email address will not be published. Required fields are marked *