Showing posts with label References. Show all posts
Showing posts with label References. Show all posts

Wednesday, 20 April 2011

An Challenging Interview question with a difference

The following program is provided:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

//This class can be designed as you wish
class SR
{

public
:
SR(int i) {}
};


int
main()
{

int
sum = 0;
for
(int i = 1; i < 100; i++)
{

SR ii(i);
while
(i--)
sum += i;
}

cout<<"Expected value of sum = 161700" << endl;
cout<<"Returned value of sum = " << sum << endl;

return
0;
}


As already mentioned above, the expected output of 161700 is provided. The class 'SR' could be written as one wishes. How to do write the class 'SR' to get the desired output?
.
.
.
.
Give it a try before looking at the solution.
.
.
.
.
.
The modified program with the correct 'SR' class as follows:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

//This class can be designed as you wish
class SR
{

public
:
SR(int& i):ref(i)
{

var = i;
}
~
SR()
{

ref = var;
}

private
:
int
var;
int
&ref;
};


int
main()
{

int
sum = 0;
for
(int i = 1; i < 100; i++)
{

SR ii(i);
while
(i--)
sum += i;
}

cout<<"Expected value of sum = 161700" << endl;
cout<<"Returned value of sum = " << sum << endl;

return
0;
}



Source: Modified from here. Another similar question is available here.

Sunday, 26 July 2009

Initialising References in a class

Unassigned references cannot exist for a class. There is no C++ concept of an "unassigned reference" and the behavior of any program that has one is undefined. If a class contains a reference then it must be initialised through an initialiser list. You can't declare a reference variable in the class and then assign it later on. Non-reference variables and pointers can be left uninitialised but references must have a value upon entry. The only way to do this is through an initialiser.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Example showing initializing of refrences in a class
#include<iostream>

using namespace
std;

class
A
{

public
:
A(string &name);
void
print()
{

cout<<"a = "<<a<<" and s = "<<s.c_str()<<endl;
}

void
set_a(int x) {a = x;}
private
:
A(); //Shouldnt really be called because we need to have a reference
int a;
string &s;
};


A::A(string &name): s(name)
{

//s = name; - error. Should be in initialiser
a = 0;
}


int
main()
{

string z = "zahid";
A a(z);
a.print();
z = "christian";
a.print();
a.set_a(30);
a.print();
return
0;
}

The output is as follows:

Friday, 24 April 2009

Example showing Pass by Value and Pass by Reference

This is a very simple example of Pass by value and pass by reference. I have shown both the simple type and structures in the same example.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Example showing pass by value and pass by reference

#include<iostream>

using namespace
std;

//Pass by value
void func1(int num)
{

num++;
}


//Pass by reference
void func2(int &num)
{

num++;
}


typedef struct
x
{

int
a;
int
*b;
}
X ;

//Pass by value, pointer still incremented in b
void newFunc1(X xyz)
{

xyz.a++;
(*
xyz.b)++;
}


//Pass by reference, both variables incremented
void newFunc2(X &xyz)
{

xyz.a++;
(*
xyz.b)++;
}


int
main()
{

int
someNum = 3;
cout<<"\n1. someNum = "<<someNum<<endl;
func1(someNum);
cout<<"\n2. someNum = "<<someNum<<endl;
func2(someNum);
cout<<"\n3. someNum = "<<someNum<<endl;

cout<<"\n\nTrying this on structures"<<endl;
X someX;
someX.a = 13;
int
number = 50;
someX.b = &number;
cout<<"\n1. someX.a = "<<someX.a<<" someX.b = "<<*someX.b<<endl;
newFunc1(someX);
cout<<"\n2. someX.a = "<<someX.a<<" someX.b = "<<*someX.b<<endl;
newFunc2(someX);
cout<<"\n3. someX.a = "<<someX.a<<" someX.b = "<<*someX.b<<endl;
cout<<endl;
return
0;
}




The output is as follows: