![]() |
|
|
|
| ||||||
|
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. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| 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. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-25-2007) | ||
| ||||
| 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(); P.S I'm new at VS2005 XD |
| |||
| 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. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-25-2007) | ||
| ||||
| 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> somehow... |
| |||
| 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. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-26-2007) | ||
| |||
| (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; 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;
} 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;
} 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:: ) |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (07-26-2007) | ||
| |||
| Quote:
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. Last edited by Bench : 07-26-2007 at 07:23 AM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (07-26-2007) | ||
| ||||
| Quote:
Quote:
![]() |
| |||
| Quote:
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: 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. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-26-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |