Noutați

What Is The Inline Namespace Feature In Modern C++?

In modern C++, namespaces are a kind of library or framework. They are useful to hold the same name of classes, methods, and other entities in different namespaces. C++11 standard and other standards above, allow the inline keyword in a namespace definition and in this post, we explain how to use inline namespaces in modern C++. What is a namespace in modern C++? Namespaces in C++ allow users to group entities like classes, methods, variables and functions in a narrower namespace scope instead of using in the global scope. This prevents name conflicts between classes, methods, functions and variables in large projects. Namespaces organize the elements of developers into different logical scopes referred to by names. Thus, different users (or namespaces) may use same method names and variables in different running processes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19   #include namespace nsA { void myf() { std::cout

Read More

What Is An Extended Friend Declaration in Modern C++?

In modern C++ the standards have a wonderfully named extended friend declaration feature to address the class declarations which is a template parameter as a friend. This extended friend declaration is available with the C++ compiler that has C++11 and above standards. In this post, we explain a friend and extended friend declaration in modern C++. What is a friend declaration in modern C++? The friend declaration is used in a class body with the friend keyword and grants a function or another class access to private and protected members of the class where the friend declaration appears. C++11 extends the current language to support a wider range of friend declarations. Now, in modern C++has the extended friend declaration feature to address class declarations which is a template parameter as a friend. This extended friend declaration is available with a C++ compiler that has C++11 and above standards. Syntaxes for the friend declaration (since C++11):   friend typename_specifier ;   and   friend simple_type_specifier ;   Is there a simple example of a friend declaration in modern C++?? Here is a simple friend declaration C++ example.   class Ta;   class Tx { friend Ta; };   How do I make a function declaration with friend declaration in modern C++? In C++, we can declare functions with friend. Here is the syntax:   friend function_declaration   and we can declare the F() function with friend as shown below.   float F(float m) { return m*9.81; // F=m.g }   class Tx { friend float F(float m); };   How to create a function definition with friend declaration in modern C++? In C++, we can define functions with friend. Here is the syntax how we can do,   friend function_definition   and we can declare a reset() function with friend as below.   class Tx { int i; friend void reset( Tx& T) { T.i = 0; } };   What is an elaborated class specifier with friend declaration in modern C++? According to the C++ standard, an elaborated type specifier must be used in a friend declaration for a class. That means the class-key of the elaborated_ type_specifier is required. In C++, we can use elaborated class specifier with friend. Here is the syntax.   friend elaborated_class_specifier ;   How to use a template with friend declaration in modern C++? Here is an template example that uses friend.   template class Tw { friend Tw; };   How to use a namespace with friend declaration in a class in modern C++? Here is a friend example that is used in a class of a namespace.   class Ts;   namespace Ns { class Tx { friend Ts; }; }   Is there a full example of an extended friend declaration in modern C++? Here is a full example to extended friend declaration in modern C++. 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73   #include   class Ta;   class Tb { […]

Read More

What Is An Assignment Operator In C++?

One of the most commonly used features of C++ software, in common with many programming languages, are assignment operators. These take the form of copy assignment and move assignment operators. In C++, a copy assignment operator can be used with “operator=” to create a new object from an existing one. In this post, we explain assignment operators with copy assignment operator examples in C++. If you are looking for more information on assignment operators in C or C++, the following post discusses them in depth. What are classes and objects in C++? Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects, and they are user-defined data types that we can use in our program. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.   class myclass {           public:       std::string str; };   What is a copy assignment operator in C++? The Copy Assignment Operator, in a class is a non-template non-static member function that is declared with the “operator=“. When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the typical declaration of a copy assignment operator which is defaulted, Syntax (Since C++11).   class_name & class_name :: operator= ( const class_name& ) = default;   Here is an example in a class.   Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator   Is there a simple example of using the assignment operator in C++? The forced copy assignment operator is default in any class declarations. This means you don’t need to declare it as above. Let’s give examples without using it. Let’s give a simple C++ example of a copy assignment operator with default option. Here is a simple class:   class myclass {   public:   std::string str;   };   Because this is default in any class declaration, and it is automatically declared. This class is same as below.   class myclass {   public:   std::string str;     Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator };   And here is how you can use this “=” copy assignment operator with both class examples above.   Tmyclass o1, o2;   o2 = o1; // Copy Assingment Operator   Is there a full example of how to use the assignment operator in C++? Here is an example with a copy assignment operator in a class, 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   #include #include   class Tmyclass {   public:   std::string str;        // Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator };   int main() { Tmyclass o1, o2;   o1.str = “LearnCplusplus.org”;   o2 = o1; // Using Copy Assignment Operator   std::cout

Read More

What Are The Rules Of Zero, Three, Five, And Six In C++?

In C++, classes and structs are one of the most important parts of modern software development. In modern C++, there are some rules to support the principles of programming, in class definitions there are a few rules to be considered, these are the Rule of Zero, the Rule of Three, the Rule of Five, and the Rule of Six. In this post, we explain all of these rules with examples. C++ is an Object-Oriented Programming (OOP) language, and OOP is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methods, functions of objects). Most developers find that using OOP techniques helps them to map real-world behavior and bring an organizational structure to data. These attributes and methods are variables and functions that belong to the class – part of the class’s code and they are generally referred to as class members. First, let’s refresh our memory about the fact that Resource Acquisition Is Initialization (RAII) in OOP programming, and the Single Responsibility Principle and how that relates to the Rule of Zero in C++. What is resource acquisition in C++? The principle of Resource Acquisition Is Initialization (RAII) term used in several OOP programming languages, which relates to the ability to manage resources, such as memory, through the copy and move constructors, destruction, and assignment operators. RAII is about the declaration and use of destructors, copy-move operators, and memory management in these members and methods. These cause new rules in development. What is the single responsibility principle in C++? The Single Responsibility Principle (SRP) is a computer programming principle that states, “A module should be responsible to one, and only one, actor.” This principle exposes a rule for the classes in C++, called Rule of Zero. Now, let’s see what the Rule of Zero in C++ is. What is the rule of zero in C++? The Rule of Zero means that, if all members have default member functions, no further work is needed. This is the simplest and cleanest semantics of programming. The compiler provides default implementations for all of the default member functions if there are no special member functions that are user-defined. You should prefer the case where no special member functions need to be defined.  Here is more about Rule of Zero with C++ Examples, What is the rule of three in C++? The Rule of Three states that if you need to define a class that has any of the following special member functions a copy constructor, copy assignment operator, or destructor then usually you need to define all these three special member functions. So, these 3 special member functions below should be defined if you have at least one of them defined, Copy constructor Copy assignment operator Destructor Here is more about Rule of Three with C++ examples, What is the rule of five in C++? The Rule of Three is outdated after C++11. C++11 comes with two additional special members of move semantics: the move constructor and the move assignment operator. So, there is another rule, the Rule of Five. The Rule of Five states that if you need to define any of the five special members below, copy constructor, copy assignment operator, move constructor, move assignment operator, or a destructor then you probably need to define or delete (or at least consider) all five of them. Here is more about Rule of Five with C++ examples, Actually, this could be called “The Rule of Six“, because the […]

Read More

Learn How To Draw Charts With Simple TeeChart (TChart) Examples in C++

The RAD Studio, C++ Builder 11 and C++ Builder CE Community Editions have a lot of amazing visual and nonvisual components that you can use in your modern applications for Windows and mobile. One of these is the free chart component for the VCL and FMX frameworks called TeeChart (TChart). TChart comes with RAD Studio including RAD Studio 10.x,11.x and the CE versions. How To Install and Use TeeChart (TChart) in C++ Builder CE? When you install RAD Studio on the second page of the “platforms” choices are some optional modules and components. One of those is “TeeChart Standard”. This is the free version of TChart which you can use in your applications. If you want more detailed professional charts in your applications, there is also TeeChart Pro by Steema. The TeeChart Pro charting component library offers hundreds of Graph styles in 2D and 3D for data visualization, 56 mathematical, statistical, and financial functions for you to choose from together with an unlimited number of axes and 30 Palette components. Please visit their official Steema web page for more details. How To Develop Apps With TeeChart (TChart) in C++? If you installed TChart in C++ Builder, you can use this component in your VCL or FMX applications. To do this: Create a New C++ Builder VCL Windows Application in RAD Studio Go to Palette window, there is TChart component (under the TeeChart Lite category) to visualize many kind of chart graphics. Drag it on to form, or If you have a specific area like a panel, rectangle or a tab drag it on to this area. You can move, resize or align it by selecting Align->Client selection, you can also set its margins. C++ Builder 11 CE Form Design with TChart component Now you can modify its default settings and you can add your custom charts, Pie, Bars, y=f(x) series, etc. How To Draw y = f(x) Series With TeeChart (TChart) in C++? Double click to TChart to create your own Chart series. Press “Add…” Button in Editing Chart Window, this will bring you to the Series Gallery as shown below. Select Functions tab and select y=f(x) series, Press OK, and Close When you are in form designer mode, be sure that there is Series1 in the Structure panel as below, Now you have a y =f(x) graph, add a Button (TButton) Double Click to your button (i.e. Button1) When user clicks this button we can clear and add series in this button click like so: Series1->Clear(); Series1->Add(  0.0, 55.0, clTeeColor ); Series1->Add( 10.0, 72.0, clTeeColor ); Series1->Add( 30.0, 95.0, clTeeColor ); Series1->Add( 40.0, 123.0, clTeeColor ); How To Draw Bar Series With TeeChart (TChart) in C++? If you want you can add a new Bar Chart, Double click to TChart to create your own Chart series. Press “Add…” Button in Editing Chart Window, this will bring you Series Gallery as below, In Series tab, select Bar Series,(i.e. this will be named as Series2) Press OK, and Close Add a new Button (TButton), named as Button2 Double click to this button (i.e Button2) When user clicks this button we can clear and add bar series in this button click as below here Series2->Clear(); Series2->Add(  88.0, “Jan”, clTeeColor ); Series2->Add(  72.0, “Feb”, clTeeColor ); Series2->Add(  95.0, “Mar”, clTeeColor ); Series2->Add( 123.0, “Apr”, clTeeColor ); that’s […]

Read More

What Is The Long Long Int Type In C++?

In the early days of C++ there were few data types such as char, float, and int. Over time these types improved with new additions. Modern C++ is really amazing, it allows you to define your data type in accordance with the limits of your variable values. One of the largest integer types is the unsigned long long or unsigned long long int and in this post we explain how to use the long long int type. What are the fundamental variables In C++? A professional developer should always know the size of data types and their limits and which data types of a variable is needed in these limits, because the operating system allocates memory and decides what can be stored in the reserved memory. In addition to standard types in C++, there are fixed-width integers which are defined types in header  with a fixed number of bits. Fixed-width integers are called as intN_t or intX_t integers where N or X represents number of bits reserved for that type.  What is long long int type in C++? The long long integral type (or long long int) is an integer type for the larger numbers that doubles bytes of long type integer. The long long and long long int types are identical as same as long and long int types are identical. In modern C++, C++11 standard introduced long long integral type to be more compatible with C99 standards. Detailed information on this feature, can be seen in this proposal: long long type Proposal document. Adding long long was proposed previously by Roland Hartinger in June of 1995. At the time, long long had not been considered by the C committee, and the C++ committee was reluctant to add a fundamental type that was not also in C. Almost a decade later long long was part of C99, and many major C++ compilers start to support it. long long has been added to standards with C++11. If we consider that int variable has size of 4 bytes, long long int has size of 8 bytes. usigned long long int is same. Here are the most used integer types in C++, Type Typical Bit Width Typical Range int 4bytes -2147483648 to 2147483647 unsigned int 4bytes 0 to 4294967295 signed int 4bytes -2147483648 to 2147483647 short int 2bytes -32768 to 32767 unsigned short int 2bytes 0 to 65,535 signed short int 2bytes -32768 to 32767 long int 4bytes -2,147,483,648 to 2,147,483,647 signed long int 4bytes same as long int unsigned long int 4bytes 0 to 4,294,967,295 long long int 8bytes -(2^63) to (2^63)-1 unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615 Is there a simple example of how to use the long long int type in C++? Here is a simple example that shows how you can use int, long int long long int, unsigned long long int,   int i = 2147483647; long int l = 2147483647; long long int ll = 9223372036854775807; unsigned long long int ull= 18446744073709551615;   Is there an example of how to use long long int type in C++? Here is an example of how you can use long long, long long int and unsigned long long, unsigned long long int, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19   #include   int main() { int i = […]

Read More

What Is The Rule Of Six In Modern C++?

In C++, classes and structs are one of the most important parts of modern application development. In modern C++, there are some rules to support the principles of programming, one of which is the Rule of Six in C++ (also known as the Rule of Five, excluding the constructor). In this post, we explain the Rule of Six in C++ with examples. C++ is an Object-Oriented Programming (OOP) language, and OOP is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methods, functions of objects). Most developers find that using OOP techniques help them to map real-world behavior and bring an organizational structure to data. These attributes and methods are variables and functions that belong to the class – part of the class’s code and they are generally referred to as class members. First, let’s refresh our memory about the fact that Resource Acquisition Is Initialization (RAII) in OOP programming, and the Single Responsibility Principle and how that relates to the Rule of Zero in C++. What is resource acquisition in C++? The principle of Resource Acquisition Is Initialization (RAII) term used in several OOP programming languages, which relates to the ability to manage resources, such as memory, through the copy and move constructors, destruction, and assignment operators. RAII is about the declaration and use of destructors, copy-move operators, and memory management in these members and methods. These cause new rules in development. What is the single responsibility principle in C++? The Single Responsibility Principle (SRP) is a computer programming principle that states “A module should be responsible to one, and only one, actor.” This principle exposes a rule for the classes in C++, called Rule of Zero. Now, let’s see what the Rule of Zero in C++ is. What is the rule of zero in C++? The Rule of Zero means that, if all members have default member functions, no further work is needed. This is the simplest and cleanest semantics of programming. The compiler provides default implementations for all of the default member functions if there are no special member functions that are user-defined. You should prefer the case where no special member functions need to be defined.  Here is more about Rule of Zero with C++ Examples, What is the rule of three in C++? The Rule of Three states that if you need to define a class that has any of the following special member functions a copy constructor, copy assignment operator, or destructor then usually you need to define all these three special member functions. So, these 3 special member functions below should be defined if you have at least one of them defined, Copy constructor Copy assignment operator Destructor Here is more about Rule of Three with C++ examples, What is the rule of six in C++? The Rule of Three is outdated after C++11. C++11 comes with two additional special members of move semantics: the move constructor and the move assignment operator. So, there is another rule, the Rule of Six. The Rule of Six states that if you need to define any of the six special members below, constructor copy constructor, copy assignment operator, move constructor, move assignment operator, or a destructor then you probably need to define or delete (or at least consider) all six of them. This rule also known as “The Rule of Five“, because the default constructor is special, and, therefore, sometimes excluded. Note that, when you define […]

Read More

Everything You Need To Know About Move Constructors And Copy Assignment in Modern C++

Hello C++ Developers, today we have a great collection of C++ posts about Move Constructors and the Copy Assignment Operator. All of the C++ examples in these posts can be used with C++ Builder Enterprise, Architect, and Professional Editions, or the free version C++ Builder 11 CE Community Edition. These examples can be used in console applications, in VCL Windows applications, or in Multi-Device Firemonkey (FMX) applications on Windows and mobile. Here, our standard C++ examples, can also be used with Dev-C++, BCC C++ Compilers, and some other compilers such as the GCC compiler. If you just starting out on your C++ journey and want to jump to a modern IDE and C++ compiler, there is a free version of C++ Builder, C++ Builder 11 CE Community Edition released in April 2023. If you are a start-up developer, student, hobbyist, or just interested in learning to code then the C++ Builder Community Edition may well be just the thing for you. Table of Contents Where can I learn Modern C++ with a free C++ compiler? How to use a move constructor and copy assignment in modern C++ with C++ Builder CE? How to learn modern C++ for free using C++ Builder CE with examples? What is new in C++ Builder CE? What might be next for C++ Builder? Where can I learn Modern C++ with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or a professional C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use a move constructor and copy assignment in modern C++ with C++ Builder CE? In a modern C++, there are a number of features which help you learn, master, and remember the various features and functions of the C++ language. One such feature of modern C++ is the move constructor that allows you to move the resources from one object to another without copying them. In the first post, we explain what the move constructor is in Modern C++. https://learncplusplus.org/what-is-a-move-constructor-in-modern-c/ Using a good quality C++ compiler actively helps you write better code and prompts you with tips on how to use the various capabilities of the C++ standards. In the next post, we explain what a typical declaration of a move constructor is. https://learncplusplus.org/what-is-a-typical-declaration-of-a-move-constructor/ One of the move constructors is forcing a move constructor to be generated by the compiler, and in the next post, we explain the forced (default) move Constructor in Modern C++. https://learncplusplus.org/what-is-a-default-forced-move-constructor-in-modern-c/ Other feature of a C++ are assignment operators such as copy assignment and move assignment operators. In C++, a copy assignment operator is used with “operator=” to create a new object from an existing one. In the next post, we explain […]

Read More

What is The Move Assignment Operator In Modern C++?

The Move Assignment Operator is one of the great features of Object-Oriented Programming in professional development. It complements features like the copy assignment operator, copy constructor, move constructor, and destructor. Since the C++11 standards, the move assignment operator is declared by using “operator=” and it allows you to move one object to another object. In this post, we explain what a move assignment operator is along with some C++ examples. First, let’s remind ourselves what are the classes and objects in C++. What are classes and objects in C++? Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.   class Tx {           public:       std::string str; };   What is a move assignment operator in C++? The Move Assignment Operator is an assignment operator that is a non-template non-static member function which is declared with the “operator=“. When you create a class or a type that is move assignable (that you can move objects with the std::move), it must have a public move assignment operator. Here is a simple syntax for the typical declaration of a move assignment operator. Syntax (Since C++11),   class_name & class_name ::operator=(class_name &&)   Here is an example of a move assignment operator declaration in a class.   Tx& operator=(Tx&& other) {         return *this; }   This is how you can move one object to another one with move assignment operator.   Tx o1, o2;   o2 = std::move(o1);   When the move assignment operator is called, lvalue object type of an assignment expression is the same type or implicitly converted type of the rvalue object. Move assignment operator is similar to changing two pointers of data blocks in C language. These can be, pointers to data blocks (i.e bitmaps), pointers to any structs, pointers to dynamically-allocated objects, I/O streams, running threads, file descriptors, TCP sockets, etc. Is there a simple example of using the move assignment operator in C++? The move assignment operator is default in any class declarations. This means you don’t need to declare it as above, let’s give examples without using it. Let’s give a simple C++ example to move assignment operator with default option, here is a simple class.   class Tx {   public:     std::string str;   };   Because this is default in any class declaration, and it is automatically declared. This class is same as below.   class Tx {   public:     std::string str;       Tx& operator=(Tx&& other) = default; // Move Assignment Operator };   And here is how you can use move assignment operator with both class examples above.   Tx o1, o2;   o2 = std::move(o1);   Is there a full example of how to use the move assignment operator in C++? Here is an example with a move assignment operator in a class that moves one object to another. 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 […]

Read More

Learn More About Copy Assignment Operator In Modern C++

Hello C++ developers. This week, we continue our Copy Assignment Operator topics in C++. We have more posts in detail with examples. All of the C++ examples in these posts can be used with C++ Builder or the C++ Builder 11 CE Community Edition. These examples can also be used with Dev-C++, BCC C++ Compilers, and some other compilers such as the GCC compiler. If you just starting out on your C++ journey and want to jump to a modern IDE and C++ compiler, there is a free version of C++ Builder, C++ Builder 11 CE Community Edition released on April 2023. If you are a start-up developer, student, hobbyist, or just interested in learning to code then the C++ Builder Community Edition may well be just the thing for you. Table of Contents What is a copy assignment operator in modern C++? Learn about the copy assignment operator with modern C++ examples Where can I learn modern C++ with free C++ compilers? What is new in C++ Builder CE? What is a copy assignment operator in modern C++? Object-Oriented Programming (OOP) is a method of mapping real-world objects and data to computer functions and data structures. Classes and Objects are part of object-oriented methods and typically provide features such as properties and methods. One of the features of an OOP Editor is a copy assignment operator that is used with “operator=” to create a new object from an existing one.  The Copy Assignment Operator in a class is a non-template non-static member function that is declared with the “operator=“. This operator allows you to copy objects of classes, structs, and unions. When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. In the first post, we explain the use of a forced default copy assignment operator with C++ examples. https://learncplusplus.org/what-is-a-forced-default-copy-assignment-operator-in-modern-c/ The declaration and definition of a class are different terms in C++ language. In the next post, we explain what the declaration and definition are and what we mean by an implicitly declared copy assignment operator in C++ along with some examples. https://learncplusplus.org/what-is-implicitly-declared-copy-assignment-operator-in-c/ In C++, the Copy Assignment operator is the default in any class declaration, and it is automatically declared. This is also called the forced copy assignment operator which is default in any class declarations. This means, if you don’t want this default feature, you should delete it by using the delete option as given in the syntax above. In the next post, we explain what we mean by “avoiding implicit copy assignment operator”, and how can we use the delete option with copy assignment in C++ examples. https://learncplusplus.org/what-is-avoiding-implicit-copy-assignment-in-c/ Learn about the copy assignment operator with modern C++ examples LearnCPlusPlus.org has been producing full educational daily articles about C and modern C++ that can be used with C++ Builder, C++ Builder CE, Dev-C++, BCC Compiler and other compilers such as the GCC compiler. Here are our post picks for today. What Is A Forced (Default) Copy Assignment Operator In Modern C++ What is Implicitly-declared Copy Assignment Operator In C++? What is Avoiding Implicit Copy Assignment In C++? Remember that we had similar topics in some previous posts about Copy Constructors, Learn about Forced Copy Constructor (Default Copy Constructor) in C++ Learn Implicitly Declared Default Constructor in C++ Learn about Deleted Copy […]

Read More