PDA

View Full Version : C++ FAQ's



Putnam
02-14-2010, 05:34 AM
note : I took some Q n A's directly from some site, I can't remember the name though.
=====


What is C++?

C++ is a programming language. It actually means "increased C", showing its nature as an evolution of the C language.
_____

Is it necessary to already know another programming language before learning C++?

Not necessarily. C++ is a simple and clear language in its expressions. It is true that code written with C++ may be seen by a stranger of programming a bit more confusing than some other languages due to the intensive use of special characters ({}[]*&!|...), but once you know the meaning of these characters it can be even more proper and clear than other languages that rely more on English words.
Also, the simplification of the input/output interface of C++ in comparison to C and the incorporation of the standard template library in the language, makes the communication and manipulation of data in a program written in C++ as simple as in other languages, without losing the power it offers.
_____

How can I learn C++?

There are many ways. Depending on the time you have and your preferences. Although there are many special classes you can take in college and other education facilities, if you are reading this you are probably teaching yourself. Using tutorials and how-tos (including this FAQ), you can achieve your goal of being able to read and write code in C++.
_____

What is OOP: Object-oriented programming?

OOP is a programming model that treats programming from a perspective where each component is considered an object, with its own properties and methods, replacing or complementing structured programming pattern, where the focus was on procedures and parameters.
Sauce (http://en.wikipedia.org/wiki/Object-oriented_programming)
_____

What is ANSI-C++?

ANSI-C++ is the name by which the ANSI/ISO standard for the C++ language is known. But before this standard was published, C++ was already widely used and therefore there is a lot of code out there written in pre-standard C++. Referring to ANSI-C++ explicitly differentiate it from pre-standard C++ code, which is incompatible in some ways.
_____

What is Visual C++? And what does "visual programming" mean?

Visual C++ is the name of a C++ compiler with an integrated environment from Microsoft. It includes special tools that simplify the development of large applications as well as specific libraries that improve productivity. The use of these tools is generally known as visual programming. Other manufacturers also develop these types of tools and libraries, like Borland C++, Visual Age, etc...
_____

Is C++ a practical language?
Yes.

C++ is a practical tool. It's not perfect, but it's useful.

In the world of industrial software, C++ is viewed as a solid, mainstream tool. It has widespread industry support which makes it "good" from an overall business perspective.
_____

Is C++ a perfect language?

Nope

C++ wasn't designed to demonstrate what a perfect language looks like. It was designed to be a practical tool for solving real world problems. It has a few bugs, but the only place where it's appropriate to keep fiddling with something until it's perfect is in an academic setting. This isn't C++'s goal.
_____

What's the big deal with OO?

Object-oriented techniques are the best way we know of to develop large, complex software applications and systems.

OO hype: the software industry is "failing" to meet demands for large, complex software systems. But this "failure" is actually due to our successes: our successes have propelled users to ask for more. Unfortunately we created a market hunger that the "structured" analysis, design and programming techniques couldn't satisfy. This required us to create a better paradigm.

C++ is an OO programming language. C++ can also be used as a traditional programming language (as "as a better C"). However if you use it "as a better C," don't expect to get the benefits of object-oriented programming.
_____

What is a class?

The fundamental building block of OO software.

A class defines a data type, much like a struct would be in C. In a computer science sense, a type consists of both a set of states and a set of operations which transition between those states. Thus int is a type because it has both a set of states and it has operations like i + j or i++, etc. In exactly the same way, a class provides a set of (usually public: ) operations, and a set of (usually non-public: ) data bits representing the abstract values that instances of the type can have.

You can imagine that int is a class that has member functions called operator++, etc. (int isn't really a class, but the basic analogy is this: a class is a type, much like int is a type.)

Note: a C programmer can think of a class as a C struct whose members default to private. But if that's all you think of a class, then you probably need to experience a personal paradigm shift.
Sauce (http://www.codersource.net/cpp_tutorial_class.html)
_____

What is an object?

A region of storage with associated semantics.

After the declaration int i; we say that "i is an object of type int." In OO/C++, "object" usually means "an instance of a class." Thus a class defines the behavior of possibly many objects (instances).
_____

When is an interface "good"?

When it provides a simplified view of a chunk of software, and it is expressed in the vocabulary of a user (where a "chunk" is normally a class or a tight group of classes, and a "user" is another developer rather than the ultimate customer).
_____

What is encapsulation?

Preventing unauthorized access to some piece of information or functionality.

The key money-saving insight is to separate the volatile part of some chunk of software from the stable part. Encapsulation puts a firewall around the chunk, which prevents other chunks from accessing the volatile parts; other chunks can only access the stable parts. This prevents the other chunks from breaking if (when!) the volatile parts are changed. In context of OO software, a "chunk" is normally a class or a tight group of classes.

The "volatile parts" are the implementation details. If the chunk is a single class, the volatile part is normally encapsulated using the private: and/or protected: keywords. If the chunk is a tight group of classes, encapsulation can be used to deny access to entire classes in that group. Inheritance can also be used as a form of encapsulation.

The "stable parts" are the interfaces. A good interface provides a simplified view in the vocabulary of a user, and is designed from the outside-in (here a "user" means another developer, not the end-user who buys the completed application). If the chunk is a single class, the interface is simply the class's public: member functions and friend functions. If the chunk is a tight group of classes, the interface can include several of the classes in the chunk.

Designing a clean interface and separating that interface from its implementation merely allows users to use the interface. But encapsulating (putting "in a capsule") the implementation forces users to use the interface.
Sauce (http://www.exforsys.com/tutorials/c-plus-plus/encapsulation--an-introduction.html)
_____

Is Encapsulation a Security device?

No. (lol)

Encapsulation != security.

Encapsulation prevents mistakes, not espionage.

What's the difference between the keywords struct and class?

The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults.

struct and class are otherwise functionally equivalent.

OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.
_____

What is a reference?

An alias (an alternate name) for an object.

References are frequently used for pass-by-reference:

void swap(int& i, int& j)
{
int tmp = i;
i = j;
j = tmp;
}

int main()
{
int x, y;
// ...
swap(x,y);
}

Here i and j are aliases for main's x and y respectively. In other words, i is x — not a pointer to x, nor a copy of x, but x itself. Anything you do to i gets done to x, and vice versa.

OK. That's how you should think of references as a programmer. Now, at the risk of confusing you by giving you a different perspective, here's how references are implemented. Underneath it all, a reference i to object x is typically the machine address of the object x. But when the programmer says i++, the compiler generates code that increments x. In particular, the address bits that the compiler uses to find x are not changed. A C programmer will think of this as if you used the C style pass-by-pointer, with the syntactic variant of (1) moving the & from the caller into the callee, and (2) eliminating the *s. In other words, a C programmer will think of i as a macro for (*p), where p is a pointer to x (e.g., the compiler automatically dereferences the underlying pointer; i++ is changed to (*p)++; i = 7 is automatically changed to *p = 7).

Important note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object.
Sauce (http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29)
_____

What happens if you return a reference?

The function call can appear on the left hand side of an assignment operator.

This ability may seem strange at first. For example, no one thinks the expression f() = 7 makes sense. Yet, if a is an object of class Array, most people think that a = 7 makes sense even though a is really just a function call in disguise (it calls Array::operator[](int), which is the subscript operator for class Array).

class Array {
public:
int size() const;
float& operator[] (int index);
// ...
};

int main()
{
Array a;
for (int i = 0; i < a.size(); ++i)
a[i] = 7; // This line invokes Array::operator[](int)
}

_____

When should I use references, and when should I use pointers?

Use references when you can, and pointers when you have to.

References are usually preferred over pointers whenever you don't need "resetting". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

The exception to the above is where a function's parameter or return value needs a "sentinel" reference. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references should always alias objects, not a deference NULL pointer).

Note: Old line C programmers sometimes don't like references since they provide reference semantics that isn't explicit in the caller's code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine.
_____

Is there any difference between List x; and List x();?
A big difference!

Suppose that List is the name of some class. Then function f() declares a local List object called x:

void f()
{
List x; // Local object named x (of class List)
// ...
}

But function g() declares a function called x() that returns a List:

void g()
{
List x(); // Function named x (that returns a List)
// ...
}

_____

Can I make a constructor call another constructor as a primitive?

No way.

Dragons be here: if you call another constructor, the compiler initializes a temporary local object; it does not initialize this object. You can combine both constructors by using a default parameter, or you can share their common code in a private init() member function.
_____

What is a destructors?

A destructor gives an object its last rites.

Destructors are used to release any resources allocated by the object. E.g., class Lock might lock a semaphore, and the destructor will release that semaphore. The most common example is when the constructor uses new, and the destructor uses delete.

Destructors are a "prepare to die" member function. They are often abbreviated "dtor".
Sauce (http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr380.htm)
_____

What's the order that local objects are destructed?

In reverse order of construction: First constructed, last destructed.

In the following example, b's destructor will be executed first, then a's destructor:

void userCode()
{
Nou a;
Nou b;
// ...
}

_____

What's the order that objects in an array are destructed?

In reverse order of construction: First constructed, last destructed.

In the following example, the order for destructors will be a[9], a[8], ..., a[1], a[0]:

void userCode()
{
Nou a[10];
// ...
}

_____

Can I overload the destructor for my class?

No.

You can have only one destructor for a class Nou. It's always called Nou::~Nou(). It never takes any parameters, and it never returns anything.

You can't pass parameters to the destructor anyway, since you never explicitly call a destructor (well, almost never).
_____

Should I explicitly call a destructor on a local variable?

No!

The destructor will get called again at the close } of the block in which the local was created. This is a guarantee of the language; it happens automatically; there's no way to stop it from happening. But you can get really bad results from calling a destructor on the same object a second time! Bang! You're dead!
_____

What is "self assignment"?

Self assignment is when someone assigns an object with itself. For example,

#include "Nou.hpp" // Declares class Nou

void userCode(nou& x)
{
x = x; // Self-assignment
}

Obviously no one ever explicitly does a self assignment like the above, but since more than one pointer or reference can point to the same object (aliasing), it is possible to have self assignment without knowning it:
[/highlight=c++]
#include "Nou.hpp" // Declares class Nou

void userCode(Nou& x, Nou& y)
{
x = y; // Could be self-assignment if &x == &y
}

int main()
{
nou z;
userCode(z, z);
}
[/highlight]
Sauce (http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html)
_____

What is Operator overloading?

It allows you to provide an interface to users of your class.

Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for function calls:

class Nou {
public:
// ...
};

#if 0

// Without operator overloading:
Nou add(Nou, Nou);
Nou mul(Nou, Nou);

Nou f(Nou a, Nou b, Nou c)
{
return add(add(mul(a,b), mul(b,c)), mul(c,a)); // Yuk...
}

#else

// With operator overloading:
Nou operator+ (Nou, Nou);
Nou operator* (Nou, Nou);

Nou f(Nou a, Nou b, Nou c)
{
return a*b + b*c + c*a;
}

#endif

____

What are the benefits of operator overloading?

By overloading standard operators on a class, you can exploit the intuition of the users of that class. This lets users program in the language of the problem domain rather than in the language of the machine.

The ultimate goal is to reduce both the learning curve and the defect rate.
_____

What is a friend?

Something to allow a class to grant access to another class or function.

Friends can be either functions or other classes. A class grants access privileges to its friends. Normally a developer has political and technical control over both the friend and member functions of a class (else you may need to get permission from the owner of the other pieces when you want to update your own class).
Sauce (http://www.codersource.net/cpp_tutorial_friend.html)
_____

Do friends violate encapsulation?

If they're used properly, they actually enhance encapsulation.

You often need to split a class in half when the two halves will have different numbers of instances or different lifetimes. In these cases, the two halves usually need direct access to each other (the two halves used to be in the same class, so you haven't increased the amount of code that needs direct access to a data structure; you've simply reshuffled the code into two classes instead of one). The safest way to implement this is to make the two halves friends of each other.

If you use friends like just described, you'll keep private things private. People who don't understand this often make naive efforts to avoid using friendship in situations like the above, and often they actually destroy encapsulation. They either use public data (grotesque!), or they make the data accessible between the halves via public get() and set() member functions. Having a public get() and set() member function for a private datum is OK only when the private datum "makes sense" from outside the class (from a user's perspective). In many cases, these get()/set() member functions are almost as bad as public data: they hide (only) the name of the private datum, but they don't hide the existence of the private datum.

Similarly, if you use friend functions as a syntactic variant of a class's public: access functions, they don't violate encapsulation any more than a member function violates encapsulation. In other words, a class's friends don't violate the encapsulation barrier: along with the class's member functions, they are the encapsulation barrier.
_____

What are some advantages/disadvantages of using friend functions?

They provide a degree of freedom in the interface design options.

Member functions and friend functions are equally privileged (100% vested). The major difference is that a friend function is called like f(x), while a member function is called like x.f(). Thus the ability to choose between member functions (x.f()) and friend functions (f(x)) allows a designer to select the syntax that is deemed most readable, which lowers maintenance costs.

The major disadvantage of friend functions is that they require an extra line of code when you want dynamic binding. To get the effect of a virtual friend, the friend function should call a hidden (usually protected:) virtual member function. This is called the Virtual Friend Function Idiom. For example:

class Base {
public:
friend void f(Base& b);
// ...
protected:
virtual void do_f();
// ...
};

inline void f(Base& b)
{
b.do_f();
}

class Derived : public Base {
public:
// ...
protected:
virtual void do_f(); // "Override" the behavior of f(Base& b)
// ...
};

void userCode(Base& b)
{
f(b);
}


The statement f(b) in userCode(Base&) will invoke b.do_f(), which is virtual. This means that Derived::do_f() will get control if b is actually a object of class Derived. Note that Derived overrides the behavior of the protected: virtual member function do_f(); it does not have its own variation of the friend function, f(Base&).
_____

Should my class declare a member function or a friend function?

Use a member when you can, and a friend when you have to.

Sometimes friends are syntactically better (e.g., in class Nou, friend functions allow the Nou parameter to be second, while members require it to be first). Another good use of friend functions are the binary infix arithmetic operators. E.g., aComplex + aComplex should be defined as a friend rather than a member if you want to allow aFloat + aComplex as well (member functions don't allow promotion of the left hand argument, since that would change the class of the object that is the recipient of the member function invocation).

In other cases, choose a member function over a friend function.
_____

Does delete p delete the pointer p, or the pointed-to-data *p?

The pointed-to-data.

The keyword should really be delete_the_thing_pointed_to_by. The same abuse of English occurs when freeing the memory pointed to by a pointer in C: free(p) really means free_the_stuff_pointed_to_by(p).
_____

Can I free() pointers allocated with new? Can I delete pointers allocated with malloc()?

No!

It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program. But it is illegal, immoral, and despicable to call free() with a pointer allocated via new, or to call delete on a pointer allocated via malloc().

Beware! I occasionally get e-mail from people telling me that it works OK for them on machine X and compiler Y. That does not make it right! Sometimes people say, "But I'm just working with an array of char." Nonetheless do not mix malloc() and delete on the same pointer, or new and free() on the same pointer! If you allocated via p = new char[n], you must use delete[] p; you must not use free(p). Or if you allocated via p = malloc(n), you must use free(p); you must not use delete[] p or delete p! Mixing these up could cause a catastrophic failure at runtime if the code was ported to a new machine, a new compiler, or even a new version of the same compiler.

You have been warned.
____

Can I use realloc() on pointers allocated via new?

No!

When realloc() has to copy the allocation, it uses a bitwise copy operation, which will tear many C++ objects to shreds. C++ objects should be allowed to copy themselves. They use their own copy constructor or assignment operator.

Besides all that, the heap that new uses may not be the same as the heap that malloc() and realloc() use!
_____

Do I need to check for NULL before delete p?

No!

The C++ language guarantees that delete p will do nothing if p is equal to NULL. Since you might get the test backwards, and since most testing methodologies force you to explicitly test every branch point, you should not put in the redundant if test.

Wrong:

if (p != NULL)
delete p;[/code]

Right:

delete p;

____

Is inheritance important to C++?

Yep.

Inheritance is what separates abstract data type programming from object-oriented programming.
Sauce (http://www.nextdawn.nl/cplusplus-tutorial-inheritance)
_____

When would I use inheritance?

As a specification device.

Human beings abstract things on two dimensions: part-of and kind-of. A Ford Taurus is-a-kind-of-a Car, and a Ford Taurus has-a Engine, Tires, etc. The part-of hierarchy has been a part of software since the ADT style became relevant; inheritance adds "the other" major dimension of decomposition.
_____

How do you express inheritance in C++?

By the : public syntax:

class Car : public Vehicle {
public:
// ...
};
[/highlight
_____

Why can't my derived class access private: things from my base class?

To protect you from future changes to the base class.

Derived classes do not get access to private members of a base class. This effectively "seals off" the derived class from any changes made to the private members of the base class.
____

What is a "virtual member function"?

From an OO perspective, it is the single most important feature of C++:

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

The derived class can either fully replace ("override") the base class member function, or the derived class can partially replace ("augment") the base class member function. The latter is accomplished by having the derived class member function call the base class member function, if desired.
Sauce (http://www.codersource.net/cpp_virtual_functions.html)
____

How can C++ achieve dynamic binding yet also static typing?

When you have a pointer to an object, the object may actually be of a class that is derived from the class of the pointer (e.g., a Vehicle* that is actually pointing to a Car object). Thus there are two types: the (static) type of the pointer (Vehicle, in this case), and the (dynamic) type of the pointed-to object (Car, in this case).

Static typing means that the legality of a member function invocation is checked at the earliest possible moment: by the compiler at compile time. The compiler uses the static type of the pointer to determine whether the member function invocation is legal. If the type of the pointer can handle the member function, certainly the pointed-to object can handle it as well. E.g., if Vehicle has a certain member function, certainly Car also has that member function since Car is a kind-of Vehicle.

Dynamic binding means that the address of the code in a member function invocation is determined at the last possible moment: based on the dynamic type of the object at run time. It is called "dynamic binding" because the binding to the code that actually gets called is accomplished dynamically (at run time). Dynamic binding is a result of virtual functions.
_____

What's the difference between how virtual and non-virtual member functions are called?

Non-virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object.

In contrast, virtual member functions are resolved dynamically (at run-time). That is, the member function is selected dynamically (at run-time) based on the type of the object, not the type of the pointer/reference to that object. This is called "dynamic binding." Most compilers use some variant of the following technique: if the object has one or more virtual functions, the compiler puts a hidden pointer in the object called a "virtual-pointer" or "v-pointer." This v-pointer points to a global table called the "virtual-table" or "v-table."

The compiler creates a v-table for each class that has at least one virtual function. For example, if class Circle has virtual functions for draw() and move() and resize(), there would be exactly one v-table associated with class Circle, even if there were a gazillion Circle objects, and the v-pointer of each of those Circle objects would point to the Circle v-table. The v-table itself has pointers to each of the virtual functions in the class. For example, the Circle v-table would have three pointers: a pointer to Circle::draw(), a pointer to Circle::move(), and a pointer to Circle::resize().

During a dispatch of a virtual function, the run-time system follows the object's v-pointer to the class's v-table, then follows the appropriate slot in the v-table to the method code.

The space-cost overhead of the above technique is nominal: an extra pointer per object (but only for objects that will need to do dynamic binding), plus an extra pointer per method (but only for virtual methods). The time-cost overhead is also fairly nominal: compared to a normal function call, a virtual function call requires two extra fetches (one to get the value of the v-pointer, a second to get the address of the method). None of this runtime activity happens with non-virtual functions, since the compiler resolves non-virtual functions exclusively at compile-time based on the type of the pointer.
_____

Should I learn C before I learn OO/C++?

Don't bother.

If your ultimate goal is to learn OO/C++ and you don't already know C, reading books or taking courses in C will not only waste your time, but it will teach you a bunch of things that you'll explicitly have to un-learn when you finally get back on track and learn OO/C++ (e.g., malloc(), printf(), unnecessary use of switch statements, error-code exception handling, unnecessary use of #define macros, etc.).

If you want to learn OO/C++, learn OO/C++. Taking time out to learn C will waste your time and confuse you.
_____

How/Where Do I Get a C/C++ Compiler?

In general, people on this forum generally use the following:
On Unix-like OS (Linux, FreeBSD, OpenBSD, NetBSD, Darwin etc.):
ubuntu : sudo apt-get install gcc
gcc

On Windows:
Bloodshed Dev C++ (which uses gcc as the backend)
Visual C++ - I recommend this.
Borland C++ and Borland C++ Builder
_____

How do I compile my program?

On unix-like systems:
gcc -o exename myfile.c
This will compile myfile.c to produce an executable file called exename. You can then run exename by typing ./exename

On Windows systems, different compilers have different keys to compile a program.
Bloodshed/Dev C++: Ctrl-F9 compiles, Ctrl-F10 executes.
Visual C++: F7 compiles, F5 executes.
Borland C++ Builder: Ctrl-F9 compiles, F9 executes.
Turbo-C: F9 compiles, Ctrl-F9 executes.
_____

Why does my scanf/fscanf/sscanf stop working?

Most C input is provided in a stream. That is, it is a series of characters made available one at a time. The scanf() function family are format-sensitive functions; they not only collect the characters for you, but attempt to convert them to a type (such as an integer) that you specify. They have great difficulty converting ZyGH4 to a meaningful number so they fail. The conversion attempt is governed by format specifiers that YOU provide. Since these may not match the input actually encountered, the family returns a value indicating the number of items successfully scanned AND assigned. If this value is zero, you have nothing. If this value is EOF, there was an end-of-file or other error. If you don't examine the return, how will you know? If an error occurs it will not be automatically cleared. Operations on the stream will continue to return an error until clearerr(), fseek(), fsetpos(), or rewind() is called. This means that a loop that is designed to pause for input will loop indefinitely.

The characters that f/s/scanf attempt to convert as one value are all the characters up to the first whitespace character (space, tab, newline) or up to the specified field width, or up to the first character that cannot be converted. (Note: The [ and c format directives are not whitespace delimited, but we won't consider them for the explanation here). If a character conflicts with the format specification, the function terminates and the character is left in the stream as if it had not been read. You probably will not expect it to be there to serve as input for your next call, so your input will not behave as you expect.

Example of proper usage:
[highlight=c++]
int status;
status = scanf ("%s%d\n", name, &number);

Check the value of 'status' after the scanf() call. If it is not what you expect (two, in this case), you didn't get all your fields. If it is EOF, your stream is broken and will remain so until you clear the error.
_____

Why does getline() not work correctly with Visual C++ (VC++)? Why do I have to type Enter twice for getline to process my input line?

If you're reading this, you've probably noticed that getline() is not functioning the way your book says it should -- you need to hit twice for the program to read your input. For example:

#include
#include

using namespace std;

int main()
{
string name;
cout<<"Enter a name:\n";
getline(cin, name);
cout<
int main(void)
{
char *buf1 = "42";
char buf2[] = "69.00";
int i;
double d;
long l;
i = atoi(buf1);
l = atol(buf1);
d = atof(buf2);
return 0;
}

_____

How do I convert an integer/float/double/long type variable to a char array?

Use sprintf(). Many books/websites tell you to use itoa(), itol(), itof() etc., but these functions are compiler specific and are therefore non-portable. sprintf() is definitely the way to go.

#include
int main(void)
{
int i = 42;
float f = 69.0;
double d = 105.24;
long l = 23;
char buf[50];

sprintf(buf, "%d", i);
sprintf(buf, "%f", f);
sprintf(buf, "%f", d);
sprintf(buf, "%ld", l);
sprintf(buf, "%d %f %ld", i, f, l);
return 0;
}

C++ users may also use the stringstream or ostringstream class (the deprecated form was strstream class)

#include
#include
using namespace std;

int main(void) {
stringstream ss;
int i = 42;
double d = 105.24;

ss << i << " " << d;

// Convert to string or char array
string s = ss.str();
char buf[50];
sprintf(buf, ss.str().c_str());
}

_____

How do I convert a hex/octal/any-other-base value to a number?

Use strtol() or sscanf().

#include
#include
int main(void)
{
long l; int i;
unsigned int ui;
char *hexstr = "12FC3";
char *octstr = "1245";
char *binarystr = "1101";

l = strtol(hexstr, NULL, 16);
l = strtol(octstr, NULL, 8);
l = strtol(binarystr, NULL, 2);
sscanf("12", "%d", &i);
sscanf("14", "%ld", &l);
sscanf(hexstr, "%x", &ui);
sscanf(hexstr, "%o", &ui);

return 0;
}

C++ users may want to use stringstream instead (strstream is deprecated).

#include
#include
using namespace std;

int main(void)
{
long l; int i;
char *hexstr = "12FC3";
char *octstr = "1245";
stringstream ss;

ss << hex << hexstr;
ss >> l;
ss.clear();
ss << oct << octstr;
ss >> i;

return 0;
}

_____

How do I convert an integer to hexadecimal (hex) /octal (oct)?

Use sprintf().

#include
int main(void)
{
int i = 42;
char buf[50];
sprintf(buf, "%x", i); /* convert to hex */
sprintf(buf, "%o", i); /* convert to octal */
return 0;
}

C++ users may want to use stringstream instead (strstream is deprecated).

#include
#include
using namespace std;

int main(void)
{
int i = 42;
char buf[50];
stringstream ss;
ss << hex << i;
ss >> buf;

ss.clear();
ss << oct << i;
ss >> buf;
return 0;
}

_____

What's the equivalent of perl/pascal/vb/php's chr() and ord() functions in C/C++?

There aren't any functions like this in C or C++ and they aren't needed anyway. You can get a char's ASCII value by simply assigning it to an integer variable.

char ch;
int i;

ch = 'A';
i = ch; /* Assigns the ASCII value of 'A' (i.e.) 65 to i */
printf("%d\n", i); /* Prints 65 */


=====

This is from my site,
RSTIPS.net (http://rstips.net/index.php)

Various Sources are mentioned above. Look for "Sauce"

Frement
02-14-2010, 09:43 AM
Nicely put together.

Zyt3x
02-14-2010, 09:46 AM
What is a friend?

Something to allow a class to grant access to another class or function.
Sounds like a nice friend :rolleyes:

Nice FAQ, putnam :)

void_hatred
02-14-2010, 10:08 AM
Nice detailed tutorial :)
If I remember correctly, C++ initially stood for, "C with Classes" but then was changed to "increased C"?

If that's true you may want to mention it somewhere, if not, then I apologize :)

void_hatred.

chitin
02-14-2010, 10:16 AM
nice info. add the sources/biblio at bottom and u got rep++ gj..

Frement
02-14-2010, 10:24 AM
Ohh and for all the code use [ CODE ] [/ CODE ] tags :)

Zyt3x
02-14-2010, 10:50 AM
#include <iostream>
using namespace std;
void main()
{
cout << "Hello World!" << endl;
}

:)

void_hatred
02-14-2010, 10:54 AM
When I was reading Accelerated C++, they actually advised not to use
using namespace std;

but rather only use what you actually use, e.g.


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


etc etc, this makes execution time quicker because when including all of iostream you include a lot of things you don't use.

just some pointless information :)

void_hatred

Zyt3x
02-14-2010, 11:01 AM
When I was reading Accelerated C++, they actually advised not to use
using namespace std;

but rather only use what you actually use, e.g.


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


etc etc, this makes execution time quicker because when including all of iostream you include a lot of things you don't use.

just some pointless information :)

void_hatredOh, thanks :D
I didn't know that was possible :redface:
Well, you learn something new everyday :P

Putnam
02-14-2010, 08:52 PM
Thanks, I'll format it now. I pretty much copy/pasta'd from my site.

x[Warrior]x3500
02-15-2010, 06:57 AM
When I was reading Accelerated C++, they actually advised not to use
using namespace std;

but rather only use what you actually use, e.g.


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


etc etc, this makes execution time quicker because when including all of iostream you include a lot of things you don't use.

just some pointless information :)

void_hatred

wow i never have heard of that. i might actually do that. oh and very nice FAQ. hopefully this will encourage more ppl to learn C++. :D