Well, today I had my C++ viva at my school..and I screwed it up!! The answers to this question which I was asked may be easy but I couldn't get it..Few questions of which I haven't got the answers yet--
1.

class A
{ public:
   int a;
int b;

};

In the class A there are two public members. If a pointer is created for the object of this class, it will point to first public member of this class. How would I be accessing the next member in the following cases--
1. if the object was static
2. dynamic object
3. normal object

2. How to access any element of two dimensional array using pointers .

3. I was also asked the output for this--

#include<iostream.h>
int main()
{
	int a;
	a=cin.get();
	cout<<a;
	return 0;
}

I tried this code but I am not getting the reason behind the output..

It would be great if you could answer these..
Thank you

Recommended Answers

All 6 Replies

I am not sure what a viva is but it sounds like a test or quiz or homework, so http://www.daniweb.com/techtalkforums/announcement8-2.html.
Maybe if you explain what you were thinking the answers are we will be more than glad to help you out.

Viva is kind of oral test we have over here in our school. These were the questions which I couldn't answer. I don't know about the first and the last question. But about arrays I do have some idea but I am not able to get the exact way. There is repetition of elements that I am getting..I know why the repetition is there but I'm not able to rectify it.
Here's rough code I tried--

#include<iostream.h>
int main()
{
	int a[2][3];
	int *ptr;
	ptr=*a;
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<3;j++)
		{
			cin>>a[i][j];
		}
	}
	for( i=0;i<2;i++)
	{
		ptr=ptr+i;
		for(int j=0;j<3;j++)
		{
			cout<<*(ptr+j)<<endl;
			
		}
	}
	return 0;
}

In the last question I am not able to understand the way the function is behaving.

make a minor change and it will be easier to use and see what is going on.(you don't have to type all those numbers every time you run the program.)

int main()
{
	int a[2][3] = {0};
	int *ptr = *a;
	int count = 1;
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<3;j++)
		{
			a[i][j] = count++;
		}
	}
	for( i=0;i<2;i++)
	{
		ptr=ptr+i;
		for(int j=0;j<3;j++)
		{
			cout<<*(ptr+j)<<endl;
			
		}
	}
	return 0;
}

since cin.get by default accepts character values therefore the output is the ASCII code of whatever u entered

#include<iostream.h>
int main()
{
    int a;
    a=cin.get();
    cout<<a;
    return 0;
}

in the code a is taking the AsCII value

Well, today I had my C++ viva at my school..and I screwed it up!! The answers to this question which I was asked may be easy but I couldn't get it..Few questions of which I haven't got the answers yet--
1.

class A
{ public:
   int a;
int b;

};

In the class A there are two public members. If a pointer is created for the object of this class, it will point to first public member of this class. How would I be accessing the next member in the following cases--
1. if the object was static
2. dynamic object
3. normal object

2. How to access any element of two dimensional array using pointers .

3. I was also asked the output for this--

#include<iostream.h>
int main()
{
	int a;
	a=cin.get();
	cout<<a;
	return 0;
}

I tried this code but I am not getting the reason behind the output..

It would be great if you could answer these..
Thank you

i think to access the next member of the class you just need to use the pointer object like a normal object

i.e.

ptr->b=3;

this should assign

or you can also use:

(*ptr).b=3;

(this is your case for accessing the second public member using the normal pointer to the object)

I hope it will work :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.