Found 27104 Articles for Server Side Programming

The most elegant way to iterate the words of a C/C++ string

Arjun Thakur
Updated on 11-Feb-2020 10:27:10

1K+ Views

There is no one elegant way to iterate the words of a C/C++ string. The most readable way could be termed as the most elegant for some while the most performant for others. I've listed 2 methods that you can use to achieve this. First way is using a stringstream to read words seperated by spaces. This is a little limited but does the task fairly well if you provide the proper checks. example#include #include #include using namespace std; int main() {     string str("Hello from the dark side");     string tmp;         ... Read More

What is object slicing in C++ or Java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

2K+ Views

Object slicing is used to describe the situation when you assign an object of a derived class to an instance of a base class. This causes a loss of methods and member variables for the derived class object. This is termed as information being sliced away. For example, class Foo { int a; }; class Bar : public Foo { int b; }; Since Bar extends Foo, it now has 2 member variables, a and b. So if you create a variable bar of type Bar and then create ... Read More

What are the rules about using an underscore in a C++ identifier?

Arjun Thakur
Updated on 11-Feb-2020 10:22:52

2K+ Views

From MSDN docs −Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers.So you should avoid using names like −__foo, __FOO, _FOOAnd names like the following should not be used in the global namespace −_foo, _barOther than this, there are some more prefixes like LC_, SIG_, and suffixes like _t ... Read More

Can a local variable's memory be accessed outside its scope in C/C++?

Rishi Rathor
Updated on 11-Feb-2020 10:20:27

124 Views

Let us look at an example where you MIGHT be able to access a local variable's memory outside its scope.Example#include int* foo() {    int x = 3;    return &x; } int main() {    int* address = foo();    cout

Why the use of "using namespace std' considered bad practice?

Abhinaya
Updated on 30-Jul-2019 22:30:21

396 Views

C++ has a standard library that contains common functionality you use in building your applications like containers, algorithms, etc. If names used by these were out in the open, for example, if they defined a queue class globally, you'd never be able to use the same name again without conflicts. So they created a namespace, std to contain this change.The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them.While this practice is okay for example code, pulling in the ... Read More

Why the use of iostream::eof inside a loop condition considered wrong?

Nancy Den
Updated on 23-Jun-2020 13:40:06

139 Views

Just because we haven't reached the EOF, doesn't mean the next read will succeed.Consider you have a file that you read using file streams in C++. When writing a loop to read the file, if you are checking for stream.eof(), you're basically checking if the file has already reached eof. So you'd write the code like −Example#include #include using namespace std; int main() {    ifstream myFile("myfile.txt");    string x;        while(!myFile.eof()) {       myFile >> x;       // Need to check again if x is valid or eof       if(x) { ... Read More

What is The Rule of Three with reference to C++?

Govinda Sai
Updated on 23-Jun-2020 13:40:50

100 Views

The Rule of three is a rule of thumb when using C++. This is kind of a good practice rule that says that If your class needs any ofa copy constructor, an assignment operator, or a destructor, defined explicitly, then it is likely to need all three of them.Why is this? Its because, if your class requires any of the above, it is managing dynamically allocated resources and would likely be needing the other to successfully achieve that. For example, if you require an assignment operator, you would be creating copies of objects currently being copied by reference, hence allocating ... Read More

Why can C++ templates only be implemented in the header file?

Ramu Prasad
Updated on 11-Feb-2020 10:14:53

741 Views

When you instantiate a template in C++, the compiler creates a new class. This class has all the places where you placed the template arguments replaced with the actual argument you pass to it when using it. For example −template class MyClass {    T foo;    T myMethod(T arg1, T arg2) {       // Impl    } };And somewhere in your program use this class, MyClass x;The compiler creates a new class upon encountering this for every type argument you pass it. For example, if you created 3 objects with different template arguments you'll get 3 classes, ... Read More

How to start object-oriented programming in C++?

Arjun Thakur
Updated on 02-Mar-2020 08:09:42

608 Views

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of attributes; and instructions to do things, in the form of methods.For example, a person is an object which has certain properties such as height, gender, age, etc. It also has certain methods such as move, talk, and so on.ObjectThis is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.ClassWhen you define a class, you define a blueprint for an object. This doesn't actually ... Read More

What is the use of the '&' symbol in C++?

Sravani S
Updated on 07-Nov-2023 20:29:43

27K+ Views

The & symbol is used as an operator in C++. It is used in 2 different places, one as a bitwise and operator and one as a pointer address of operator.Bitwise ANDThe bitwise AND operator (&) compares each bit of the first operand to that bit of the second operand. If both bits are 1, the bit is set to 1. Otherwise, the bit is set to 0. Both operands to the bitwise AND operator must be of integral types. Example #include   using namespace std;   int main() {      unsigned short a = 0x5555;      // pattern 0101 ...      unsigned short b = 0xAAAA;      // pattern 1010 ...      cout

Advertisements