| Register | Members | Radio & TV | Games | Quran | Calendar | Search |
| Tutorials/Software Reviews & Tech Support Tutorials to some often repeated & Need help? Got Computer Issues? |
![]() |
| LinkBack | Thread Tools | Rate Thread | Display Modes |
| ||||
| Learn C++ (for Advance Users) What Is C++? C++ is a general-purpose, platform-neutral compiled programming language that supports various programming paradigms, including procedural programming, object-based programming, object-oriented programming, generic programming and functional programming. Unlike several other programming languages, C++ doesn’t impose a specific programming paradigm on its users. This liberality has two major advantages: it enables reuse of C code with minimal or no modifications at all, and it enables designers to choose the paradigm that suits their needs best. A Bit of History As opposed to most commercial programming languages that are designed from scratch, C++ was designed as an extension, or superset, of C. Bjarne Stroustrup started to design C++ in 1979 at Bell Laboratories. At that time, the language didn’t have a special name; it was called "C with classes". C with classes supported object-oriented programming facilities including classes, polymorphism, inheritance and operator overloading (these are discussed in the next sections). In 1983 (and according to other versions, in 1984) the name "C++" was proposed and accepted. The first commercial implementation of C++, cfront 1.0, was released in 1985. Cfront was a C++ to C translator. The intermediary C code would then be compiled to native machine code. The C++ Standardization Designing C++ as a superset of C offers several advantages: Reuse of C code in C++ applications Efficiency Platform neutrality Relatively quick migration from C to C++ Avoiding mistakes that occur with languages that are designed from scratch However, the dependency on C as the substratum of C++ also incurs certain complexities such as the lack of an automatic garbage collection, widespread use of pointers, unchecked array bounds, the lack of a native string type and cryptic declarator syntax. C++ is a vendor- and platform-neutral language. It’s defined and maintained by an ISO committee that consists of the delegates from national standardization bodies such as ANSI, BSI Group, DIN and many others. The first C++ international standard was ratified in 1998. It has since undergone two major revisions. The first revision is dubbed "C++03". C++03 includes new libraries for smart pointers, regular expressions, tuples and hashed containers, as well as some bug fixes. The second revision of the C++ standard is taking place in these very days. The intent is to finalize the new standard by the end of 2008, hence the name C++09 (or C++0x). The ISO C++ standard defines the core language, its standard libraries, and implementation requirements. Standard C++ is often used as common denominator among vendors and platforms. Typically, each vendor extends standard C++ by adding platform-specific extensions (e.g., compiler-specific keywords, code libraries and runtime support for dynamic linking, type-checking and proprietary frameworks). However, it’s possible to develop large-scale applications using pure standard C++ thereby ensuring portability and simplifying future maintenance.
__________________ ![]() |
| Sponsored Links |
| ||||
| Re: Learn C++ C++ as "Better C" In the late 1980s and the early 1990s, C++ was primarily touted as an object-oriented programming. Yet even if you use C++ as a procedural programming language, it offers considerable improvements compared to C: Improved memory management References Default parameter values Better type-checking Strongly typed pointers Function overloading Let’s look at some of these improvements more closely: Improved memory management. In C, you have to call library functions (malloc(), calloc() and free()) to allocate and release memory dynamically. In C++, you use keywords, not library functions, to manage memory. The C++ keyword new allocates and initializes an object dynamically. Similarly, the keyword delete destroys an object allocated using new. Array allocation and deallocation are performed by new[] and delete[]. User-defined types are first class citizens. C++ allows you to use a struct or a union’s name in declarations and definitions as you would use a built-in type: Quote:
Quote:
Quote:
Static Type Checking. All C dialects predating C99 (K&R C, C89, C94 etc.) allow programmers to call a function that hasn’t been declared. C++, however, doesn’t allow you to call a function before it has been declared. This restriction enables the C++ compiler to check the type and number of each argument. Without mandatory prototypes, not only would your code become brittle and bug-prone, it would be impossible to use reference arguments and default argument values. In C++, pointers are strongly-typed, which means that you can’t assign one pointer to another, unless they are pointing to objects of the same type, or one of the types is derived from the other. For example, assigning a pointer to string to a pointer to int is illegal because string and int aren’t related types. Function Overloading. In C, you can’t use the same name to declare two different functions. C++ allows you to do that, though. This is called function overloading. When two functions with the same name but different parameter lists are declared, the compiler checks the arguments passed to a function call to decide which overloaded version should be invoked: Quote:
__________________ ![]() |
| |||||
| Re: Learn C++ The Structure of a C++ Program Although many C++ implementations nowadays support dynamic linking, components (e.g., Microsoft’s ATL) or a virtual execution environment (Microsoft’s Managed C++) the C++ standard recognizes only one execution context called a program. A program consists of one or more source files that can be compiled separately. A source file together with all the headers and source files included via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion preprocessing directives, is called a translation unit. Each translation unit that is compiled successfully produces a binary object file. These object files are subsequently linked into a single executable program. A well-formed C++ program must contain the function main(). main() is traditionally regarded as the program’s entry point. Here is a minimal C++ program: Quote:
Quote:
Quote:
Quote:
Quote:
std::cout is the qualified name of the standard output stream object, cout. This object is automatically created whenever you #include <iostream>. The overloaded insertion operator << comes next. The insertion operator is a function in disguise (explained below). It takes an argument and passes that object on to cout. In this case, the argument is a literal string that we want to print onscreen. The second argument, std::endl, is a manipulator that appends the newline character after the string and forces buffer flushing. Again, a manipulator is a function in disguise. Finally, the return statement at the end of the program returns the program’s exit status to the hosting environment. A program is executed in the context of a process. Therefore, when the program terminates, the operating system collects the return code of the program. This way, other processes can examine the exit code of the terminated process. By convention, a return statement is placed at the end of a C++ program although you can safely omit it. If you omit the return statement, the program will implicitly return 0 to the hosting environment. As trivial as this program seems, it exposes some of the most important features of C++. First, you will immediately notice the use objects instead of functions for performing I/O operations. Second, the standard libraries of C++, including <iostream>, are declared in the namespace std (std stands for standard.) Thus, instead of declaring standard functions, classes, and objects globally, C++ declares these entities in a dedicated namespace, thereby reducing the chances of clashing with user code and third-party libraries.
__________________ ![]() |
| ||||
| Re: Learn C++ Overloading The operator << is particularly interesting. It behaves as a function that takes an argument, yet it looks like the built-in left-shift operator. In reality, it’s an overloaded operator. Operator overloading is another fundamental concept of C++. An overloaded operator is a function whose name is an operator. You can override almost all the built-in operators of C++ (+, *, >, ++ and so on) this way. Operator overloading may sound revolutionary and even dangerous at first because it seemingly allows programmers to override the meaning of built-in operators. What’s to stop programmers from defining operator + as the subtraction operator? In reality, C++ imposes certain restrictions on operator overloading. The combination of these restrictions and good object-oriented practices guarantee that this feature makes code uniform and intuitive. Consider the concept of a string. In C, assigning strings with the = operator is not allowed. The C++ std::string class overloads the = operator, allowing you to assign strings. For example: Quote:
Quote:
Overloading in C++ isn’t limited to operators, though. You can also overload functions by giving two or more functions the same name. The overloaded functions are distinguished by their parameter list. For example, a function that opens a file may have three different overloaded versions: Quote:
Quote:
As opposed to certain popular programming languages, a C++ source file is compiled into native machine code, not bytecodes or any other form of intermediary code that needs to be translated further at runtime. The main advantages of this compilation to native code policy are efficiency and runtime speed. The compiled code is optimized for the target hardware. In addition, the implementation doesn’t waste precious CPU cycles and memory on translating bytecodes into machine code at runtime. There is tradeoff though. The compiled binaries aren’t portable to other platforms; instead, you need to compile the source file for every target platform, using a different compiler.
__________________ ![]() |
| ||||
| Re: Learn C++ Support for Object-Oriented Programming C++ didn’t invent object-oriented programming. The first object-oriented programming language is Simula 67 which was created by Kristen Nygaard. However, C++ was the first widely used programming language that supported object-oriented programming. An object-oriented programming language typically supports these features: Abstract datatyping Information hiding Encapsulation Dynamic binding Polymorphism Inheritance
__________________ ![]() |
| ||||
| Re: Learn C++ Generic Programming C++ also supports generic programming. Generic programming enables programmers to write algorithms and classes that are type-independent. The programmer writes a single code mold called a template, from which the compiler synthesizes type-specific specializations, or instances. Here’s an example of a generic swap algorithm: Quote:
In addition to function templates, C++ supports class templates. A class template serves as a mold from which other classes are instantiated. Class templates are widely used in the Standard Library as generic containers: list, vector, stack, and so on. A rich set of generic algorithms that operate on generic containers is one of the main strengths of C++.
__________________ ![]() |
| ||||
| Re: Learn C++ Articles, Interviews, and Books "A Tour of C++" is a sample chapter from The C++ Programming Language, Special Edition by Bjarne Stroustrup. This article is an excellent introduction to C++, its standard library, and the various programming paradigms it supports. A History of C++: 1979- 1991 by Bjarne Stroustrup outlines the evolution of C++ during its first 12 years. The emphasis is on the ideas, constraints, and people that shaped the language, rather than the minutiae of language features. The evolution of C++ is traced from C with Classes to the early ANSI and ISO standards work and the explosion of use, interest, commercial activity, compilers, tools, environments, and libraries. My recent interview with Bjarne Stroustrup discusses Stroustrup’s forthcoming book Programming: Principles and Practice using C++ as well as the future of C++, Stroustrup’s Danish roots. Donald Knuth recently gave an interview that every professional programmer can’t afford to miss. In this interview, Knuth addresses the issue of Open Source code, multithreading and an urban legend about winning a programming contest with a single compilation. The C++ Programming Language, Special Edition by Bjarne Stroustrup, is still the best C++ book you can find today. Although it’s 8 years old and therefore doesn’t cover the two last revision rounds of standard C++ (C++03 and C++09), it still offers a detailed and complete account of C++98. C++ Standard Library Extensions, The: A Tutorial and Reference By Pete Becker is the best (and only) complete reference to TR1 extensions. The book covers shared_ptr, the regex library, tuples, hash containers, reference_wrapper, the array and function class templates, type traits and the numeric libraries of C++. Programming: Principles and Practice using C++ by Bjarne Stroustrup is targeted at beginners. Although this isn’t a book dedicated solely to C++, it uses C++ as the primary programming language. This book is particularly useful for novices who are learning C++ without any prior experience in programming.
__________________ ![]() |
| ||||
| Re: Learn C++ Special Member Functions: Constructors, Destructors, and the Assignment Operator Objects are the fundamental unit of abstraction in object-oriented programming. In a broad sense, an object is a region of memory storage. Class objects have properties that are determined when the object is created. Conceptually, every class object has four special member functions: default constructor, copy constructor, assignment operator, and destructor. If these members are not explicitly declared by the programmer, the implementation implicitly declares them. This section surveys the semantics of the special member functions and their roles in class design and implementation.
__________________ ![]() |
| |||||||||||||||||||||||||
| Re: Learn C++ Constructors A constructor initializes an object. A default constructor is one that can be invoked without any arguments. If there is no user-declared constructor for a class, and if the class doesn't contain const or reference data members, C++ implicitly declares a default constructor for it. Such an implicitly declared default constructor performs the initialization operations needed to create an object of this type. Note, however, that these operations don't involve initialization of user-declared data members. For example: Quote:
Other implementation-required operations that are performed by implicitly-defined constructors are the invocation of a base class constructor and the invocation of embedded object's constructor. However, C++ doesn't declare a constructor for a class if the programmer has defined one. For example: Quote:
Trivial Constructors As noted previously, compilers synthesize a default constructor for every class or struct, unless a constructor was already defined by the user. However, in certain conditions, such a synthesized constructor is redundant: Quote:
Constructors of Built-in Types You might be surprised to hear this: Built-in types such as char, int, and float also have constructors. You can initialize a variable by explicitly invoking its default constructor: Quote:
Quote:
Quote:
Quote:
Quote:
Constructors have many peculiar characteristics. They don't have a name (and therefore can't be called directly, as opposed to ordinary member functions); they don't have a return value; and their address cannot be taken. Non-default constructors are even more odd. A constructor that takes a single argument operates as an implicit conversion operator by default. Today, I will explore this phenomenon in further detail and explain how to use the explicit qualifier to restrain constructors' behavior. Constructor Categories In essence, constructors are what differentiates between a POD struct and a real object, as they automatically shape a raw chunk of memory into an object with a determinate state. A class may have several constructors (in this discussion, I'm referring to "plain constructors," not to copy constructors), each of which taking a different number of arguments. A constructor that may be called without any arguments is a default-constructor. A non-default constructor is one that takes one or more arguments. (The C++ literature provides no special name for this type of constructor, so I'm referring to it as a "non-default constructor.") Non-default constructors are further divided into two subcategories: those that take a single argument and thus operate as implicit conversion operators, and those that take multiple arguments. For example, Quote:
Quote:
Quote:
Quote:
Quote:
The problem here is that the implicit conversion (taking place behind the programmer's back) switches off the compiler's static type checking. Without this implicit conversion, the compiler would have complained about a type mismatch. C++ creators perceived this problem long ago. They decided to add a "patch" to the language in the form of the keyword explicit. Constructors declared explicit will refuse to perform such implicit conversions: Quote:
Quote:
However, this approach would have caused existing code to break. In some classes, for example std::complex and other mathematical classes, implicit conversions are rather useful. The C++ creators therefore decided to leave the original semantics constructors intact, while introducing a mechanism for disabling implicit conversions, when necessary. As a rule, every constructor that takes a single argument, including constructors that take multiple arguments with default values such as the following, should be explicit, unless you have a good reason to allow implicit conversions: Quote:
A constructor may include a member initialization (mem-initialization for short) list that initializes the object's data members. For example: Quote:
Initialization of const members. For example: Quote:
Quote:
Quote:
Quote:
Due to the performance difference between the two forms of initializing embedded objects, some programmers use mem-initialization exclusively. Note, however, that the order of the initialization list has to match the order of declarations within the class. This is because the compiler transforms the list so that it coincides with the order of the declaration of the class members, regardless of the order specified by the programmer. For example: Quote:
Quote:
Quote:
Copy Constructor A copy constructor initializes its object with another object. If there is no user-defined copy constructor for a class, C++ implicitly declares one. A copy constructor is said to be trivial if it's implicitly declared, if its class has no virtual member functions and no virtual base classes, and if its entire direct base classes and embedded objects have trivial copy constructors. The implicitly defined copy constructor performs a memberwise copy of its sub-objects, as in the following example: Quote:
Novices are sometimes encouraged to define the four special member functions for every class they write. As can be seen in the case of the Website class, not only is this unnecessary, but it's even undesirable under some conditions. The synthesized copy constructor (and the assignment operator, described in the next section) already "do the right thing." They automatically invoke the constructors of base and member sub-objects, they initialize the virtual pointer (if one exists), and they perform a bitwise copying of fundamental types. In many cases, this is exactly the programmer's intention anyway. Furthermore, the synthesized constructor and copy constructor enable the implementation to create code that's more efficient than user-written code, because it can apply optimizations that aren't always possible otherwise.
__________________ ![]() Last edited by Style Mantra; 25-10-2008 at 12:55 PM.. |
| |||||||
| Re: Learn C++ Constructors FAQ, Part I Question: What is a default constructor? Answer: Many programmers believe that a default constructor is a constructor that the compiler automatically generates when the user doesn’t define a constructor. This isn’t correct. A default constructor can be user-defined or compiler-generated; it’s a default constructor if and only if it can be invoked without any arguments. The following classes all have default constructors: Quote:
Question: When does the compiler implicitly declare a default constructor? Answer: The compiler will implicitly declare a default constructor for any class that doesn’t declare any constructor explicitly, provided that the class in question doesn’t contain any reference or const data members. Notice that if the user declares a copy constructor, that constructor will preclude the compiler’s implicit declaration of a default constructor. Consider: Quote:
Quote:
Quote:
Quote:
Answer: Here’s another common myth -- many believe that the implicitly-declared default constructor does secret initialization operations behind their back, or that it allocates memory for the object. This is sheer nonsense. The implicitly-declared constructor does absolutely nothing! Remember: declaring a constructor -- either explicitly or implicitly -- doesn’t necessarily mean that the constructor is also defined. The following examples demonstrate the difference between a constructor declaration and its definition: Quote:
Quote:
The second part of this series will also answer additional questions including: When does the compiler implicitly define a default constructor? Is it true that the implicitly-defined constructor allocate memory for its object? Does the implicitly-defined constructor initialize the object’s data members?
__________________ ![]() |
| ||||
| Re: Learn C++ Constructors FAQ, Part II The second installment lists the conditions under which an implicitly-declared constructor will also be implicitly-defined. It also explains why the compiler implicitly declares constructors even when they are not defined, seemingly for no purpose. Question: When Does the Compiler Implicitly-Define An Implicitly-Declared Constructor? Answer: An implicitly-declared default constructor is implicitly-defined if and only if the implementation needs that definition. The implementation needs an implicit definition in the following cases: Classes with virtual member functions. A class that has virtual member functions must have a non-trivial constructor. A non-trivial constructor is either a user-defined constructor or a constructor that is implicitly defined. The non-trivial constructor is responsible for internal initializations such as assigning the correct vptr address of each polymorphic object. Base and embedded subobjects. A class must have a non-trivial constructor if it has base class(es) with non-trivial constructor(s) or embedded objects with non-trivial constructor(s). The non-trivial constructor has to initialize the base class(es) and the embedded object(s) first. For example: Quote:
Quote:
Question: Why Does The Compiler Implicitly Declare Trivial Constructors If They Are Never Defined? Answer: Under certain conditions, the compiler implicitly-declares a default constructor but doesn’t define it. Such a constructor is said to be trivial. Many C++ programmers find this notion of trivial member functions rather confusing. Why on earth does the compiler bother to declare a member function implicitly when it knows that that function won’t be implicitly defined? Do implicit declarations of this kind serve any purpose? The first thing to bear in mind is that implicit declarations (and implicit definitions) are conceptual. That is, the compiler doesn’t go through your header files and inserts lines of code with constructor declarations to them. Rather, the compiler, the linker and the program behave as if the trivial member functions were declared. In practice, the compiler simply sets a few bits in a class’ type information record, indicating that the said type has a (trivial) default constructor declaration. So if you’re worried about excess verbiage of source files or the potential bloat of executable files, rest assured that nothing of this kind should happen. Now let’s get to the more important conundrum: why bother at all with these trivial declarations? After all, C doesn’t need this mechanism for its structs and unions. The truth is that implicit declarations have a contractual role. Each implicit declaration is like an article in a contract that states how a certain class may be used. When the compiler implicitly declares one of the special member functions, it grants certain authorizations to the user. By contrast, if the compiler doesn’t implicitly-declare a member function, the user is restricted with respect the class’s usage. Consider: Quote: |