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

List of graphics commands in c plus plus?

That's easy. There aren't any. C++ is designed to be as generic as possible, thus it provides generic text-based output only. Graphics output is platform-specific and is therefore not provided by the C++ standard. You can, of course, use graphics in C++, but you need a library and API that is specific to your platform and hardware. Most IDEs will provide an appropriate library, but as soon as you use graphics in your programs, you will generally lose the benefit of compiling your code on other platforms, unless you write your code specifically to cater for other non-generic libraries that are suitable for compilation upon other platforms.

What is the difference between prefix and postfix increment operator in c plus plus?

Both the prefix and the postfix increment operators increment the operand. The difference is what is the value of the expression during the evaluation of the expression. In the prefix form, the value is already incremented. In the postfix form, it is not.

int a = 1;

int b = ++a;

// both a and b are now equal to 2

int a = 1;

int b = a++;

// a is equal to 2 and b is equal to 1

How to write a C program to generate Fibonacci series up to 10 elements and store in array?

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

int swap(int* , int*);

int main()

{

int c=0;

int b=1;

int max;

cout<<"Enter the maximum limit of fibbonacci series = ";

cin>>max;

if(max>0)

{

cout<<c<<endl;

}

else

{

exit (EXIT_FAILURE);

}

for(int i=0;i<max;i++)

{

c=b+c;

swap(&b,&c);

if(c<max)

{

cout<<c<<endl;

}

else

{

break;

}

}

getch();

return 0;

}

int swap(int *x,int *y)

{

int z;

z=*x;

*x=*y;

*y=z;

return z;

}

Why you are using strcpy in c plus plus?

In short, you don't. strncpy is deemed unsafe as it has potential to cause buffer overruns. To copy strings safely in C++, use std::string instead.

For examples and syntax, see related links, below.

What program do they use to make c plus plus compilers?

The first generation C++ compiler was written in C. Newer generations of C++ compilers are written using the previous generation of C++, however some implementations also use assembler, either in part or in whole.

Bear in mind that one of the first programs ever written for a computer was an assembler. Before assembler, all code had to be written in machine code, the native language of the computer, which was labour intensive and prone to error. But that was exactly how the first generation assembler had to be written. Thereafter, the assembler was used to create the next generation assembler, and the next, until high-level languages began to appear (again, written in assembler), until C finally appeared, which eventually led to C++.

What is rtti in c plus plus programming?

RTTI is runtime type information. It is a basic requirement of the dynamic_cast operator in order to ensure all casts between polymorphic objects within a class hierarchy are done safely. Other cast operators such as safe_cast also utilise RTTI. However, RTTI has an overhead that can be detrimental to performance thus RTTI is often utilised in debug code to verify C-style casts in release code (such as reinterpret_cast) are always done safely. Both static_cast and reinterpret_cast are inherently unsafe, however static_cast does invoke user-defined conversions, whereas reinterpret_cast does not, and should only be used as a last resort. But try telling Microsoft that...

In many cases, RTTI can be eliminated completely by utilising a well-designed class hierarchy with virtual interfaces that avoid the need to cast between object types. In other words, it should not be necessary for any base class to even be aware of any of its derived class' types (let alone their specialities) in order to use them. By calling virtual methods, casting between types is done safely and automatically, behind the scenes, by virtue of the virtual table. Moreover, a v-table has a much lesser overhead than RTTI.

That said, there will be times when RTTI is the only solution to a problem, especially when working with legacy code and libraries for which you have little or no control over. If there's ever any doubt about what a pointer is really pointing at, use RTTI. Better safe than sorry.

Write a program to print n natural numbers using do-while loop?

//(this is just a function, call this as count(n) in your main loop)

void count(int n){

int i = 1;

do{

printf("%d", i);

i++;

} while(i<=n);

}

OOPs stands for Object Oriented Programming then whats that extra s stands for?

As per the website, www.acronymfinder.com, OOPS stands for Object-Oriented Programming and Systems.

Regards,

Anthony

anthonymail@rediffmail.com

How do you write a program to compute and print the factorial of given number using a while loop in c plus plus?

// returns n!

int fact(final int n) {

// keep track of factorial calculation in f

// f starts at n, and we will multiply it by all integers less than n

int f = n;

// loop from n-1 down to 2

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

// increase our total product

f *= i;

}

return f;

}

Can you get STD's when you are a virgin?

The only STDs you can be confident about are those that were specifically tested for. Your partner cannot infect you unless he or she is already infected. However, you can infect your partner if you are infected.

There are several STDs that are not routinely tested for as part of an STD examination. Tests for herpes and HPV are expensive and in the case of herpes not always reliable. Health care providers usually only test for those if there is an indication that someone is infected (for HPV usually an abnormal Pap smear).

Unless you are certain you are entering a long term monogamous relationship and that you have both been extensively tested it is unwise to engage in sexual activity without condoms.

If you are entering a relationship where you are going to have skin on skin sex, without condoms, you could establish some certainty about STDs as follows:

1. HIV test.

2. Herpes test

Its all very gloomy and unromantic doing this, but if you want to do it properly, see a doctor and suggest these three steps.

Structure in c?

Structures are a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth.
->A structure is a collection of related elements , possibly of different types , having a single name.

->"each element in a structure is called field".

->A FIELD is a smallest element of named data that has meaning. It has many characteristics of the variable .

->It exits in memory . it can be assigned values, which in turn can be accessed for selection or manipulation.

-> A field differs form variable primarily in that it is a part of structure.

ITS SYNTAX IS:

struct tag name

{

field list;

};

Token separation source code in C Plus Plus?

\\ Token Separation \\

Write a program to identify and generate the tokens present in the given input

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<iostream.h>

int key = 0;

char expr[100];

char cont[][20]={"CONTROLS","for","do","while","NULL",};

char cond[][20]={"CONDITION","if","then","NULL"};

char oprt[][20]={"OPERATOR","+","-","*","/","%","<","<=",">",">=","=","(",")","NULL"};

char branch[][20]={"BRANCHING","goto","jump" ,"NULL"};

void checking(char[],char[][20]);

void main()

{

int i,j,l,k,m,n;

char sbexpr[50],txt[3];

clrscr();

cout<<"Enter the expression:";

gets(expr);

for(i=0;expr[i]!=NULL;i++)

{

key=0;

for(j=i,k=0;expr[j]!=32 && expr[j]!=NULL;i++,j++,k++)

sbexpr[k]=expr[j];

sbexpr[k]=NULL;

if(key==0) checking(sbexpr,cond);

if(key==0) checking(sbexpr,cont);

if(key==0) checking(sbexpr,branch);

if(key==0)

{

for(m=0;sbexpr[m]!=NULL;m++)

{

key=0;

txt[0]= sbexpr[m];

txt[1] = NULL;

if(key==0) checking(txt,oprt);

if((key==0) ((sbexpr[m]>=97 && sbexpr[m]<=122) (sbexpr[m]>=65 && sbexpr[m]<=90)))

{

cout<<"\n"<<sbexpr[m]<<"------->"<<"Identifier\n";

key = 1;

}

}

}

if(key == 0)

{

cout<<"\n"<<sbexpr<<"------->"<<"Address\n";

key = 1;

}

}

getch();

}

void checking (char expr[],char check[][20])

{

for(int i=1;strcmp(check[i],"NULL")!=0;i++)

{

if(strcmp(expr,check[i])==0)

{

cout<<expr<<"------>"<<check[0]<<"\n";

key = 1;

}

}

}

What is difference between Near structure and far structure?

We haven't used near and far addressing for well over a decade. It was common in older systems where memory was addressed by segment and offset. For instance, on a 32-bit system we might use 16-bits to address the segment and 16-bits to address the offset within that segment. If we were referring to an offset within the current segment then we'd use a 16-bit near pointer, but if we needed to refer to another segment then we'd use a 32-bit far pointer. Today we use a normalised pointers.

What is pointer to an object in c plus plus?

A pointer is simply a variable that stores a memory address. Thus a pointer to an object is simply a variable that stores the memory address of an object. Since pointers are variables, they require memory of their own. Pointers may also be constant, which simply means you cannot change what they point to. Pointers can also be dereferenced to provide indirect access to the memory they point to -- hence they are known as pointers. However, unlike C, pointers are not the same as references. In C++, a reference is simply an alias for a memory address and requires no storage of its own.

Write a c plus plus program to add two matrix using arrays?

Matrix Add

/* Program MAT_ADD.C

**

** Illustrates how to add two 3X3 matrices.

**

** Peter H. Anderson, Feb 21, '97

*/

#include <stdio.h>

void add_matrices(int a[][3], int b[][3], int result[][3]);

void print_matrix(int a[][3]);

void main(void)

{

int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };

int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };

int r[3][3];

add_matrices(p, q, r);

printf("\nMatrix 1:\n");

print_matrix(p);

printf("\nMatrix 2:\n");

print_matrix(q);

printf("\nResult:\n");

print_matrix(r);

}

void add_matrices(int a[][3], int b[][3], int result[][3])

{

int i, j;

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

{

result[i][j] = a[i][j] + b[i][j];

}

}

}

void print_matrix(int a[][3])

{

int i, j;

for (i=0; i<3; i++)

{

for (j=0; j<3; j++)

{

printf("%d\t", a[i][j]);

}

printf("\n");

}

}

Can you connect to database in unix environment using c or c plus plus languages?

At the simplest level, a database is simply a data container. As such an array can be considered a database. However, when we think of a database we usually imagine a container that combines one or more related tables of data, that allows us to easily modify data, to search for data, and so on. Databases are typically disk-based centralised repositories (data servers) containing a massive amount of data, while end-users (data clients) are really only concerned with a small amount of that data (a subset). Data can also be generated by the database, such as when returning the number of records in the database that match a specified criteria.

Database Management Systems (DBMS) are the simplest way to make and use a database as all the functionality you need is readily available, all you need do is connect to the DBMS and query it, typically using a text-based script such as SQL (structured query language). MySQL is a popular choice because it is open source and can be used under the terms of the Gnu Public Licence (GPL).

What is 999 plus 98756434221?

12.1932631112635318

if you have a calculator large enough, you can work the rest out yourself

How do you print a pyramid of numbers C plus plus?

#include<iostream>

#include<vector>

#include<string>

#include<random>

#include<ctime>

void print_pyramid (const std::vector<int>& v)

{

size_t tiers {v.size()}, spaces {1}, row {1};

while (tiers > row)

{

tiers -= row++;

++spaces;

}

tiers = spaces + 1;

std::vector<int>::const_iterator i=v.begin();

for (size_t tier=0; tier!=tiers; ++tier)

{

if (spaces)

std::cout << std::string(spaces--,'\t');

for (size_t num=0; num!=tier && i!=v.end(); ++num)

std::cout << *i++ << "\t\t";

std::cout << std::endl;

}

}

int main()

{

std::default_random_engine generator ((unsigned)time(0));

std::uniform_int_distribution<int> distribution (1,100);

for (size_t loop=0; loop!=5; ++loop)

{

size_t max = distribution (generator);

std::vector<int> v;

v.reserve (max);

for (size_t i=0; i!=max; ++i)

v.push_back (distribution (generator));

print_pyramid (v);

}

}

Arrays in c?

1>an array is a static data structure.after declaring an array it is impossible to change its size.thus sometime memory spaces are misused.

2>each element of array are of same data type as well as same size.we can not work with elements of different data type.

3>in an array the task of insertion and deletion is not easy because the elements are stored in contiguous memory location.

4>array is a static data structure thus the number of elements can be stored in it are somehow fixed.

C plus plus programming language program for hybrid inheritance?

There's no such thing as hybrid inheritance in C++. Hybrid inheritance implies two or more different types of inheritance but there are really only two types of inheritance in C++ and they are mutually exclusive: single inheritance and multiple inheritance.

A class that inherits directly from one class uses single inheritance.

A class that inherits directly from two or more classes uses multiple inheritance.

The only way to combine these two inheritance patterns is through multi-level inheritance, where a class inherits directly from one or more derived classes. However, whenever we create a derivative, we're only concerned with the base class or classes we are directly inheriting from. The fact they may or may not be derivatives themselves is largely irrelevant from the viewpoint of the derivative. Indeed, the only time we really need to consider one of the lower bases classes is when we need to explicitly invoke a virtual function of that particular class, as opposed to implicitly invoking the most-derived override of that function as we normally would. However, this is really no different to a derived class override invoking its direct base class method.

Virtual base classes are also thought of as being a type of hybrid inheritance, however virtual base classes merely determine which class is responsible for the construction of those classes. Normally, a derived class is responsible for the construction of all its direct base classes, which must be constructed before the derived class can begin construction. In turn, those base classes are responsible for the construction for their own base classes. In this way, derived classes are automatically constructed from the ground up, base classes before derived classes, in the order declared by the derived class.

For example, consider the following hierarchy:

struct X {};

struct Y : X {};

struct Z : Y {};

Z inherits from Y so in order for a Z to exist we must first construct a Y. By the same token, Y inherits from X so in order for a Y to exist we must first construct an X. Thus when we initiate construction of a Z, that initiates construction of a Y which initiates construction of an X.

Now consider a virtual base class:

struct X {};

struct Y : virtual X {};

struct Z : Y {};

The construction sequence is exactly the same as before (X before Y before Z), the only difference is that when we now instantiate a Z, as the most-derived class in the hierarchy it becomes responsible for the construction of the virtual X. Z is also (still) responsible for the construction of a Y, but Y no longer needs to construct an X because a (virtual) X already exists.

Virtual base classes become more relevant in multiple inheritance, where two or more base classes share a common base class:

struct W {};

struct X : virtual W {};

struct Y : virtual W {};

struct Z : X, Y {};

Here, Z uses multiple inheritance from X and Y. Both X and Y use single inheritance from W. Without virtual inheritance, Z would inherit two separate instances of W, specifically X::W and Y::W. But by declaring W as a virtual base of X and Y, the most-derived class, Z, becomes responsible for the construction of W, as well as its direct base classes, X and Y. Neither X nor Y need to construct a W because a W will already exist. Thus X::W and Y::W now refer to the same instance of W.

Note that we do not need to write any additional code for this mechanism to work. The virtual keyword alone is all we need. Even if X or Y provided explicit initialisation of W, those initialisers would be ignored by the compiler since initialisation of W is automatically the responsibility of the most-derived class. The only time those explicit initialisers would be invoked is if we explicitly instantiate an instance of X or Y, because then X or Y become the most-derived class.

What are range of character data type in c plus plus?

The range of character data types in C++ is the set of characters in the host's character set.

It is inappropriate to consider the numerical range of such things as characters, because that depends on the particular codeset involved, such as ASCII, EBCDIC, UNICODE, KANJI, etc. Doing that leads to non-portable code, and lazy programming practices. Do not write code that depends on the collating sequence of the character set, or the numerical difference between two characters.

Type char can be signed or unsigned, the value range is -128..127 or 0..255 respectively.

What is the difference between the calling function and called function?

Function calling is when you call a function yourself in a program. While function invoking is when it gets called automatically.

For example, consider this program

struct s

{

int a,b,s;

s()

{

a=2;

b=3;

}

void sum()

{

s=a+b;

}

};

void main()

{

struct s obj; //line 1

obj.sum(); // line 2

}

Here, when line 1 is executed, the function(constructor, i.e. s) is invoked.

When line 2 is executed, the function sum is called.

Disadvantages of object-oriented programming language?

There are no disadvantages in OOP itself -- it's just a tool. Like any other tool, if used appropriately, there can be no negatives. But if used inappropriately, companies could collapse and countries could fall. The secret to good OOP code is simply good design. Get that wrong and you'll spend most of your time redesigning instead of addressing the problem you were actually trying to solve.

Not every problem can be addressed with OOP -- it is not a silver bullet (and was never intended as such). If the data is modelled on a relational database, then OOP is probably the best approach, but for anything else you'd be better looking elsewhere before considering OOP. By the same token, C++ isn't always the best language to use when you want a working solution as quickly as possible, but at least you get the choice whether to use OOP or not. Some languages give you no option whatsoever, which is disadvantageous by itself.

According to the Gregorian calendar it was Monday on the date 01 01 1900 if any year is input through the keyboard write a c program to find your what is the day on 1st January of this year?

#include <stdio.h>

int main ()

{

int year,days,d;

printf("year?"); scanf("%d",&year);

days = 365*year + (year-1)/4 - (year-1)/100 + (year-1)/400 ;

d=days%7;//to find which day of week

if(d==1)

printf("\n\n\tmonday");

else if(d==2)

printf("\n\n\ttuesday");

else if(d==3)

printf("\n\n\twednesday");

else if(d==4)

printf("\n\n\tthursday");

else if(d==5)

printf("\n\n\tfriday");

else if(d==6)

printf("\n\n\tsaturday");

else if(d==0)

printf("\n\n\tsunday");

return(0);

}