answersLogoWhite

0

📱

C++ Programming

Questions related to the C++ Computer Programming Language. This ranges all the way from K&R C to the most recent ANSI incarnations of C++, including advanced topics such as Object Oriented Design and Programming, Standard Template Library, and Exceptions. C++ has become one of the most popular languages today, and has been used to write all sort of things for nearly all of the modern operating systems and applications." It it a good compromise between speed, advanced power, and complexity.

2,546 Questions

How do you program reverse letters using arrays in C plus plus?

char * RevString( char * str )

{

char * dup = strdup( str );

size_t len = strlen( str );

int x = 0;

int y = len;

while( x

str[ x++ ] = dup[ --y ];

free( dup );

return( str );

}

Note that you don't actually need this function since the standard built-in strrev() function reverses strings more efficiently than this.

How do you run dev-c plus plus on Windows 8?

I don't run either DevCPP or Windows 8. However, I did some checking around and found the following answers. The related links to them are provided beneath this answer.

1) Run DevCPP in Windows XP Compatibility Mode: right-click the icon and select "Run in earlier versions of Windows"

2) Update DevCPP (see the SourceForge related link below)

The problem seems to be that you're running an outdated version of MinGW GCC.

If neither of the above answers help, try a Web search for devcpp "windows 8" or devc++ "windows 8".

Also, keep in mind that Windows 8 is still in beta development, so expect errors and problems to crop up. I'd recommend sticking with earlier Windows versions until at LEAST a service pack gets released if you continue to encounter technical difficulties and are unable to find answers on the Web.

Write a program to create object without name?

You cannot create an object without a name. The name identifies the object. If you really mean anonymous classes, then the following is an example of an anonymous class (a class with no identifier):

typedef struct

{

unsigned x;

unsigned y;

} POINT;

There are some restrictions with regards anonymous classes:

  • They can have no constructors or destructor.
  • They cannot be returned from functions.
  • They cannot be passed as type-checked arguments to functions.

Implement a base class Geo and derived from this class Rect a constraint such as any derived classes from Geo must have method to calculate the area.Derive from Rect class Sqr its const take one value?

This is not a question, it is an assignment.

class Geo {

public:

virtual double area (void) const = 0;

virtual ~Geo (void) {}

// ...

};

class Rect : public Geo {

private:

double m_width;

double m_height;

public:

Rect (double width, double height): m_width (width), m_height (height) {}

double area (void) const override { return m_width * m_height; }

~Rect (void) override {};

// ...

};

class Sqr : public Rect {

public:

Sqr (double width): Rect {width, width} {}

~Sqr (void) override {}

// ...

};

What is unreachable code in c plus plus?

Unreachable code is code that can never execute. It usually happens when you have a logic error in your code, such as when an if condition always evaluates true (or false). For example:

int foo(int& x)

{

if( &x == NULL ) // always evaluates false...

{

// Unreachable code:

std::err<<"int foo(int&) : argument int& is a NULL reference"<<std::endl;

return(-1);

}

return(0);

}

In the above example, the entire if() statement is redundant because the argument, x, is a reference. In C++, references can never be NULL, so the if statement can never evaluate true. Furthermore, in this particular example, the entire function can be said to be redundant (but not unreachable), simply because it serves no purpose. It will always return zero.

What is concession constructor?

Constructors are basically used to evoke methods of a class creating its object..and as far as i know there is no constructor called concession constructor..

What is virtual inheritance?

Virtual inheritance is used in multiple inheritance hierarchies whenever two or more classes inherit from the same base class. For example, consider the following minimal example:

struct A {int x;};

struct B : A {};

struct C : A {};

struct D : B, C {};

Here, A is a common base class of both B and C, thus B::A and C::A are independent instances of A. However, D inherits from both B and C thus indirectly inherits both B::A and C::A. This introduces an ambiguity when implicitly referring to A from D, as in the following example:

D d; d.x = 42; // error: ambiguous

When the compiler sees this it won't know whether we are referring to B::A.x or C::A.x, thus we must be explicit when referring to A via D:

d.B::x = 42; // ok

The problem with this is that we still have two separate instances of x so unless we are extremely careful about which x we are referring to we can easily end up with two different values for x with no way of knowing which was the correct value with respect to D.

What we really need is for B and C to share the same instance of A and we achieve that through virtual inheritance:

struct A {int x;};

struct B : virtual A {};

struct C : virtual A {};

struct D : B, C {};

D d;

d.x = 42; // ok

Note that d.x can be referred to as d::A.x, d::B::A.x or d::C::A.x because they all refer to the same instance of x, thus the ambiguity is eliminated. In effect, the most-derived class in the hierarchy (D) is now a direct descendant of A rather than an indirect descendant of both B::A and C::A.

Note also that the virtual keyword has no effect on independent instances of either B or C because they then become the most-derived classes within their own hierarchy and therefore behave exactly as they would had the virtual keyword been omitted. The virtual keyword only comes into play when we derive from a class with a virtual base.

It is also possible to have a base class that is both virtual and non-virtual in the same hierarchy:

struct A {int x;};

struct B : virtual A {};

struct C : virtual A {};

struct D: A {}

struct E : B, C, D {};

Here, A is a virtual base of both B and C, as before, but not of D. Thus E::A implicitly refers to the virtual A while E::D::A must be explicitly referred to as E::D::A because it is a separate instance. Such classes are best avoided, but if we don't have access to the source code we sometimes don't have a choice in the matter.

However, if a base class holds no data it really doesn't matter whether it is declared virtual or not because a class that has no data consumes just one byte regardless of how many instances there are.

Write a c plus plus program to print prime numbers up to 50?

#include <iostream>

bool isPrime(int p)

{if( p<=1 ( p>2 && p%2==0 ))return( false );int max = (int)sqrt(( double ) p ) + 1;

for( int i=3; i<=max; i+=2 )if( p%i==0 )return( false );return( true );

}

int main()

{for( int i=1; i<50; i>2?i+=2:++i )if( isPrime(i) )std::cout << i << std::endl;return( 0 );

}

How can you make a splash screen in c or c plus plus?

Many people use a modified version of the program's About dialogue window as a splash screen, but you can also create a dedicated splash screen window that contains an image of the company logo along with some program information. The choice is yours but ultimately a splash screen is nothing more than a window that is shown while a program loads in the background. When the program has loaded, the splash screen is closed automatically.

The simplest method of displaying your splash screen is to use a program loader function. Typically this will be your program's main function or a function called from your main function. The program loader first loads and displays the splash screen, then loads the rest of your program in the background before closing the splash screen window and finally showing the main window.

Some splash screens display progress information during the load sequence, thus your program loader should send messages to the splash screen and give time to the system to allow messages to be processed. This will increase the time it takes to load your program, but if the load process is lengthy to begin with, it is better to give some indication of the progress.

How can access an interface from another interface and how to access from a class?

Interfaces are designed to do exactly that: to interface or to interact. In object-oriented programming languages such as C++, you can incorporate up to three different interfaces per class. The private interface is accessible only to the class itself and to friends of the class. The protected interface is the same as the private interface but is also accessible to derivatives of the class. The public interface is accessible to any code.

For one interface to interact with another interface, the first must have access to the second. If the first is a friend of the second or both are members of the same class, the first has unrestricted access to the private, protected and public interfaces of the second. If the first is derived from the second but is not a friend, the first only has access to the protected and public interfaces of the second. If the first is completely separate from the second, the first only has access to the public interfaces of the second.

What is the need for indentation in while writing a c plus plus program?

Indenting is a good habit to inculcate while writing any type of code

Although indentation is not mandatory and will never affect the working of your programme in C++ it makes the code more easy to read and debug especially for larger programmes. Most IDE's eg eclipse automatically indent the code as you type.

What are attributes and methods in class?

Attributes are the class member variables, the data, fields or other properties that define the class state. Methods are the functions of a class, the operations that define its behaviour, typically working in conjunction with the class member attributes to either alter the class state (mutators) or query the class state (accessors). Special methods such as the class constructors, its destructor and conversion operators are invoked indirectly through compiler-generated code while all others are called directly via programmer-generated code.

Why the return type of all the function by default is int in c?

Because int is the most common data-type in C. Don't rely on this though, always specify the return type explicitly.

How a string can be handled by array?

A string is an array. To be precise, a string is an array where every element is a character type. The term string simply relates to the fact the array (like any other type of array) resides in contiguous memory, thus forming a string of characters in memory.

There are in fact two types of string array. The most common is the ASCII string, where each element is 1 byte in length (an array of type char). The other is the UNICODE string where each element is 2 bytes in length (an array of type wchar_t).

In essence, ASCII strings are constructed from character codes 0-255 in the standard ASCII character set, while UNICODE can represent up to 65,536 different character codes, the first 256 being the same as the ASCII character set. UNICODE is generally used to provide character codes that are not available in ASCII, however many systems (Microsoft in particular) handle all strings as UNICODE strings (converting to and from ASCII as required). If you do a lot of string manipulation, then it is often best to use UNICODE at all times to reduce the amount of conversions going on in the background (check your compiler's documentation for specifics).

When dealing with strings we often talk about string buffers. A buffer is simply an array of a specific size (allocated dynamically or statically) within which we can store a sequence of character codes. Since characters are consistently either one byte or two bytes long, it's easy to determine how many characters we can place in these buffers.

However, just to confuse matters, there is actually a third type of string known as a variable-width encoded string. Microsoft calls these MBCS strings, but this really means multibyte character set, which is not the same. The width of a character is determined by the encoding, not the character set. The details don't really concern us, but suffice to say, each character has variable-width and there are many different encoding schemes available. As a general rule, if you must deal with variable-width encoded strings, use UNICODE instead, and only encode as variable width when you must. It'll make your life a lot simpler in the long run.

You will undoubtedly encounter other types of string, such as std::string (and its UNICODE equivalent, std::wstring). This is really just an object that encapsulates a character array so you can use it just as you would a standard character array. However, do not let this fool you -- it is not a character array per se, it is merely a wrapper that mimics an array. There is an underlying array within the object's members, and several useful methods to manipulate the array (such as concatenating two strings), however memory management is handled by the object. While this simplifies things for programmers, it is not the most efficient way to store a string when that is all you want to do. But if you want to actually manipulate the string, then std:string is as good a way as any.

To make use std:string you must include in your project. Do not confuse this with which merely declares a set of functions for manipulating c strings and arrays (the old fashioned way). This header is often called "cstring", which is often confused with the Microsoft-specific class CString (note the captialisation). CString can be likened to std:string in a lot of respects and crops up virtually everywhere in Microsoft objects. It is actually derived from the CStringT template which is itself derived from CSimpleStringT. It is therefore more complex, but the interface is extremely simple to use and if you do a lot of string manipulation, then CString can make life that much easier. But as it is not part of the standard language, its best avoided when code must be shared between platforms.

Write a program to count number of spaces in a string using array?

#include<iostream>

#include<string>

size_t count_spaces(std::string& str)

{

size_t spaces=0;

for(size_t i=0; i<str.size(); ++i)

if( str[i]==32 )

++spaces;

return( spaces );

}

int main()

{

std::string str("This is a string with some spaces.");

size_t spaces = count_spaces(str);

std::cout<<"The string ""<<str.c_str()<<"" has "<<spaces<<" spaces.\n"<<std::endl;

}

1 Define a class in C plus plus to store following information of books Title Author Publisher Price?

class Book

{

public:

Book(std::string title, std::string author, std::string publisher, double price) : m_title(title), m_author(author), m_publisher(publisher), m_price(price) {}

std::string get_title()const{return(m_title);}

std::string get_author()const{return(m_author);}

std::string get_publisher()const{return(m_publisher);}

int get_price()const{return(m_price);}

private:

std::string m_title;

std::string m_author;

std::string m_publisher;

double m_price;

}

What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class.

By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class.

Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected.

Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.

What is persistence in oops explain with example?

The phenomenon where the object outlives the program execution time & exists between execution of a program is known as persistance

What is the function of thymine?

One of four nucleobases in the nucleic acid of DNA, thymine is also known as 5-methyluracil. Thymine creates thymidine when combined with deoxyribose.

How many bits in a bool variable?

It depends on the implementation. Technically, only one, because that's all you need to store a true/false value. From an implementation standpoint, however, the compiler may well allocate a full byte, or even a word, for a boolean variable, because the cost of doing bit manipulation is too much, both in terms of time and of space.

Why is priority configured in 4096 increments when using PVST plus?

Use of the extended bridge ID leaves only four bits for the bridge priority.