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 put a c plus plus program on a website?

If you mean how do you upload the C++ source code to a website, the simplest way is to organise your solution within a parent folder, with sub-folders for each project in the solution, and ZIP the parent folder. You can then upload the ZIP file just as you would any other file.

If you mean how do you upload the source code so that it may be viewed and read in a browser, you will have to copy/paste the code into an HTML file using appropriate tags to format the code. Websites that expect you to upload source code will normally provide you with the tools to do so, usually just by wrapping the code in HTML <code></code> tags. If your code is modularised (multiple source files and headers), you should wrap each file separately and annotate the text to indicate which file is which (or just place the file name as a comment in the source itself, along with any required copyrights). If you're uploading to your own website, then it would be worth looking for a tool that automatically converts your source code into the required HTML.

Static variable in c plus plus?

There are two uses for a static variable in C++. When declared outside of a class, a variable is regarded as being global. However a static variable is deemed local to the file in which it is declared. That is, the variable is scoped to the file, and cannot be accessed by code outside of that file. This aspect was inherited from C.

C++ also allows static variables to be declared inside a class. In this case, the variable is local to the class. By contrast, instance variables (non-static member variables) are local to each instance of the class. With static variables, there is only one instance of each variable which can be shared by all instances of the class. It is not unlike a global but it is scoped to the class.

Since all static variables are instantiated at compile time, they exist for the entire duration a program runs. Even if they fall from scope, they never lose their value. Static variables defined within a class are also available even when no instances of the class are instantiated. Their visibility outside of the class is dependent upon whether they are declared public, protected or private.

What is difference constructor and function in programming C plus plus?

A Constructor is called when you are making a new instance of a class and member functions are functions that you can call within your class or else call using the instance of that class.

for example


class Foo {


public:

int bar;

Foo(int bar) {this->bar = bar;}

inc_bar(); {this->bar++;}

};


Foo instance(10); // Constructor is called and bar is set to 10

cout << instance.bar << endl;

instance.inc_bar(); // call the member function to increment bar

cout << instance.bar << endl;

Is there anything similar between C and C plus plus with reference to data hiding?

No. Data hiding is a feature of object oriented programming. C does not support OOP, and therefore has no private member access. All members are public in C.

A program for queue ADT by using arrays in c plus plus?

Arrays are not suitable for implementing queues because while they are ideal for adding to the end, the are not ideal for extraction from the beginning. For that you need a deque. Regardless, the STL (standard template library) already provides an efficient queue ADT in std::queue.

What is the code for date validation in c plus plus?

You need to check the day, month and year separately, to ensure they are within range. You also need to deal with leap days (29th February) which is every 4 years, but not the 100th unless it is the 400th. Finally, you need to deal with the change from the Julian to Gregorian calendars, which skips the 5th to 14th October, 1582.

bool checkdate(unsigned int d, unsigned int m, unsigned int y)

{

return( !(( d<1 d>31 m<1 m>12 ( y==1582 && m==10 && (d>4 && d<15 )))

( d==31 && ( m==2 m==4 m==6 m==9 m==11 ))

( m==2 && ( d>29 ( d==29 && ( y%4 ( !( y%100 ) && y%400 )))))));

}

How do you get the array values in C plus plus?

Use the array suffix operator ([]), or use a pointer offset from the start of the array, such that an offset of 1 is equivalent to the size of an array element (all elements in an array must be of equal size). The latter method is what actually goes on behind the scenes when using the array suffix operator, but the former is generally easier to use.

What are 4 valid variable types in C plus plus?

long, short, char and bool are 4 valid variable types. There are many more available, including (but not limited to) float, double, int, wchar_t, size_t, as well as compound types (such as long long) and unsigned/signed variations, such as unsigned int. All of these types are primitive, integral or built-in data types. More complex data types can be formed from struct, class and union declarations, but they all simply build upon the integral types.

How can you pause or break a C plus plus program running in DOS-BOX?

There is no pause function as such, but you can easily roll your own:

#include <iostream>

#include <limits>

void Pause()

{

std::cout << "Press ENTER to continue...";

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

}

int main()

{

Pause();

return( 0 );

}

Write a program that input a positive integer n and then prints a triangle of asterisk n lines high and 2n-1 columns wide?

#include<iostream.h>

void main(void)

{

int n;

cout<<"Enter A Value For Triangle:"<<endl;

cin>>n;

cout<<endl;

for(int i=n;i>=1;i--)

{

for(int j=1;j<=i;j++)

cout<<" ";

for(int k=2*n;k>=2*i;k--)

cout<<"*";

cout<<endl;

}

cout<<endl;

cout<<endl;

for(int l=0;l<=n-4;l++)

cout<<" ";

cout<<"ENJOY"<<endl<<endl;

}

How do you create an object type object in c plus plus?

An object is simply an instance of a class.

#include<iostream>

class my_object {};

int main()

{

my_object X; // instantiate an instance of the class my_object, identified as X.

}

Definition of loop and its types and programs in c plus plus?



Executing a segment of a program repeatedly by introducing a counter and later testing it using the if statement.

A sequence of statements are executed until some conditions for termination of the loop are satisfied.

A Program loop consists of two segments:
1.Body of the loop
2. Control Statement


Depending on the position of the control statement in the loop, a control strcture may be classifies either as the 2:

  1. Entry Controlled Loop
  2. Exit Controlled Loop
1.Entry Control Loop-(Pre Test Loop)
The control conditions are tested before the start of the loop execution.
If the conditions are not satisfied , then the body of the loop will not be executed.]
eg:
While Loop

2.Exit Control Loop-(Post Test loop)
The Test is performed at the end of the body of the loop and there fore the body is executed unconditionally for the first time.
eg:
Do-While



while loop
for loop
do-while loop

What is copy construct in c plus plus?

A copy constructor is a separate, compiler created constructor for a class. This allows the programmer to instantiate a class and copy the contents of the given class into the new class. For example:

If I have a class foobar:

Foobar foobar;

Foobar foobar1( foobar );

The second line creates a class in the exact image of the original class, with all of its variables set the same way as the original class.

What is a do while statement in c plus plus?

A do-while statement is a type of loop that iterates while a condition remains true. The condition is evaluated at the end of each iteration, thus the statement always executes at least once.

do {

statement;

} while (expression);

What is overloading function in c and explain with example?

Short answer: You can't.

Long answer: Function overloading is when you define two functions with the same name that take different arguments, e.g.:

void pickNose(int fingerId);

void pickNose(Finger finger);

This is a valid construct in C++, which supports function overloading. If you try this in C, you'll get some error about function already defined.

What is the importnce of file handling?

Most applications cannot operate efficiently (or sometimes even at all) without some method to save and load data of some sort. While a database may suffice in many cases, in others it's impractical (either because availability of a DBMS isn't guaranteed, the data stored isn't enough to justify the added overhead, etc.) In such cases, using a file of some sort is often the only other reasonable option. Doing so requires file handling commands to open and close the file, read from and write to the file, etc.

What supports reusability and extensibility of classes in c plus plus?

There are two ways to reuse a class in C++. Composition and inheritance. With composition, any class data member can be an instance of an existing class. With inheritance, we can derive a new class from an existing class. Either way, we create a new class of object with all the properties of the existing class which can be extended and/or replaced with properties of our own.

How do you handle object array?

Exactly as you would any other type of array. An object's size is determined in the same way a structure's size is determined, by the sum total size of its member variables, plus any padding incurred by alignment.

However, you cannot create arrays of base classes. Arrays of objects can only be created when the class of object is final; a class that has a private default constructor, otherwise known as a "leaf" class. This is because derived classes can vary in size; array elements must all be the same size.

To create an array of base classes you must create an array of pointers to those base classes instead. Pointers are always the same size (4 bytes on a 32-bit system).

Static arrays are ideally suited to arrays of leaf objects where the number of objects never changes, or the maximum number of objects is finite and fixed. Although you can use dynamic arrays of leaf objects, you will incur a performance penalty every time the array needs to be resized, because every object's copy constructor must be called during the reallocation. Dynamic arrays are better suited to arrays of pointers to objects -- only the pointers need to be copied during resizing, not the objects they point to.

How is dynamic initialisation achieved in c plus plus?

The C++ standard has this to say about dynamic initialisation:

"Objects with static storage duration shall be zero-initialised before any other initialisation takes place. Zero-initialisation and initialisation with a constant expression are collectively called static initialisation; all other initialisation is dynamic initialisation."

How do you make chess game in c plus plus programming?

This question cannot be answered here. Go to amazon.com and find a book about chess-programming.

Who created c and c plus plus and java?

Dennis Ritche developed C for bell labs for the unix operating system http://en.wikipedia.org/wiki/C_(programming_language) Bjarne Stroustrup developed C++ http://en.wikipedia.org/wiki/C%2B%2B

How do you increment date using strdate in c plus plus?

In short, you don't.

The strdate() function returns a formatted string that represents the current date. It is not intended that this result be used in a date calculation such as increment. The complexities of converting back to month day and year, and then dealing with the special rules of the Gregorian calender, along with leap years is unrealistic, given that there are library functions that will do this for you already. Try this...

#include <time.h>

#include <iostream>

using namespace std;

...

char cdate[9];

time_t tt = time(NULL) + 86400; // tomorrow

struct tm *tomorrow;

tomorrow = localtime (&tt);

strftime(cdate, 9, "%m/%d/%y", tomorrow);

cout << cdate << endl;

What is the difference between abstract class and normal class?

Any class which has one or more abstract methods is called an abstract class. But in the normal class we can't have any abstract methods.

We cannot create an object for the abstract classes.

When we inherit the abstract class we should implement the abstract method which we inherit.

How do C and C plus plus differ in terms of data abstraction and classes and structs?

They differ insofar as C does not use object-oriented programming at all -- there are no classes (only structures), therefore there was nothing to abstract. C++ (which literally means 'the successor to C') is an extension of C that primarily adds object-orientated support to the language. Everything you can do in C you can also do in C++, but with the added benefits of OOP you can do a whole lot more, more easily, including the creation of abstract data types.