The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-25-2007, 05:00 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon9 Question about using;

If i'm not wrong, using is just the same thing as import in Java right?
Is there a standard C++ library? I realized that there's also non-standard API version of Java that's offered by jEdit, does that mean the same thing when we say that there are many APIs in C++?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 07-25-2007 at 05:59 PM.
Reply With Quote
  #2 (permalink)  
Old 07-25-2007, 06:40 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
The using keyword in C++ is similar to the import keyword in Java. However, using is used in combination with header files in C++. In Java, there is a default class path where the compiler automatically detects the java and javax packages, which pretty much eliminates the need for header files.

They are similar in that Java has the import statement to allow you to use things like java.io.InputStreamReader without the java.io. prefix, and C++ uses the using keyword to let you use things like std::cout without the std:: prefix.
However, they are different in that C++ requires you to use #include <iostream> to even use std::cout while Java lets you use java.io.InputStreamReader whether you imported it or not.

To answer your question, yes they are basically the same. If you imported java.io.InputStreamReader in Java, you could do a similar thing by including iostream and then using std::cin in C++. If you imported java.io.*, it would be slightly comparable to using namespace std, though using namespace std gives you access to everything within the std namespace whereas importing java.io.* only gives you access to classes and interfaces that are direct children of the java.io package, just as you can't use java.io things as just InputStreamReader without the io. prefix when you import java.*

I would recommend looking up a Java-C++ compare/contrast document like this one on a search engine.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-25-2007)
  #3 (permalink)  
Old 07-25-2007, 08:13 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
I'm looking for the C++ standard APIs so what I know what to import if I wanted to use a certain feature such as Random number generator (in java we have to import java.util.Random)...???

However, I'm not sure whether VS2005 supports function as Eclipse for Java where it will help you choose and import a certain things based on the keywords that you declared. For example:

Code:
Random r = new Random();
In Eclipse, we can put our cursor to Random keyword that we typed, and press CTRL+SHIFT+M to import java.util.Random from JDK. So we don't have to remember thousands of classes name lol...

P.S I'm new at VS2005 XD

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-25-2007, 09:55 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
I'm not sure how it is done using .NET (VS2005 is .NET), but in normal C++, it can be done the following way:
Code:
// iostream - IO functions
// cstdlib - stdlib.h (C header); used for srand and rand functions
// ctime - time.h (C header); used for time function as argument to srand

#include <iostream>
#include <cstdlib>
#include <ctime>

//constants for the maximum/minimum values of the generated number
//  only change the MAX_VAL and MIN_VAL. everything else should be fine.
#define    MAX_VAL    6
#define MIN_VAL    1
#define    RANGE    (MAX_VAL - MIN_VAL + 1)

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

int main () {
    // seed the random number generator with current time as unsigned value
    srand((unsigned)time(0));

    //a simple bit of math for a random value between two integers
    // (you can find others if you want a better one
    //   as this is not exactly random enough sometimes)
    cout << ((rand() % RANGE) + MIN_VAL) << endl;

    return 0;
}

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-25-2007)
  #5 (permalink)  
Old 07-25-2007, 10:00 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
do you personally memorized most of the common libraries that can be included in C++ ???

I don't even know what do:

Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
contains? I want to see what are methods that I can use by including those libraries in my program... somehow...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 07-26-2007, 12:38 AM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Actually, the only ones in C++ that I know are the C++ names for the C header files and the iostream (cout/cin), string (string), fstream (files) and sstream (stringstreams) C++ headers.

There are two wonderful C++ references that I seem to use:
C++ Reference (scroll through that page or look on the left for a menu to take you to the appropriate section)
C/C++ Reference

I personally use the first a lot more because of the fact that things seem well-organized and not very difficult to read or navigate, probably due to the fact that the layout stays rather consistent. There are also comprehensive examples for nearly everything at the first one. However, the other one is nice because it is simple, yet effective. It just gets boring looking at such a plain site at times, which can have an effect on how much you want to code.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-26-2007)
  #7 (permalink)  
Old 07-26-2007, 06:38 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
(Based on my somewhat limited understanding of Java) Java uses heirarchies of packages to organise libraries of classes, which is a far cry from the header system that C++ inherited from 'C', although the namespace system provides some of the benefits of java's package system. With that in mind, it wouldn't make sense for the two keywords to be direct equivalents, Although that's probably the closest comparison you'll get.

the C++ 'using' keyword allows you to select as few or as many names from a class or namespace as you like, to be visible without needing the :: operator (the scope operator) when referring to the name.

Most of the time, the easiest thing to do when starting out learning C++ is to type one using directive at the top of every program
Code:
using namespace std;
in tiny test programs, name clashes are easily avoided, so you can get away with that. But in larger programs, you'll quickly discover that alot of common, useful names are already taken up by the standard library. Still, you can be a little lazy with some of the names
Code:
#include <iostream>
#include <string>
using std::cout;
using std::string;

int main()
{
    //Just suppose we come up with a very poor name for a string variable
    string cin = "Hello World";
    cout << cin;
}
Of course, noone in their right mind would go about naming a variable 'cin', because its just about one of the most common names used by the C++ standard library (Unless they wanted to totally throw anyone who was reading their code). But as you can see, the program compiles, because std::cin hasn't been brought into the global namespace. If the above program had contained using namespace std; there'd have been a compiler error.


Depending where you place the using keyword, you can localise its effect. for example, If you don't want to bring the std namespace anywhere into your program, except for main, you can do
Code:
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    string my_str = "Hello World";
    cout << my_str;
}
Which limits the effect of using namespace std; to the main() function.

Best practice though, once you get to that stage, is to avoid the temptations of laziness, and use fully-qualified names instead (That is, names where you start out with std:: )

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (07-26-2007)
  #8 (permalink)  
Old 07-26-2007, 07:19 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
do you personally memorized most of the common libraries that can be included in C++ ???

I don't even know what do:

Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
contains? I want to see what are methods that I can use by including those libraries in my program... somehow...
I think, if you exclude the 'C' libraries (those which begin with the letter 'c'), then the C++ standard library is fairly limited. (And thus, once you get to grips with it, easy to remember). The reason being that the library can be mostly divided up into 3 or 4 seperate 'parts'.

First of all, you have the 'stream' family, which is all about I/O and string manipulation

<istream> - generic input stream
<ostream> - generic output stream
<fstream> - file i/o
<sstream> - string manipulation
<iostream> - standard i/o (cin, cout, etc)


Then you have the various STL containers. These all store multiple items of data together. (Each of them does it slightly differently, but in each case, they're almost always better than 'C' style arrays)

These are unsorted STL containers -
<deque> - double-ended queue
<queue> - ordinary queue (First In/First Out)
<vector> - a one-dimensional matrix (a "C++ Array" )
<list> - a linked list.
<stack> - stack (Last In/First Out)

More STL containers, all based on a Binary Tree, thus are automatically sorted -
<set> - Holds sets of data. each item must be unique
<multiset> - As above, but allows duplicates
<map> - allows a dictionary of data to be created, sorted based on a 'key'
<multimap> - same as a map, but allows duplicate keys

I would also count this one as a special-case container, because it behaves similarly to STL containers, except that its contents are characters

<string> - Far, far better than horrible C-style char arrays;

One great thing about STL containers, is that once you understand how to use one container, you generally understand how to use them all. (Most often, you will probably ignore all the STL containers except for vector)


That's pretty much all you need.. there are a few other C++ libraries which don't fit comfortably under any general heading (Iterators, Algorithms, etc). Most of which are geared towards manipulating STL containers. You don't need to worry much about them yet.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 07-26-2007 at 07:23 AM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (07-26-2007)
  #9 (permalink)  
Old 07-26-2007, 01:44 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
There are two wonderful C++ references that I seem to use
Thanx a lot rpgfan3233, it's really helpful

Quote:
std::cin hasn't been brought into the global namespace
Is using then the same thing as when you declare an instance variable in Java? private String cin; for example...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-26-2007, 02:19 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
Thanx a lot rpgfan3233, it's really helpful



Is using then the same thing as when you declare an instance variable in Java? private String cin; for example...
No, because using in C++ is similar to import in Java. It gives you more convenient access to members of a package/namespace.

When you declare an instance variable like that, it would be comparable to declaring a member of a class, a struct or even a global variable, though the latter can be misused rather often, especially when moving from C to C++. The reason why I say it can be like a member of any of those is because an instance variable is inside a wrapper - the class. In C++, you can use global variables to affect your entire file. That file in C++ is like the class that you create in Java. It can also be a member of a class or a struct as well. After all, those two are wrappers themselves:
PHP Code:
class SomeClass {
public:
    
//constructor for the class
    
SomeClass () : _exampleVal(42) { } //initializes private member variable _exampleVal to 42

    //destructor for the class
    
~SomeClass () { _exampleVal 0; } //not really necessary, but it is nice to have 0s anyway

    //returns _exampleVal since you can't get its value directly
    
inline int getValue () { return _exampleVal; }

    
//sets _exampleVal since you can't set its value directly
    
inline void setValue (int value) { _exampleVal value; }
private:
    
int _exampleVal;
}; 
Note that in that case, the class keyword could probably have been replaced with the struct keyword. struct in C++ was carried over from C, and it is a sort of container for various things. In C++, the idea of public, private and protected members was introduced, so struct is similar to class.

Now you are probably wondering what the difference is between struct and class if they do basically the same thing. Firstly, within a struct all members are public by default whereas all members are private in a class. The above code example could have left out the private section entirely and moved the private member variable to a location inside the class before the public section. That is the only difference defined by the C++ standard, though your compiler may also distinguish them in some other way. However, many programmers have their own conventions to help them decide when to use which one. A discussion of when to use what can be found at the following Web page: Struct vs Class - C++

I personally agree with the post about using structs when they contain only variables and no functions and data hiding isn't as much of an issue. With things that require functions and C++ features such as inheritance, private/protected members, etc., however, classes are recommended.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-26-2007)
Reply


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

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



All times are GMT -7. The time now is 08:18 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50