Most viewed questions in C++

0 votes
0 answers

Static vs dynamic type checking in C++

I'm curious in the differences between static ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
309 views
0 votes
1 answer

What is a Class and Object in C++?

A Class is like a blueprint, an ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
309 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
308 views
0 votes
0 answers

What is the difference between a concrete class and an abstract class?

I'm learning C++, but I'm having trouble ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
306 views
0 votes
1 answer

C++ string input

To read from cin, use a std::string.  Then you don't have to guess how big your buffer should be. std::string input; cin >> input; cout << intput; If you ...READ MORE

Jul 11, 2022 in C++ by Damon
• 4,960 points
306 views
0 votes
0 answers

How to use the priority queue STL for objects?

class Person { public: int age; }; I'd want to put objects of the type Person in a priority queue. priority_queue< ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
304 views
0 votes
1 answer

The static keyword and its various uses in C++

Static variables exist during the "lifetime" of the translation unit in which they are declared, and: It cannot be accessible from any other translation unit if it is in a namespace scope (i.e. outside of functions and classes).  This is referred to as "internal linking" or "static storage lifetime."  (Except for constexpr, do not do this in headers; otherwise, you would wind up with a different variable in each translation unit, which is really confusing.) If it is a variable in a function, it, like any other local variable, cannot be accessed from outside the function.  (This is the mentioned local) Class members have no limited scope owing to static, but they may be referenced from both the class and an instance (like std::string::npos). locations as code: static std::string namespaceScope = "Hello"; void ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
299 views
0 votes
0 answers

memory allocation for objects

When we instantiate a variable in C++, ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
296 views
0 votes
1 answer

Examples of good gotos in C or C++

Here's one method I've heard of folks use. But I've never seen it in the wild.  And that only pertains to C since RAII allows you to accomplish this more idiomatically in C++. void foo() { if (!doA()) ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
296 views
0 votes
0 answers

Simple C++ swap function

Why is it that if I have a method like this to swap two numbers, it doesn't work[swap], (I know I can accomplish this by defining pointers in the prototype and then passing the addresses of the relevant variables in main()), yet it works for arrays without having to supply pointers and addresses? It does not work. void num_exchange(int m, int n); int main(){ int num1 ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
296 views
0 votes
0 answers

What are Containers/Adapters? C++

What exactly are containers/adapters? I am familiar ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
295 views
0 votes
0 answers

What is meant with "const" at end of function declaration?

I received a book in which it is written: class Foo { public: int ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
293 views
0 votes
0 answers

What is the difference between std::list<std::pair> and std::map in C++ STL?

What distinguishes std::list<std::pair> from std::map? Does the ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
289 views
0 votes
0 answers

Differentiate between function overloading and function overriding

Differentiate between function overloading and function overriding ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
288 views
0 votes
1 answer

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

Jun 20, 2022 in C++ by Damon
• 4,960 points
284 views
0 votes
1 answer

What is a lambda expression in C++11?

In C++11, what is a lambda expression? A: It's the object of an autogenerated class with overloading operator() const under the hood.  Closure is a type of object that is produced by the compiler.  This 'closure' idea is similar to C++11's bind notion.  Lambdas, on the other hand, usually produce better code.  Full inlining is also possible with calls through closures. Q: When do you think I'd utilise one? A: Define "simple and tiny logic" and request that the compiler generate the code from the preceding question.  You tell the compiler the expressions you wish to be inside the operator ().  The compiler will produce everything else for you. Q: What kind of problem do they tackle that couldn't be solved before they were introduced? A: It's some form of syntactic sugar, like using operators instead of functions for custom add, subtract, and other operations... However, wrapping 1-3 lines of genuine logic to some classes, and so on, saves additional lines of needless code!  Some engineers believe that if the number of lines is reduced, there is a lower likelihood of mistakes (which I agree with). Example of usage auto x = [=](int arg1){printf("%i", ...READ MORE

Jun 15, 2022 in C++ by Damon
• 4,960 points
284 views
0 votes
0 answers

memory allocation for objects

In C++, a variable like int x ...READ MORE

Aug 16, 2022 in C++ by Nicholas
• 7,760 points
281 views
0 votes
0 answers

What are the differences between struct and class in C++?

I now want to understand the distinctions ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
280 views
0 votes
1 answer

What does the explicit keyword mean?

To resolve the parameters to a function, the compiler is permitted to do one implicit conversion.  This implies that the compiler can utilise constructors with a single argument to convert from one type to another to find the correct type for a parameter. Here's an example class with a constructor ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
277 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

Jun 2, 2022 in C++ by Damon
• 4,960 points
277 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
276 views
0 votes
1 answer

Use of multisets in C++ [duplicate]

Because a multi-set does not need the storage of single-element objects.  You're considering storing anything in a multi-set, such as a string.  But it is not its intended use.  You may use whatever struct you want and compare it to a single element in the struct. As an example: struct PhoneBookEntry { std::string name; ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
274 views
0 votes
0 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

As a mathematician, one of my pet peeves of C-derived languages is : (-1) % 8 // comes out as ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
268 views
0 votes
0 answers

What type of members can I add in a c++ abstract class

Hello, let's say I have an abstract class with a few pure abstract functions and a few classes that derive from it, and all of the data from these classes eventually becomes similar, I was wondering if it would be wise, or even possible, to declare a vector under protected in the abstract class to collect the data so something like that. class A { protected: vector <string> ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
267 views
0 votes
0 answers

In C, are arrays pointers or used as pointers?

First, look at this code When this is ...READ MORE

Aug 11, 2022 in C++ by krishna
• 2,820 points
264 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
261 views
0 votes
0 answers

Differences between C++ string == and compare()?

I just read some recommendations on using std::string ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
260 views
0 votes
1 answer

What is the name of the "<<" and ">>" operators? [duplicate]

According to cplusplus.com's documentation: This operator (<<) applied to ...READ MORE

Jun 20, 2022 in C++ by Damon
• 4,960 points
260 views
0 votes
0 answers

How to generate a random number in C++?

I'm attempting to develop a dice game, and I need random numbers to represent the sides of the die.  I can make a number between 1 and 6).  Using #include <cstdlib> #include <ctime> #include <iostream> using namespace ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
256 views
0 votes
0 answers

not able to run graphics in DEV C++

Unable to run the graphics from following ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
253 views
0 votes
0 answers

How to concatenate two strings in C++?

I have a private class variable char ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
252 views
0 votes
0 answers

Why was the ampersand chosen as the symbol for references in C++?

Does anyone know why the ampersand was ...READ MORE

Aug 16, 2022 in C++ by Nicholas
• 7,760 points
249 views
0 votes
0 answers

Difference between set.upper_bound() and upper_bound(set.begin(), set.end()) stl

I discovered that set.upper bound() was quicker ...READ MORE

Jun 29, 2022 in C++ by Damon
• 4,960 points
249 views
0 votes
0 answers

How to create a dynamic array of integers

How can I use the new keyword ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
248 views
0 votes
0 answers

Inbuilt __gcd(A,B) function in C++

I recently learned about a c++ specia ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
248 views
0 votes
0 answers

Can you use "cin" with string?

To enter a string, I was instructed to use gets(str) rather than cin.  In the programme below, though, I can use cin perfectly well.  Could you please tell me if you can use cin?  Please accept my apologies for my poor English.  The application allows you to enter up to five names and then print them on the screen. The code is as follows: #include <iostream> #include <string.h> using namespace std; int main() { ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
246 views
0 votes
0 answers

What is the difference between Java and C++?

What is the difference between Java and ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
245 views
0 votes
0 answers

What are the differences between a pointer variable and a reference variable?

What distinguishes a reference variable from a ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
244 views
0 votes
0 answers

Why use #define instead of a variable

What is the purpose of the #define ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
244 views
0 votes
0 answers

Constructor Overloading in C++

My C++ overloading does not work as I expect it to: #include "Node.h" #include <iostream> Node::Node() { cout ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
244 views
0 votes
0 answers

What is the point of function pointers?

The purpose of function pointers is difficult ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
243 views
+1 vote
0 answers

Using push_back vs at in a vector in C++

I'm not sure how to use vectors in C++.  It has to do with the vector's push back technique.  I used push back to insert entries into the vector in the first programme.  I used at() to insert entries into the vector in the second application. #include <iostream> #include <vector> #include <string> using namespace std; int main ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
243 views
0 votes
0 answers

Why aren't cmath's pow and sqrt templates?

I'm curious as to why std::sqrt and ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
240 views
0 votes
0 answers

How to access static members of a class?

I'm learning C++ and Qt, but even the simplest code that I copy and paste from a book produces problems. On Ubuntu 10.04, I'm using g++4.4.2 with the QtCreator IDE.  Is there a distinction between the syntax of the g++ compiler and those of other compilers?  When I try to access static members, for example, something always goes wrong. #include <iostream> using namespace std; class A { ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
239 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
237 views
0 votes
0 answers

What is a stream in C++?

I've been hearing a lot about streams, ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
234 views
0 votes
0 answers

How to pass reference-to-function into another function

Using function pointers as arguments for other ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
234 views
0 votes
0 answers

What are access specifiers? Should I inherit with private, protected or public?

I'm not sure what access modifiers signify ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
233 views
0 votes
0 answers

XOR Operation Intuition

Except for one, every element in an array of numbers appears twice.  Locate that solitary one. Keep in mind that your algorithm should have a linear runtime complexity.  Could you do that without using any more memory? class Solution { public: int ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
233 views
0 votes
0 answers

Why do we use volatile keyword?

I've never used it, but I'm curious ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
232 views