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

Dani AI

Generated

Three quick, practical clarifications for ’s three viva points (class members, 2‑D arrays, and cin.get), with pointers to the useful notes already posted by and .

For class-member access: an object pointer identifies the whole object, not “just the first public member.” Access members using the language facilities (member access or the pointer‑to‑member feature) regardless of whether the object is automatic, static, or heap‑allocated — the only difference between those cases is lifetime and how the pointer was obtained. A safer, language‑level way to refer to a particular member is the pointer‑to‑member syntax; it avoids guessing offsets:

struct A { int a, b; };
A obj;
A* p = &obj;
int A::*pm = &A::b;
p->*pm = 42;   // set b through a pointer-to-member

Low‑level offset tricks (casting to char* and adding offsetof) exist but are fragile and only defined for standard‑layout types; prefer pointer‑to‑member or normal ->/. access in portable code.

For 2‑D arrays: the storage is contiguous, so two clear options exist — a row pointer or a flat pointer to the first element. The common bug in the posted loop was cumulatively advancing the row pointer; instead compute the row start each iteration:

int a[2][3];
int (*row)[3] = a;          // row[i][j] works
int *flat = &a[0][0];       // flat[i*3 + j] works
// set row i: use row[i][j] or flat + i*3 + j

As suggested, initializing values helps debugging.

About the cin.get() question: the extraction returns an integer‑type that can hold any character code or an end‑of‑file indicator (implementation‑defined negative value). Assigning it to an int and sending that int to cout prints the numeric code. To display the character itself, cast to char or use cout.put; to detect EOF compare the returned value with EOF or the stream’s eof/fail state. and were correct about seeing the numeric value — the extra detail is that cin.get() is intentionally typed to allow EOF detection.

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.