Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 transferring to different functions
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu May 27, 2004 9:47 pm   Post subject: (No subject)

guruguru wrote:
Ok. I agree.

Quote:
In very few programming languages are classes actually "things" in and of themselves


What do you catagorize classes as in C++? A blueprint? It can also be seen as a thing that contains other things can it not be? Because theoretically, a blueprint by itself would do nothing- you have to activate one of its components. But a class can activate its components automatically at creation and destruction. So it's not quite a blueprint and more of a 'designer' that mainly lays out plans but can also implement.


Not really. In the end a class (in C++) is merely a blueprint. A class inside a class merely gets "name-mangled" into something else by the compiler. As for "members" of a class... well, they merely describe what an object of that class should have.

Trust me, this is coming from someone who's currently making the latest of several attempts to develop an assembler for a simple virtual machine that does several of these things. In the end, you have two choices:

  • A really complex computer that can understand ideas like classes as first class objects themselves. Or...
  • A really simple machine that expects the compiler to break sophisticated programming concepts down into simple instructions.


The latter is the route that C and C++ take, since they're compiling to native code for very simple machines. Languages with virtual machines (like the aforementioned Python, Ruby, etc.) can afford the former approach, since their machine exists only in software and isn't limited by the actual silicon.
Sponsor
Sponsor
Sponsor
sponsor
guruguru




PostPosted: Thu May 27, 2004 9:53 pm   Post subject: (No subject)

Very interesting... I'm not trying to argue with you btw :p. I'm curious, for what virtual machine are you developing an assembler for?
wtd




PostPosted: Thu May 27, 2004 10:01 pm   Post subject: (No subject)

My own "Flick" virtual machine.

The design has gone through various stages, depending on my mood at the time.

The latest incarnation featured two accumulator registers (one 64-bit int register and one 64-bit floating point register), a branch link register, a stack, and a flat memory space.

My next design will likely have a large number of general purpose registers.
signal_1001




PostPosted: Fri May 28, 2004 8:32 pm   Post subject: (No subject)

Well thank you for your arguments, unfortunately im still quite confused. I had heard about global variables but apparently we're not suppose to use them for one reason or another i'm not sure. how exactly do you pass variables through functions as parameters. That may be what i want. What i sort of need is for example to be able to get the name of the player during my puzzles() when they entered it during the main().
I really liked that code that was used.
Quote:
Code:
char ask_question(std::ostream& out = std::cout, std::istream& in = std::cin)
{
char option = '\0';
do
{
out << "Would you like to:" <<std::endl
<< "\ta)\tSpin the wheel? << std::endl
<< "\tb)\tBuy a vowel?" << std::endl
<< "\tc)\tSolve the puzze?" << std::endl;
in >> option;
option = toupper(option);
} while (option != 'A' && option != 'B' && option != 'C');
return option;
}

void do_action(char option_selected std::ostream& out = std::cout)
{
switch (option_selected)
{
case 'A':
spin_the_wheel();
break;
case 'B':
buy_a_vowel();
break;
case 'C':
solve_the_puzzle();
break;
default:
out << "Bad option!" << std::endl;
break;
}
}
it looks a lot more cleaned up than mine, unfortunatly im only using a simple application, and my teacher may have taught us more C than C++ because we use iostream.h, etc. and no std:: .... i'm not really sure though. anyways... basically now im asking how passing through parameters work. Rolling Eyes thanks
wtd




PostPosted: Fri May 28, 2004 10:49 pm   Post subject: (No subject)

You're using those things because your teacher is teaching you C++ with an old compiler (or with a recent compiler, but with an outdated mindset).

Arguments to routines (functions and procedures) are passed by value by default in C and C++. This means the only thing a routine gets is a copy of whatever variable was passed in. That function cannot then make any changes to the original. Think about going to a library's reference section and not being allowed to take a book from the library, but being allowed to photocopy it. You can't do anything to the original, but you can mark up and alter your copy in any way you'd like.

For a practical example, let's say you want to pass a player's name, and their score into a function which displays their name and score, and adds $100 to their score.

Well, let's start with just displaying these things.

code:
void display_info(std::string player_name, int player_score)
{
   std::cout << player_name << " has " << player_score << std::endl;
}


Ok, that's pretty straightforward.

But, if we now want to add some amount of money to the player_score we can't. Well, we can but since the value inside the function is merely a copy of what was passed in, the addition won't have any effect outside of the function.

In order to achieve this we'll make use of C++'s pass-by-reference syntax. Using this we give the function an actual reference to the variable we originally passed in.

code:
void display_info_and_add_to_player_score(std::string player_name, int& player_score)
{
   std::cout << player_name << " has " << player_score << std::endl;
   player_score += 100;
}


On terminology

Typically the "player_name" and "player_score" in the routine header are referred to as parameters. The actual variables supplied when the routine is used are known as arguments.

And now for something entirely different

So we've got player_name and player_score parameters for functions. These are obviously related variables, yet for each player in the game we're keeping track of them with separate variables that are easy to get mixed up. It'd be pretty easy to display Mike's name with Bob's score, for instance.

Fortunately C++ gives us a solution in the form of structs by which we can create data types that don't normally exist in the language. So, let's create a player data type that can store a player's name and score.

code:
struct player
{
   std::string name;
   int score;
};


Now, structs in C++ as well as being able to contain information, can also describe actions associated with that information. In fact, those actions are how we should interact with that information, rather than having direct access to it. So we make those internal variables private, rather than publicly accessible.

code:
struct player
{
   private:
      std::string name;
      int name
};


The first publicly available thing should be the constructor. This is a special routine (unlke other routines) that initializes the state of an object. It's always given the same name as the struct/class.

code:
struct player
{
   private:
      std::string name;
      int name
   public:
      player(std::string init_name) : name(init_name), score(0) { }
};


So now we can create a player and then do absolutely nothing with it. So, le's see, we wanted to be able to print the information out.

The proper way to do this in C++, as you've doubtless seen is using the << operator.

code:
std::cout << "Hello, world!" << std::endl;


But that only works on a few types. It won't work on our player data type. But, with operator overloading, we can make it work with player variables.

The basic syntax for overloading the << operator is:

code:
std::ostream& operator<<(std::ostream& out, const player& p)


Let's take this apart. std::ostream is the type of output streams like std::cout (or just "cout" if you're "using namespace std;"). The & indicates we're returning a reference to an output stream.

Similarly, we're passing in a reference to an output stream.

The other variable we'll be passing in is a constant reference to a player object. The const doesn't mean we can't pass in a normal player variable, but once we do, we won't be able to change that object. Since we don't want the simple act of printing out the object's state to change that state, this is a good thing.

Now this is all good, but this routine also needs access to the innards of the player object, which are normally inaccessible. But, if we declare the routine as a friend of the player object, we can get around this limitation.

code:
struct player
{
   private:
      std::string name;
      int name
   public:
      player(std::string init_name) : name(init_name), score(0) { }
      friend std:ostream& operator<<(std::ostream& out, const player& p)
      {
         out << p.name << " has " << p.score;
         return out;
      }
};


Now, all that's left to do is increment the player's score. Well, we can overload the += and -= operators to do this to.

code:
struct player
{
   private:
      std::string name;
      int name
   public:
      player(std::string init_name) : name(init_name), score(0) { }
      friend std:ostream& operator<<(std::ostream& out, const player& p)
      {
         out << p.name << " has " << p.score;
         return out;
      }
      void operator+=(int increment_by)
      {
         score += increment_by;
      }
      void operator-=(int decrement_by)
      {
         score -= decrement_by;
      }
};


Now, to deal with players...

code:
int main()
{
   player mike("Mike");

   std::cout << "Mike before winning $100:" << mike << std::endl;
   mike += 100;
   std::cout << "Mike after winning $100:" << mike << std::endl;
}
signal_1001




PostPosted: Sat May 29, 2004 12:26 pm   Post subject: (No subject)

Wow... that was a big help, sort of, i understood 95% of it. but now i feel like a bit of an idiot cuz since i havent been taught how to use std:: or what it really does or means, i've been trying to figure out how it all works through reading other peoples problems on this site and the tutorials and stuff. And it seems like my teacher is just out of date because we're using MVC++ 6.0 so why he didnt teach use using namespace std and all im not too sure. anyways thanks for the help though
guruguru




PostPosted: Sat May 29, 2004 12:30 pm   Post subject: (No subject)

Using the std namespace as in typing "using namespace std" isnt bad, it's just a different way of accessing std functions. I don't know the deatils (wtd can help you more) buy I believe using std:: before the function is more reliable and faster since you are not imporiting the whole namespace.
wtd




PostPosted: Sat May 29, 2004 2:29 pm   Post subject: (No subject)

The standard (std) namespace contains a lot of stuff, and bringing all of that into your program's namespace is generally a bad idea. If you do use:

code:
using namespace std;


Make sure it's only on a function by function basis, and that whenever you define class members, or function parameters, you use the "fully qualified" name (as in "std::ostream").
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 23 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: