Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-27-2004, 06:18 PM   #1 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 209
falsepride is on a distinguished road
return types

okay im starting to learn c++. and its a strict language so everything needs to be defined as what type of value you it is right? like if i want to make a function to return a number id have to make it,
Code:
int myfunction() {
}
Is that right? If so what other types are there? im guessing var for variable, and viod returns nothing.
falsepride is offline   Reply With Quote
Old 12-27-2004, 09:31 PM   #2 (permalink)
Kernel_Killer
Regular Contributor
 
Kernel_Killer's Avatar
 
Join Date: Feb 2003
Location: indisclosed
Posts: 210
Kernel_Killer is on a distinguished road
int, char, double, float, void, etc, etc.

Don't forget your prototype at the beginning of the code as well.
__________________
Network Synapse
Screaming Electron
Kernel_Killer is offline   Reply With Quote
Old 12-27-2004, 09:35 PM   #3 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 209
falsepride is on a distinguished road
okay well i was kinda hoping for a full list. or a long list and what type of data it means.
falsepride is offline   Reply With Quote
Old 12-28-2004, 07:59 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Buy or download a book. It's hard learning from a forum only. Start with int, double, char, std::string. Forget the rest. I have a reason for this. But that comes much later.
__________________
Valmont is offline   Reply With Quote
Old 12-28-2004, 08:35 AM   #5 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 209
falsepride is on a distinguished road
what do the above mean though?
int - interger?
double - scientific notatoin?
char - character?
std::string - no idea
the :: is a operator for scope revolution, right? but what does that mean?
falsepride is offline   Reply With Quote
Old 12-28-2004, 08:45 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
Link

:: is the same as -> in php (classes)

you should really get a book at least for reference. web languages might let you slip by, but there is a lot more to c++.
__________________
Mike
sde is offline   Reply With Quote
Old 12-28-2004, 02:47 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally Posted by sde
:: is the same as -> in php (classes)
Most likely not. :: is a scope resulution operator.
:: isn't chosen because of it looks by the way. Those who studied quantor mathematics should recognize this operator.
Anyway, it is the scope resolution operator, and we're going to have some fun with it. First the basics. Observe the code below and draw your own conclusions:
Code:
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

//(auto generated) Optional function: depends on IDE. Remove if not needed.
void wait_for_enter();

//On-topic code below!

string whoAmI = "I am a global: usually a prime example of bad coding.";

namespace sde
{
  string whoAmI = "I am sde: the big boss!";
}

namespace Val
{
  string whoAmI = "I am Val: a slave of sde :)";
}

namespace falsepride
{
  string whoAmI = "I am falsepride: I'm looking at mumbo jumbo...";
}

int main(int argc, char *argv[])
{
  cout<< whoAmI <<endl; //the global
  cout<< sde::whoAmI <<endl; //sde
  cout<< Val::whoAmI <<endl; //Val
  cout<< falsepride::whoAmI <<endl; //falsepride
  
  cout<<endl;

  wait_for_enter();
  return 0;
}

//---------------------auto generated------------------------------

void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
Basically, multiple coders can work on the same code, without clashing with eachothers variables and all. Just define your stuff in your very own namespace.

And now we're going to find out whyusing namespace std; is actually bad.
Try to compile the code as it is below:
Code:
#include <ios>
#include <string>

using namespace std;

string dec;

int main()
{
  dec = "december";

  return 0;
}
Aha! This doesn't compile.
"dec" is already defined in the std namespace. Houston, we have a problem. Do we want the coder to use a different variable name for the month december then? Naah. We gotta solve this:
Code:
#include <ios>
#include <string>

using std::string;

string dec;

int main()
{
  dec = "december";

  return 0;
}
If you want to use the "dec" indentifier as well, then use it like this: std::dec
Now you're using the <ios> dec as well.
However, one shouldn't redefine typically standard keywords like I do below:
bad code below
Code:
#include <ios>
#include <string>
#include <iostream>

using std::string;
using std::endl;

string dec;
//Bad bad bad boy.
int cout;

int main()
{
  dec = "december";
  cout = 1;
  
  //Argh...
  std::cout<< cout <<endl;

  return 0;
}
This should be enough brain teasing to get you started. Good luck!
__________________
Valmont is offline   Reply With Quote
Old 12-28-2004, 06:14 PM   #8 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 209
falsepride is on a distinguished road
okay the first example i get completely, when inside of a name space they are all local variables, and you access the local variables by doing namespace_name::variable_name. but what does
Code:
using std::cin;
using std::cout;
using std::endl;
using std::string;
do? i have ideas, but id rather be sure, then get into bad thinking
falsepride is offline   Reply With Quote
Old 12-28-2004, 07:46 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
So we want to prevent the usage of using namespace std; right?
The way to code is then:

std::cout<<"hi"<<std::endl;
std::string theString("I am a string");
std::cout<<theString<<std::endl;

But there are so many keywords and reserved items that typing std:: becomes tedious. Instead, use one time code like this:

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

And now we can use cout, endl and string without typing the scope resultion operator all the time in front of them.

Here is a sample.
Code:
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

namespace falsepride
{
   int theInt(5);
   string theString("Hello");
}

//From now on, we will use falsepride's variables!
using falsepride::theInt;
using falsepride::theString;

int main()
{
  cout<<theInt<<endl;
  cout<<theString<<endl;
  
  return 0;
}
There is a small problem now. Now the rest of your collegues aren't allowed to define their stuff with the same names that you used.
The neat way to do is this (and I will add my own little project in my own namespace):
Code:
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

//Each programmer his own workspace!

namespace falsepride
{
   int theInt(5);
   string theString("Hello");
}

namespace valmont
{
   int theInt(12);
   string theString("SPQR");
}



int main()
{
  //Use falspride's definitions.
  cout<<falsepride::theInt<<endl;
  cout<<falsepride::theString<<endl;
  
  //Use valmont's definitions.
  cout<<valmont::theInt<<endl;
  cout<<valmont::theString<<endl;
  
  return 0;
}
A last example. We both are going to define a simple output function. But there will also be a global version. Just read the comments in the code for details.
Code:
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

//The global version. This one is slightly safer and certainly
//faster for LOTS of shifting with HUGE amounts of text.
void print_the_stuff(const string& stuff)
{
  cout<<stuff<<endl;
}

//falsepride chooses the unsafe yet modern way.
namespace falsepride
{
   void print_the_stuff(string stuff)
   {
     cout<<stuff<<endl;
   }
}

//Val does it the unsafe and old fashioned way.
namespace valmont
{
   void print_the_stuff(char* stuff)
   {
     cout<<stuff<<endl;
   }
}

int main()
{
  //Use falspride's version.
  falsepride::print_the_stuff("This is from falsepride.");
  
  //Use valmont's version.
  valmont::print_the_stuff("This is from Valmont.");
  
  //Use global version.
  print_the_stuff("Global version here!");
  
  return 0;
}
Play with it. For more questions, just ask.
__________________
Valmont is offline   Reply With Quote
Old 12-28-2004, 08:16 PM   #10 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 209
falsepride is on a distinguished road
so std is a namespace defined in iostream.h header file?
falsepride is offline   Reply With Quote
Old 12-28-2004, 09:05 PM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
ah nice work val .. i always thought of :: as accessing a member of a class, .. but i guess it's a lot more than that.
__________________
Mike
sde is offline   Reply With Quote
Old 12-28-2004, 10:16 PM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Oh you *must* use the scope resolution operator to access members of a class. But not just ordinary members. STATIC members only.

But for normal accessing, we use also -> and . (dot).

Let's find out about statics now:

Rules:
- A decleration of static members in a class means NOT that it is defined.
It is only there for us humans to see that it is a static member. This is called "logical presence" instead of "physical presence".
- You must define the static member OUTSIDE the class, on a global level.
Usually, immediatly after the definition of the class is the place to do so.
- A static member WILL exist BEFORE any object (instance of a class) exists.
- If statics are not initialized, the standard dictates that the static will automatically initialized with the value "0". For std::string this means an empty string.
- All instances of a class will share the same static member.

Observe how I use the :: operator to define the static just outside the class.
A demonstration follows below:
Code:
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

class ClassWithAStatic
{
public:
  static string theString;
  inline string show_static()
  {
    return theString;
  }
};

//Must declare out of class!
string ClassWithAStatic::theString("I Live before all.");

int main()
{
  ClassWithAStatic a, b;
  cout<<a.show_static()<<endl;
  cout<<b.show_static()<<endl;
  
  //Lets redefine the static member. Notice that all objects adapt it's value.
  ClassWithAStatic::theString = "I am reborn for all.";
  cout<<a.show_static()<<endl;
  cout<<b.show_static()<<endl;
  
  return 0;
}
However, I didn't want to mention this for falsepride. This should come a week later if he studies seriously.
__________________
Valmont is offline   Reply With Quote
Old 12-28-2004, 10:24 PM   #13 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally Posted by falsepride
so std is a namespace defined in iostream.h header file?
Very brave. I've asked you to draw your own conclusions and you did. That I appreciate!

The answer:
No. The otherway around:
iostream is defined within the namespace std;


namespace std
{
TONS OF SYSTEM STUFF IN HERE LIKE iostream, cmath, ios, fstream ETCETERA
}

But obviously this is devided systematically over a number of files. We call them header files.
That will come later.
A "namespace" is nothing else but a fancy name for grouping things.
This code is valid too, using {} to group things:
Code:
{ //Grouping starts here.
   {
      cout<<"hi"<<endl;
   }
}//Grouping ends here.
Useless it seems, but it is valid.
This is by the way a trick to avoid certain bugs with certain compilers. But don't you worry about that. Just demonstrating.
__________________
Valmont is offline   Reply With Quote
Old 12-28-2004, 10:31 PM   #14 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
And since I am toying with code, why not toying a bit more huh? But this time only for veterans and geeks. The rest of you better don't watch .

Question: is this valid code?
Code:
#include <iostream>

using std::cout;
using std::endl;

int main()
{
   cout<<"Hi all."<<endl;
}
I've warned you. Don't look if you can't bear it .
I am outta here. You guys have fun with this.
__________________
Valmont is offline   Reply With Quote
Old 12-29-2004, 06:03 PM   #15 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 209
falsepride is on a distinguished road
does std stand for anything? maybe standard? i duno(knowing full names sometimes makes it easy for me to remember and to make references in my brain to understand what it does). and if you need to include iostream.h to use cout, im guessing that std is a name space contain your functions and what not, but only certian functions are in it depending on what header files youve included? There are hundreds upon hundreds of header files that came with borland, how do i know functions are in each?

And as for your code, its valid right?
falsepride is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Return of the Variant (Good or Bad?) RobertStout Lounge 3 10-26-2004 12:41 PM
OpenGL.dll Mr.Anderson Platform/API C++ 3 08-13-2004 10:07 AM
MySQL Return closest matches? Admin PHP 4 07-22-2004 03:45 PM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM


All times are GMT -8. The time now is 01:52 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting