Archived

This topic is now archived and is closed to further replies.

Drevay

So if classes are a valid data type then...?

Recommended Posts

Drevay    100
So I was reading my book and a peace of code popped up - I can''t find an example that explains it for the life of me so I''ve resorted to the forums. let''s say I have some psuedo code:
class foo {
  // variables
public:
  // functions
};

foo func(in i) // right, wtf is up with this?
{
  // code
} 
Now it seems that func() would be able to be used on the right side of an expression (say, the member variable a of class foo gives the value 10 [which a holds] to something within func via
foob.a = func(i); 
or something similar). But is that the only difference? Is there anything else special about all of this? Possibly if someone could explain how it all works, what it does to the function that it doesn''t do to a function declared within class foo of type int (aka, member function) or something like that. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
overflowed    128

class foo {
public:
int i;
};

foo negate(foo C) {
C.i *= -1;
return C;
}

foo A,B;

int main() {
A.i = 5; // A.i now equals 5

B.i = 3; // B.i now equals 3


B = negate(A); // B.i now equals -5



return 0;
}



My example, if that makes any sense to you.

Just remember that all classes do is organize the variables into a single package, to put it simply (ya, I can just wait for everyone telling me all the amazing things classes do). In the end, it''s still just data in memory. When you''re passing the class to a function, you''re actually copying that data to the function, the function does *whatever* with it, then makes a new copy and sends it back (unless you get into pointers, but we won''t deal with that right now).

Clear enough? Just say if it isn''t.

Share this post


Link to post
Share on other sites
Amnesty    174
yah you can return instances of classes, reference to classes, pointers to classes. Is there any concern here, or are you just shocked?

Anyway, its quite useful indeed.
It allows you to chain commands together for one thing

// i.e

cout << a << b << c << a << b << c;


Also, if you had
class foo;
foo func(int i);

this would be leagal
foo a (func(30));
calling the copy ctor with the returned foo

Share this post


Link to post
Share on other sites
sashang    122
I'm not sure what you're after - you're question was vague.

Assuming foob is of type foo and a is of type int then the following will not compile:


foob.a = func(i);


However this will compile:


foob.a = func(i).a;


This is a definition of a function 'func' that returns a variable of type foo. It's a non-member function (or free function as they are sometimes called) so it doesn't require and instantiated object to call it.


foo func(in i) // right, wtf is up with this?

{
// code

}


[edited by - sashang on January 16, 2004 2:04:43 AM]

Share this post


Link to post
Share on other sites
Drevay    100
Fine, here''s the original code:
#include <iostream>
#include <cstring>
using namespace std;

class sample {
char s[80];
public:
void show() { cout << s << endl; }
void set(char *str) { strcpy(s, str); }
};

// Return an object of type sample.

sample input()
{
char instr[80];
sample str;

cout << "Enter a string: ";
cin >> instr;

str.set(instr);

return str;
}

int main()
{
sample ob;

// assign returned object to ob

ob = input();
ob.show();

return 0;
}

Ok, so I understand how the program works. The thing I don''t understand is: why wasn''t a member function used - couldn''t it have done the exact same thing? I mean, sure it doesn''t have to be called by an object or anything, but can''t other member functions be used in the right side of an assignment statement or only just those special functions (wtf would I call those functions anyway!?)?

Ugh.


Share this post


Link to post
Share on other sites
Nervo    344
Sure, if you wanted to you could put the entire program in that one class and the only thing in main other than the return would be instantiating an object of that class. You could design a default constructor to call the input function as a part of the class and the code flow would be through the whole class. It''s up to YOU to decide how you want to design your class. There is no set rules here, its up to you. For good code reusability I would imagine you''d want to have your class as robust as possible but also as minimal as possible. You''d want your class to be powerful enough to adapt to various situations that you might want to use it in. By putting that function as a member function, you''d have to ask yourself if that member function would be crucial for your code reuse.

Just imagine you create your own vector or linked list class. Certainly you''d come across the time where you''d have a function you create that would make use of it and even having a vector return type. It''s no different, a class is a complex user defined data structure.

Share this post


Link to post
Share on other sites
Drevay    100
So basically:

Instead of it having a return type of int, void, double, float or otherwise it has a return type of foo. Thus it will be able to return all variables and whatnot within foo.

I noticed too that since it''s not a member function it needed an object delcared within itself to actuall call upon a member function or anything else within the class.

Right, so it just makes it have the class''s return type.

Hmm, that could be very useful in some situations...!

Ok, I''m happy.


Share this post


Link to post
Share on other sites
Nervo    344
quote:
Original post by Drevay
So basically:

Instead of it having a return type of int, void, double, float or otherwise it has a return type of foo. Thus it will be able to return all variables and whatnot within foo.

I noticed too that since it's not a member function it needed an object delcared within itself to actuall call upon a member function or anything else within the class.

Right, so it just makes it have the class's return type.

Hmm, that could be very useful in some situations...!

Ok, I'm happy.


There are a number of possibilities. Personally, I wouldn't have designed my code that way. Well, heck I wouldn't have designed a class for something trivial like this anyhow

EDIT: When you said that it needed to declare a local object, well it doesn't. Thats rather inefficient.
For instance:

sample& input(sample& ab)
{
char instr[80];
cout << "Enter a string: ";
cin >> instr;
ab.set(instr);
return ab;
}

int main()
{
sample ob; // assign returned object to ob

input(ob);
ob.show();
return 0;
}


I'm using references here and it makes the code more efficient.

[edited by - nervo on January 16, 2004 3:06:27 AM]

Share this post


Link to post
Share on other sites
Drevay    100
Heh, Nervo - I''m reading about returning objects in functions in my book - so that''s why the author returned an object in that way.




Share this post


Link to post
Share on other sites
Nervo    344
quote:
Original post by Drevay
Heh, Nervo - I'm reading about returning objects in functions in my book - so that's why the author returned an object in that way.







I was checking through all my posts from a few days ago and I guess I missed your response drevay. I just want to point out in that code something that you might/might not have noticed. When I returned sample as a reference that was fine, but the way the function was called it wasn't actually necessary to return a value:


void input(sample& ab)
{
char instr[80];
cout << "Enter a string: ";
cin >> instr;
ab.set(instr);
}
int main()
{
sample ob;
input(ob); //when the function returns this call is not going to assign to anything

ob.show();
return 0;
}


As the comment says, it is not strictly needed here and so I changed the return to void. However, it does not hurt anything to do so because you can do something like this:

if(input(ob) == something) { execute something; }

But thats your choice.


[edited by - nervo on January 20, 2004 7:41:49 AM]

Share this post


Link to post
Share on other sites