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 many bytes can each of the in c plus plus language data type hold?

The only certainty is that a char holds one byte. All other primitives are implementation dependant. However, you can determine the length of any type, including user-defined types, by using the sizeof() operator.

Can you use visual c plus plus for window programming in c language?

... yes?

The question doesn't really make any sense. Any language can be used to program a computer, so long as you have a compiler for it. There definitely are both C and C++ compilers for Microsoft Windows, therefore you can write device drivers in it if you want.

Heck, you could theoretically write device drivers in LISP or BASIC if you wanted; I personally wouldn't, but it could be done.

What is difference between virtual base class and virtual function?

A virtual class allows children classes to inherit multiple classes that all share a common ancestor. Without this property, the child class would contain multiple copies of the same parent class. A virtual function is a function, with a body, defined in a parent class that may be overridden in the parent class.

Why there is only one main function in every C programmes?

Try putting 2 main functions in a program and see what happens rather than asking easy for you to resolve questions here.

Why cin and cout are iostream objects?

Most programs require both input and output. By including iostream you gain access to both, along with the most common datatypes and functions within the standard library. If you don't require all the features provided, you can include only what you need instead.

Difference between procedural programming and modular programming?

Procedural programming is a computer programming technique in which the program is divided into modules like function or subroutine or procedure or subprograms, where as ... "Modular Programming" is the act of designing and writing programs as interactions among functions that each perform a single well-defined function, and which have minimal side-effect interaction between them. Put differently, the content of each function is cohesive, and there is low coupling between functions as happens in procedural programming.

What is function in C plus plus programming language?

Functions are short procedures or subroutines that can be called as and when required. Unlike goto statements, which branch off to a labelled section of code never to return, functions always return back to the caller. Functions may also call other functions. Functions are typically used to reduce the amount of duplicate code, producing more consistent and error-free code and reducing maintenance whenever functions need to be modified. Functions are also used to make code more readable by replacing complex or compound statements with a simpler function call. By using easy to understand function names, code becomes self-documenting, reducing the need to include verbose comments that only serve to interrupt the flow of the code.

Although there is a performance penalty in calling functions as opposed to inline code, the compiler's optimisation routines can inline expand functions whenever there is an advantage in doing so. Typically this applies to short functions with one or two simple statements, but can also apply to functions that are seldom called.

Functions may also return a value back to the caller (a function that does not return a value simply returns void). Functions can modify their behaviour according to arguments supplied to the function. The number and type of arguments is dependant upon the function signature. The signature also differentiates between functions with the same name, thus allowing functions to be overloaded to cater for different arguments types.

If the argument types are references (or pointers), the function can modify the arguments passed to it unless the arguments are declared const. Passing by reference also allows functions to return more than one value to the caller (typically the return value itself is used to provide diagnostics, such as an error code).

If the arguments are passed by value, however, the function creates a local copy of the argument, thus preventing changes to the argument that was actually passed. If the argument being passed is an object, the object's copy constructor is called automatically. Since this can be detrimental to performance, objects should always be passed by reference unless the function actually needs to work on a copy of the object rather than the original object.

In C++, classes make use of functions, known as member methods, to provide the implementation of operations that may be applied to an object's data. Classes may also employ virtual functions that allow derived objects to provide more specialised implementations whilst retaining the existing generic code. This also helps to reduce duplicate code, but also allows objects to behave polymorphically. That is, it is not necessary for a generic class to know any of the implementation details of its derivatives. You simply call the generic method of the base class and the virtual table redirects the call to the most-specialised override of that function, if one exists. If not, the generic method is called instead. Classes can also make use of pure-virtual functions, such that the base class need not provide any generic implementation at all. These classes are abstract classes which cannot be instantiated by themselves. Instead, you are expected to derive new classes from abstract classes, and the derived classes must provide the implementation even if the base class provides a generic implementation. Only classes that provide or inherit a complete implementation of all pure-virtual methods can actually be instantiated.

Write a program that inputs four digits numbers like 1234 from user and then display it in reverse order in c?

#include<stdio.h>

main()

{

int i,j,k,n;

printf("Enter the number of lines");

scanf("%d",&n);

for(i=n;i>=0;i--)

{

for(k=n-i;k>0;k--)

{

printf(" ");

}

for(j=i-1;j>=0;j--)

{

printf("%2d",i);

}

printf("\n");

}

}

How do you write a C plus plus program to print the area of rectangle using overloading class?

#include<iostream>

struct point

{

int x;

int y;

double length (const point& p) const;

};

double point::length (const point& p) const

{

int w = x - p.x;

int h = y - p.y;

return std::sqrt ((w*w) + (h*h));

}

struct rectangle

{

size_t width;

size_t height;

};

struct triangle

{

point A, B, C;

double length_a () const { return B.length (C); }

double length_b () const { return A.length (C); }

double length_c () const { return A.length (B); }

double perimeter () const { return length_a() + length_b() + length_c(); }

};

size_t area (triangle& t)

{

const double s = t.perimeter() / 2;

return std::sqrt (s * (s - t.length_a()) * (s - t.length_b()) * (s - t.length_c()));

}

size_t area (rectangle& r)

{

return r.width * r,height;

}

int main()

{

triangle t {{0,10},{10,0},{0,0}};

rectangle r {10,5};

std::cout << "Triangle area: " << area(t) << std::endl;

std::cout << "Rectangle area: " << area(r) << std::endl;

}

Program to find reverse of given number using pointers?

#include

#include

void main()

{

int rev num=0;

while(num>0)

{

rev num=rev num*10+num%10;

num=num%10;

}

return rev_num;

}

int main();

{

int num=4562;

printf("reverse of number is%d",reverse digit(num));

getch();

return o;

}

What is the meaning of plus plus in c?

++a (plus plus a) is pre-incrementing operator to a
a=10;
printf("%d",++a); /* it will print 11 as ++a increment first a by 1 then prints it */

printf("%d",a++); /*it will printf 10 as it is post _ increment operator , it prints the value a first then increment it by 1 */

When does a function need an include directive?

A function requires an include directive whenever it makes use of a data type or function that cannot be forward declared, or where a forward declaration would be undesirable, and where a complete declaration of that type or function exists in another file.

That may sound far more complex than intended, but it's really quite simple. The include directive is a signal to the compiler that the specified file is to be inserted into your file in its entirety, just as if you'd typed all the code it contains by hand. This is clearly a huge time saver when it comes to common data types and functions, but how do you decide if an include directive is actually required or not? Simple: comment out the directive and attempt to compile. If it fails, the directive is required. The errors raised will indicate exactly what types or functions it exposes and where they are used.

Most C/C++ programmers split their declarations from their definitions. Declarations of functions and data types (including class declarations) are usually placed in a header file (.h), while the definitions of the functions are safely tucked away in a separate source file (.cpp). Programmers are rarely interested in the implementations -- the header should contain all the information necessary to make use of the functions and data types contained therein. But in order to compile, the source file must include the header file. And if the two files must include other files, then we generally place those directives in the header file thus ensuring that the header contains all the necessary information to allow it to be included in other files.

Can you run java program in turbo c or c plus plus?

Turbo C++ pre-dates the earliest C++ standard, ISO/IEC 14882:1998 (informally known as C++98). As such it is impossible to write standards-conforming C++ programs in Turbo C++. In particular, it lacks template metaprogramming and exception handling both of which are central to C++., and there is no standard template library because it didn't exist at the time. It's also 16-bit which limits its memory usage to 64KB, although DPMI can extend that limit to some degree.

Development of Turbo C++ effectively ceased in 1994. It was later revived in 2006 with two separate releases, Explorer (the free version) and Professional, but none of its inherent problems were resolved and no further development took place. Embarcadero acquired the software from Borland in 2008 and support was discontinued in 2009.

Today, the free version of Turbo C++ still finds uses in educational institutes (primarily in Asia) despite the fact it's of no practical use in learning C++ let alone writing C++ programs.

Turbo C++ was succeeded by C++ Builder. The current stable release is v10.1 Berlin, released in 2016.

Can you use more than one constructor in a class in c plus plus?

Yes, you can use, and often you should, use more than one constructor in a class in C++.

Normally, there is a default constructor, a copy constructor, and one or more conversion constructors. Sometimes, there are also other constructors, overloaded based on argument signature, if there are complex situations.

What is a constructoris it mandatory to use constructor in a class?

You always should define default constructor for your class. You must also define a copy constructor for your class if there are any pointers in the class.

While it is not mandatory, failure to provide a default constructor can result in bad behavior, and failure to provide a copy constructor when you have pointers in the class will result in bad behavior.

For example, without a default constructor, the compiler will not fully initialize the attributes of the class. It will initialize the virtual function table, and call base class constructors, but that is all - the attributes could be random garbage.

For another example, without a copy constructor, the compiler will generate one that simply makes a bit wise copy of the attributes. If these attributes contain pointers, then you have two pointers to the same object, not necessarily a good thing, especially if one of them get "deleted".

Write a c plus plus program to print even numbers using for loop?

  1. #include

void main()

{

int num,i;

printf("\n Enter number upto which even numbers must be displayed \t");

scanf("%d",&num);

printf("\n Even numbers less than %d are \n",num);

for(i=2;i

{

printf("%d ",i);

}

getch();

}

What is the difference between Virtual Base Class and Abstract Class in c plus plus?

You might as well compare apples and oranges. The only thing they have in common is that they are both base classes (just as apples and oranges are both fruit).

An abstract base class is an abstract data type (ADT). An ADT is defined as any class that declares one or more pure virtual functions. This prevents anyone from instantiating objects from the class other than through derivation. The derivative must implement all pure-virtual methods declared in its base class, otherwise it becomes an ADT itself. Once a derivative overrides a pure-virtual method, that method becomes virtual with respect to all subsequent derivatives. However, only derivatives that provide or inherit a complete implementation for the sum of all pure-virtual methods can physically be instantiated.

A virtual base class is a base class that is inherited virtually. That is, if class A is declared a virtual base class of class B, then any derivative of B will automatically inherit directly from A. This is useful in multiple inheritance where two or more intermediate classes are themselves derived from a common base class. Normally, the most-derived class will inherit one instance of the common base class from each of the intermediate base classes. By declaring the common base class to be virtual in the intermediate classes, the most-derived class inherits just one instance of the common base class, which is then shared amongst the intermediate classes.

Thus if class A inherits from classes B and C and they each inherit from D, then there will be two instances of D in the hierarchy (A::B::D and A::C::D). This introduces ambiguity when referring directly to A::D. But if B and C inherit from D virtually, then there is only one instance of D (A::D), and both B and C derive from this one, shared instance, virtually. This removes any ambiguity and reduces the overall footprint of the hierarchy by eliminating redundant instances of the shared base class.

Ideally, virtual base classes should have little or no data members.

Can you overload destructor for your class?

No. Classes can only have one destructor, whether you define one yourself or allow the compiler to generate one for you. The compiler-generated destructor is public by default, does not release any memory allocated to any class' member pointers, and is non-virtual, which are the three main reasons for defining your own.

What the difference between call by reference and call by value return in c plus plus?

In a pass by pointer (*, the original C standard), you're not passing the actual variable but rather its address in memory. This is required because in standard C, all function parameters are passed by value and therefore disappear when the function call is completed. In order to access and alter the original variable, you have to dereference the pointer using the address provided in the pointer argument, and this must be done each time the original variable is altered. Keeping this in mind requires vigilance to be sure you know which object referring: the variable or its memory address.

In a pass by reference (&, introduced in C++), you're telling the compiler that the variable being passed into a function has to be fundamentally linked to the original. The compiler takes care of the dirty work and lets you refer to the variable with simple assignment statements--no pointer handling required.

How to overloade operater in c plus plus?

The assignment operator overload is a typical example of operator overloading. Every class of object has an assignment operator by default, which simply assigns the value of the members of an existing class to the members of another existing class. The two classes may even be the same class. However, the default implementation is not suitable for classes that contain pointers to memory that cannot be shared with other classes. Assigning the value of one pointer to another simply copies the pointer's value (a shallow copy) but not what it points at (a deep copy). Thus the assignment operator must be overloaded to perform a deep-copy, as per the following example:

struct X {

// destructor

~X(){ delete( m_pointer ); }

// default constructor

X(): m_pointer( new int(100) ){}

// copy constructor

X( const X& x): m_pointer( x.m_pointer ? new int( *x.m_pointer) : NULL ) {}

// assignment operator overload

X& operator= ( const X& x );

int* m_pointer;

};

X& X::operator= ( const X& x )

{

if( this != &x ) // test for self-reference

{delete( m_pointer );m_pointer = x.m_pointer ? new int(*x.m_pointer) : NULL;}

return( *this );

}

In the above example, the assignment operator overload first checks to ensure that the incoming object is not a self-reference. If it is not, then we release the current allocation and create a new allocation, copying the incoming object's memory by value. If we had not checked for a self-reference, then we could easily end up deleting the very memory we wished to copy, such as in the following example:

X x;

X* y = &x;

x = *y; // self-assignment

In the above example, x and *y are obviously references to the same object, but it may not be quite so obvious if the assignment occurred elsewhere in our code. Therefore the object's assignment operator overload must cater for this eventuality and veto the assignment -- thus leaving the original object unchanged.

The assignment operator can also be overloaded in order to prevent assignment. You would do this if the object were intended to act as a singleton, for instance. In this case we simply need to declare the operator as private to the class but do not need to provide any implementation. Alternatively, we can simply return a reference to the current instance:

X& operator= ( const X& x ) { return( *this ); }

No other operator has a default implementation when applied to classes, therefore if we want to make use of other operators upon our classes, such as +, ++ or +=, then we must overload them in the class declaration. Again, each overload must test for self-references and act accordingly. However, while there's nothing to prevent you from providing a completely unique implementation of these operators, common sense dictates that all such implementations must be intuitive and predictable. While it may be fun to implement the plus operator as a minus operation, it has no practical value in the real world -- it is neither intuitive nor predictable.

The stream insertion (<<) and extraction (>>) operators are another typical example of operator overloading. These cannot be implemented within the class itself because the l-value of these operators must be a stream, while internal class operator overloads use the current instance of the class as the l-value. Therefore these operators must be implemented outside of the class.

Many programmers implement these "external" operators as friend functions. While some do have valid reasons for doing so, if the public class interface provides everything required of these operators then there is no need to declare the operator as a friend function. Although some programmers continually claim friendship undermines encapsulation this is not the case at all. Friends simply extend the class interface, nothing more. However, if the underlying class implementation is altered, all friend functions may need to be updated to cater for these changes -- which increases the maintenance cost. Therefore, if operator overloads can be implemented with friend access, then your class becomes that much easier to maintain.

Here's a typical example of an external operator implemented as a friend function:

struc X

{

friend ostream& operator<<( const X& x );

private:

int m_data;

};

ostream& operator<<( ostream& os, const X& x )

{

os << x.m_data;

return( os );

}

Now here's the same operator overload implemented without a friend function:

struc X

{

int GetData() const { return( m_data ); }

private:

int m_data;

};

ostream& operator<<( ostream& os, const X& x )

{

os << x.GetData();

return( os );

}

While there's clearly the need for an extra function call, that call is completely eliminated by virtue of the fact X::GetData() will be inline expanded by the compiler. Thus the latter will not affect performance in any way. However, the main advantage of the non-friend function comes when you later decide to alter the implementation of the class:

struc X

{

int GetData() const { return(( int ) m_data ); }

private:

double m_data;

};

Here we've changed the member variable to a double, but the GetData() accessor method still returns an int. The accessor is therefore an abstraction, thus the insertion operator overload is left unaffected by the internal change to the class. However, had we made this same change in the earlier example employing the friend function, there is no abstraction and the extraction operator will insert a double into the output stream. If this were undesirable then we'd have to alter the friend function as well. Not only have we increased the maintenance, we've completely undermined the abstraction of our class. Note that we have not undermined the encapsulation (as some would claim), only the abstraction has been undermined. Thus if you want to retain the abstraction, do not use a friend function. Friend functions should only be used when abstraction is not an issue, and the function genuinely requires access to the private members of the class.

What is latest version of c plus plus?

C++ doesn't have versions, it has standards. The implementations of those standards have versions, but the version numbering differs from one implementation to another. The current standard (as of 6th February 2017) is ISO/IEC 14882:2014.

Giving an example explain what is Operator Overloading?

Overloading refers to use of something for different purposes.

Function Overloading - It refers to the design of family of functions with one function name but with different argument list. The correct function will be invoked by checking the number and type of the arguments.

Operator Overloading - The mechanism of giving the additional property to the operator is known as operator overloading. We can overload all the operators except the following:

1.)Class members access operators

2.)Scope resolution operator

3.)Sizeof operator

4.)Conditional operator

What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point.

Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself.

Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.

What are multiple constructors in c?

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.

When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.

A constructor is declared without a return value, that also excludes void.

Therefore, when implemented, do not return a value:

Constructor Exampleclass rectangle { // A simple class int height; int width; public: rectangle(void); // with a constuctor, ~rectangle(void); // and a destructor }; rectangle::rectangle(void) // constuctor { height = 6; width = 6; }

sagar sainath samant. sybsc (computer science)

What is the summary of c plus plus and sql in a programming language?

Your question is unclear. C++ IS a programming language. SQL is not (it is a specialized language for defining relational databases, inserting/retrieving/modifying data and specifying user access.)