473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Coding Problem, Help Needed

Hello to All,

Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
{
A a = new A();
//Now i want to return ith variable of a in
//this function
}
}

class C
{
getvar()
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}
Now how do i implement GetVariable(int i) function?
The reason i have put class B in between is because
There are many classes like A and i don't want to change
them.

Any Suggestion...

Thanks In Advance...
Cheers...

Bye

Sep 19 '05 #1
6 1327
batista wrote:
Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
What's the return value type?
{
A a = new A();
//Now i want to return ith variable of a in
//this function\
There is no such concept as "ith variable" in C++. Furthermore,
since the variables have different types, you won't be able to
return them from the same function. Plain and simple.
}
}

class C
{
getvar()
Again, what return value type?
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}
Now how do i implement GetVariable(int i) function?
The reason i have put class B in between is because
There are many classes like A and i don't want to change
them.

Any Suggestion...


Do NOT do it. Post again describing what problem you're trying to solve
and maybe we can help you find a *proper* solution.

If you're trying to implement some kind of "Property" mechanism, look for
it on the web, there are many implementations already available, no sense
to reinvent the wheel, especially when your language skills are not that
advanced.

V
Sep 19 '05 #2

"batista" <sa*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello to All,

Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
{
A a = new A();
//Now i want to return ith variable of a in
//this function
}
}

class C
{
getvar()
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}
Now how do i implement GetVariable(int i) function?
The reason i have put class B in between is because
There are many classes like A and i don't want to change
them.

Any Suggestion...

Thanks In Advance...
Cheers...

Bye


I'm very sorry but your pseudo-code doesn't make very much sense. Could you
please elobarate with real code what you want to achieve? The problem with
your GetVariable(int i) function is that it would have to return different
data types and not only different member variables! However, you need to
specify the return type of a GetVariable function and there is no common
denominator for int/char/string.

Cheers
Chris
Sep 19 '05 #3
batista wrote:
Hello to All,

Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
{
A a = new A();
//Now i want to return ith variable of a in
//this function
}
}

class C
{
getvar()
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}


On top of what has already been mentioned, why do you need to
dynamically allocate "a" and "b"?

Sep 19 '05 #4
Mark P wrote:
On top of what has already been mentioned, why do you need to
dynamically allocate "a" and "b"?


OP is obviously trying to apply java-isms.
Sep 19 '05 #5

"batista" <sa*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| Hello to All,
|
| Let me explain my problem with a pseudo code:
|
| class A
| {
| int x;
| char y;
| string z;
| }

semicolons required!

|
| class B
| {
| GetVariable(int i)
| {
| A a = new A();
| //Now i want to return ith variable of a in
| //this function
| }
| }
|
| class C
| {
| getvar()
| {
| B b = new B();
| b.GetVariable(0) //it shud return x
| b.GetVariable(1) //it shud return y
| b.GetVariable(2) //it shud return z
| }
| }
|
|
| Now how do i implement GetVariable(int i) function?
| The reason i have put class B in between is because
| There are many classes like A and i don't want to change
| them.
|
Doesn't make sense. A's variables are all private in your pseudo code.
That can't be right. I'll assume a struct A.

One way is by deriving from A and overloading getVariable(...). Note:
overloading is not achieved by return type. Overloading involves
parameter types only. So references are used to modify the original
variables.

#include <iostream>
#include <string>

// guessing
struct A
{
int x;
char y;
std::string z;
A(int n, char c, std::string s) : x(n), y(c), z(s) { }
};

class B : public A
{
public:
B(int n, char c, std::string s) : A(n, c, s) { }
~B() { }
void getVariable(int& n) const { n = A::x; }
void getVariable(char& c) const { c = A::y; }
void getVariable(std::string& s) { s = A::z; }
};

int main()
{
B b(99, 'e', "a string");

int x;
b.getVariable(x);
char y;
b.getVariable(y);
std::string z;
b.getVariable(z);

std::cout << "int x = " << x << std::endl;
std::cout << "char y = " << y << std::endl;
std::cout << "int x = " << z << std::endl;

return 0;
}

/*

int x = 99
char y = e
int x = a string

*/

There are many other ways this can be achieved. Since the pseudocode
provided is not accurate, there is no point to continue.
Sep 19 '05 #6
Thanks For All the Relies,

Victor Bazarov, I don't know how u judged my lanugage skills
But, if u or anyone judged it because of that pseude code
then understand that I only want to ask about that
GetVariable() thing, not that wat shud be private/public or dynamically
allocation etc.

Peter Julian, don't get confused by public/private thing
I know that, Plus

Mark P, there is no reason of dynamically allocating a and b

I know all that

The only Question i want to know is that
is there anything in C++ like ith variable,

That i got the point...

So hope that clears a bit.

Anyway Thanks For the Replies Once Again...

Cheers...

Bye.

Sep 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: evanescent.lurker | last post by:
Hi there, I was wondering is there a point in optimization in code style... I have used to put parenthesis around some php keywords, but they are not needed. For example, I always use: echo(...
136
by: Merrill & Michele | last post by:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
4
by: Josh Golden | last post by:
i lead a small development team (based on some of my posts that might cause some people to choke themselves, but have no fear, i am NOT the lead developer, the people on my team are great - i'm...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
42
by: kiplring | last post by:
1. int intArr = new int; 2. int intArr = new int; rgnNumberArr.Initialize(); 3. int intArr = new int; for( int i=0; i<intArr .lenght; i++)
86
by: PTY | last post by:
Which is better? lst = while lst: lst.pop() OR while len(lst) 0:
3
by: bh | last post by:
If I want to loop through the values in a listbox, and get either all items in the box, or only selected items, based on a boolean variable passed into a subroutine, which method would be more...
19
by: davidsamith | last post by:
'Good code' is code that works, is bug free, and is readable and maintainable. Standards need to be followed for coding. Read more... http://brsx.co.uk/SWtesting/FAQs/FAQs012.asp
23
by: LayneMitch via WebmasterKB.com | last post by:
Hello. It's me again with another question that should generate a lot of responses. As mentioned before, I'm working on my first site (hopefully I'll be done soon and can move on to other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.