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 can you access sound card through C plus plus language?

C++ is a cross-platform programming language. As such it has no built-in platform-specific features, which includes graphics and audio support. Unlike major graphics cards which all support the OpenGL standard, there is no such standard for audio. OpenAL provides a partial standard but not all cards support it and it has never been regarded as a "standard" of any kind. However, there are many generic libraries available such as FMOD, Irrklang and Clam.

What is meant by throw command in c plus plus?

A 'throw' statement throws an exception when it is generated within a try block. The exception is then caught by the corresponding catch block. For example,

void somefn()

{

float m, n;

try

{

cin>>m>>n;

if (n == 0)

throw(n);

else

cout<<(m/n);

}

catch (float x)

{

cout<<"Division by zero not possible";

}

}

What is the difference between using angular brackets in C language and double quotes in Cpp language to enclose header files?

When you includes any header file using "" that time compiler try to locate this file first from your program's local directory and then from library. But if you include the .h file using <> then compiler assumes you are including some library file.

Write a cobol pgm to find the Fibonacci series?

Identification division. Program-id. Fibonacci. Environment division. Data division. Working-storage section. 77 n pic 9(18). 77 n1 pic z(18). 77 m pic 9(18) value 1. 77 o pic 9(18). 77 i pic 9(4) value 1. 77 q pic x. Procedure division. Para-a. Display ( 1 , 1 ) erase. Display ( 2 , 1 ) "fibonacci numbers from 1 to 100 are:". Move 0 to n. Display " ". Display 0. Display 1. Move 0 to o. Para-b. Compute n = o + m. Move n to n1. Move m to o. Move n to m. Display n1. Add 1 to i. If i = 21 display "press tab key to view next page." accept q. If i = 41 display "press tab key to view next page." accept q. If i = 61 display "press tab key to view next page." accept q. If i = 81 display "press tab key to view next page." accept q if i = 99 go to stop-para else go to para-b. Stop-para. Display " ". Stop run.

Initialization of a constant in c plus plus?

Constants must be initialised at the point of instantiation.

Global constant (internal linkage):

// file: constants.h

const int x = 42;

Every file that includes constants.h gets its own discrete copy of x which the compiler may be able to optimise away (no guarantee). However, internal linkage allows integral constants to be used as integral constant expressions.

Global constant (external linkage):

// file: constants.h

extern const int x;

// file: constants.cpp

external const int x = 42;

External linkage prohibits the use of integral constant expressions other than in the unit that defines the integral. However, for non-integral expressions, external linkage ensures each translation unit uses the same instance of the global, rather than a discrete copy, thus reducing memory consumption.

Ideally, global constants should be placed in a separate namespace to avoid polluting the global namespace:

// file: constants.h

namespace constants { const int x = 42; };

This header can be included in any translation unit that requires it:

// file: source.cpp

#include "constants.h"

int value = constants::x;

Function local constants must be initialised when they are declared:

void foo()

{

const int x = 42;

// ...

}

Function argument constants are initialised when the function is invoked:

void foo(const int x)

{

}

void bar()

{

foo (40+2); // assign the constant expression 42 to the constant x argument in foo

}

Static member constants must be initialised outside of the class declaration (preferably in the class implementation file):

// file: foo.h

class foo

{

static const int x;

};

// file: foo.cpp

const int foo::x = 42;

Nonstatic member constants must be initialised via the class constructor initialisation lists:

class foo

{

const int x;

foo (const int _x = 0): x{_x} {} // default constructor

foo (const foo& source): x{source.x} {} // copy constructor

foo (foo&& source): x{source.x} {} // move constructor (cannot move a constant)

};

Note that objects with constant member variables cannot use copy assign or move assign operators to reassign their constant members. Once assigned, a constant cannot be changed, even with a const_cast.

Write a c program for student marklist without using arrays and pointers?

#include<stdio.h>

#include<conio.h>

void main()

{

int rollno,m1,m2,m3,m4,m5,tot;

float avg;

char [20]name;

clrscr();

printf("Enter the name,rollno,mark");

scanf("%d%d%d%d%d%d%s",&rollno,&m1,&m2,&m3,&m4,&m5,&name);

tot=m1+m2+m3+m4+m5;

avg=tot/5;

printf("the avg is",avg);

getch();

}

What is plus c cor t1 flair on mri?

post gadolinium coronal T1 weighted fluid attenuation inversion recovery sequence

How Can you access Parallel Port using c plus plus?

You cannot directly access the parallel port because the operating system is managing that device. Use the file system. The name of the parallel port is "lpt1:". Open that as an ordinary file for write, write to it, and you will be writing on the parallel port.

Should an optimizing compiler for C or C plus plus be allowed to change the order of sub-expressions in a Boolean expression?

This is definitely a homework question (I'm working on it right now) so I think it's best that you answer this yourself. :)

How do you program a cricket game in c plus plus?

#include //made by debjyoti dasghosh

#include
#include //if u have any problem email me at //debjyoti.dasghosh@yahoo.com
#include
#include
int us,cs,flag=0;
void bat();
void bowl();
void cbat();
void cbowl();
void main()

{

start :

textcolor(3);

us=cs=flag=0;

clrscr();

char toss,name[30];

int rand;

randomize();

cout<<"\n\n\t\tHand Cricket.....\n\n\n\tTOSS\n\t\n\nHead Or Tail \n\nEnter H / T : ";

toss=toupper(getch());

if((toss!='H')&&(toss!='T'))

{

cout<<"\a\n\nEnter H Or T : ";

delay(1000);

goto start;

}

cout<

int i=1;

while(!kbhit())

{

rand=random(2);

switch(rand)

{

case 0 : cout<<" Head ";break;

case 1 : cout<<" Tail ";break;

}

if((i%11)==0)

cout<<'\n';

delay(50);

i++;

}

getch();

redo :

switch(rand)

{

case 0 : if(toss=='H')

{

cout<<"\nYou Won The Toss !!\n\n\nBatting Or Bowling ? \n\nEnter B / L : ";

toss=toupper(getch());

cout<

if(toss=='B')

bat();

else if(toss=='L')

bowl();

else

{

cout<<"\n\nPlease Enter Correct Option : ";

sound(700);

delay(750);

nosound();

toss='H';

goto redo;

}

}

else if(toss=='T')

{

cout<<"\n\nComputer Wins The Toss !! \n\n";

switch(rand)

{

case 0 : cout<<"\n\nComputer Choose To Bat ! ";

getch();

bowl();

break;

case 1 : cout<<"\n\nComputer Choose To Bowl ! ";

getch();

bat();

break;

}

}break;

case 1 : if(toss=='T')

{

cout<<"\nYou Won The Toss !!\n\n\nBatting Or Bowling ? \n\nEnter B / L : ";

toss=toupper(getch());

cout<

if(toss=='B')

bat();

else if(toss=='L')

bowl();

else

{

cout<<"\n\nPlease Enter Correct Option : ";

sound(700);

delay(750);

nosound();

toss='H';

goto redo;

}

}

else if(toss=='H')

{

cout<<"\n\nComputer Wins The Toss !! \n\n";

switch(rand)

{

case 0 : cout<<"\n\nComputer Choose To Bat ! ";

getch();

bowl();

break;

case 1 : cout<<"\n\nComputer Choose To Bowl ! ";

getch();

bat();

break;

}

}break;

}

if(us>cs)

{

cout<<"\n\n\t\t\t***You Win !! ***";

}

else if(cs>us)

{

cout<<"\n\n\t\t\t***Computer Win !! ***";

}

else

{

cout<<"\n\n\t\t\t***Match TIED";

}

cout<<"\n\nDo You Want To Play Again ? \n\nEnter Y / N : ";

toss=toupper(getch());

cout<

if(toss=='N')

{

cout<<"\n\nThankYou For Playing This Game !!";

delay(2000);

}

else

goto start;

getch();

}

void bat()

{

textcolor(5);

clrscr();

randomize();

cout<<"\n\n\n\nUser Batting !\n\n";

int u,c;

do

{

if(flag!=0)

{

if((cs-us)<0)

{

cout<<"\n\n\nYou Win !!";

goto end;

}

cout<<"\n\nYou Need "<<(cs-us)<<" Runs To Win ! ";

}

cout<<"\n\nEnter Shot 1 to 4 and 6 : ";

u=getche();

u-=48;

if((u>6)(u==0)(u==5))

{

cout<<"\aInvalid Shot ! ";

continue;

}

c=random(5);

switch(c)

{

case 0 : c=1 ;break;

case 1 : c=2 ;break;

case 2 : c=3 ;break;

case 3 : c=4 ;break;

case 4 : c=6 ;break;

}

if(u!=c)

{

us+=u;

cout<<"\nYou Win "<

}

else

{

cout<<"\n\nYou Are OUT \n\nYou Have "<

}

}while(u!=c);

getch();

if(flag==0)

{

flag=1;

bowl();

}

end :

}

void bowl()

{

textcolor(2);

clrscr();

randomize();

int u,c;

cout<<"\n\n\n\nUser Bowling !\n\n";

do

{

if(flag!=0)

{

if((us-cs)<0)

{

cout<<"\n\n\nComputer Wins !! ";

goto end;

}

cout<<"\n\nComputer Needs "<<(us-cs)<<" Runs To Win ! \n\n";

}

cout<<"\n\nEnter Style 1 to 4 and 6 : ";

u=getche();

u-=48;

if((u>6)(u==0)(u==5))

{

cout<<"\aInvalid Style ! ";

continue;

}

c=random(5);

switch(c)

{

case 0 : c=1 ;break;

case 1 : c=2 ;break;

case 2 : c=3 ;break;

case 3 : c=4 ;break;

case 4 : c=6 ;break;

}

if(u!=c)

{

//cout<

cs+=c;

cout<<"\nComputer Win "<

}

else

{

cout<<"\n\nYou Have Bowled Out The Computer \n\nComputer Have "<

}

}while(u!=c);

getch();

if(flag==0)

{

flag=1;

bat();

}

end:

}

Does the C plus plus in computer language stand for Cobalt?

C was made in the 70s. Smalltalk was made in the 70s. Squeak was made in 1996, Croquet in 1999, and cobalt in 2007

C was the third in a progression of three languages named A, B, C, from what I heard

A is actually BCPL I think

BCPL B C C++ (P was taken for pascal p-code)

How to draw a Flowchart of swaping of two numbers in c plus plus?

A proper flow chart for that purpose can't be depicted here, but the code would work as follows:

Given the two numbers, A and B:

C = A;

A = B;

B = C;

~if~ the numbers are integers, longs, or other types on which you can use an XOR operator (ie. not floats, doubles, etc.), then you could also do it this way:

A ^= B;

B ^= A;

A ^= B;

That is because of the way the binary XOR works. For example, let's say "A" is equal to to 12 and "B" is equal to 7:

A = 1100

B = 0111

If you run through those operations:

first A ^= B, now:

A = 1011

B = 0111

Now B ^= A:

A = 1011

B = 1100

and again, A ^= B:

A = 0111

B = 1100

or in decimal, A now equals seven, and B now equals twelve. Again, this will only work with fixed point values such as integers. You can't use it on floats.

Binary to octal numbers in c plus plus codes?

The following code demonstrates one way to convert from binary to octal based on user input:

#include<iostream>

#include<string>

typedef unsigned int uint;

std::string input(std::string prompt)

{

std::cout<<prompt<<": ";

std::string s;

std::getline(std::cin,s);

return(s);

}

uint input_binary( uint min, uint max )

{

uint result=0;

bool repeat=true;

while(repeat)

{

result=0;

repeat=!repeat;

std::string s=input("Enter a binary value");

for(uint i=0; i<s.size() && !repeat; ++i)

{

const char c=s.at(i);

if(c<'0'c>'1')

{

std::cout<<"Binary values may only contain characters '0' or '1'\n"<<std::endl;

repeat=!repeat;

}

result<<=1;

result|=(c-'0');

}

if(!repeat)

{

if(result<min result>max)

{

std::cout<<"The binary value must be in the decimal range "<<min<<".."<<max<<"\n"<<std::endl;

repeat=!repeat;

}

}

}

return( result );

}

int main()

{

uint binary = input_binary(0x1,0xffffff);

uint temp=binary;

uint len=1;

while(temp>>=3)

++len;

std::string octal(len, '0');

for( std::string::reverse_iterator i=octal.rbegin(); i!=octal.rend(); ++i )

{

*i=(binary & 0x7)+'0';

binary>>=3;

}

std::cout<<"Octal: "<<octal.c_str()<<"\n"<<std::endl;

return(0);

}

C plus plus program for searching a word from text file?

#include<iostream>

#include<fstream>

#include<vector>

#include<string>

std::string get_input_file()

{

while (true)

{

std::cout << "Enter the input file name: ";

std::string filename;

std::getline (std::cin, filename);

std::ifstream file (filename);

if (file.is_open())

{

file.close();

return filename;

}

std::cerr << "The file could not be opened.\n" << std::endl;

}

}

std::string get_search_word()

{

while (true)

{

std::cout << "Enter the word to search for: ";

std::string search;

std::cin >> search;

if (search.size())

return search;

std::cerr << "The search word cannot be an empty string.\n" << std::endl;

}

}

std::vector<size_t> get_offsets(const std::string& filename, const std::string& search)

{

std::vector<size_t> offsets;

std::ifstream file (filename);

if (!file.is_open())

throw std::exception("The file could not be opened.");

size_t offset = 0;

while (!file.eof())

{

size_t pos = 0;

char c;

while (!file.read(&c, 1).eof() && c!=search[pos])

++offset;

while (pos!=search.size()-1 && !file.read(&c, 1).eof() && c==search[++pos]);

if (pos==search.size()-1 && c==search[pos])

offsets.push_back (offset);

offset += pos;

pos = 0;

}

file.close();

return offsets;

}

int main()

{

std::cout << "Word Search\n" << std::endl;

std::string filename = get_input_file();

std::string search = get_search_word();

std::vector<size_t> offsets = get_offsets (filename, search);

std::cout << "The word was found " << offsets.size() <<

" time" << (offsets.size()==1?"":"s") << ".\n" << std::endl;

if (offsets.size())

{

std::cout << "Offsets:\n";

for (auto off : offsets)

std::cout << off << std::endl;

}

}

Write a c program to generate Fibonacci series using copy constructor?

#include

#include

void main(void)

{

int i,j,k,n;

clrscr();

i=0;

j=1;

printf("%d %d ",i,j);

for(n=0;n<=5;n++)

{

k=i+j;

i=j;

j=k;

printf("%d ",k);

}

getch();

}

Oops concepts in cpp?

Object Oriented Programming (OOP) is the implementation (programming) of Object Oriented Design (OOD). The two usually go together, in in OOD/OOP. OOD is a concept where the design of a solution to a problem revolves around the objects it manipulates, specifically, standardizing the way we think about the attributes (data) and methods (operations) available in an object. While C++ is particularly well suited to OOP, the OOP paradigm can (and should) be extended to cover everything that a program manipulates, even to the extent of simple scalar variables such as an integer. This philosophy is based on some of the definitions of a "object", i.e. that is has storage, the contents of that storage has meaning, and that there are certain operations that can be performed on that storage using a defined interface. Using this concept, even an assembly programming language can be considered to be an Object Oriented Programming Language (OOPL)! What makes C++ and other OOPL's unique are the added concepts of inheritance, polymorphism, and data encapsulation, to name just three. Inheritance is the concept that an object can be declared/defined, and then a child object can inherit the parent object's design and extend it to include other, more specific, functionality. For example, you could design an object (we call them classes) that represents a person. The person class could describe a person's name, address, and social security number. The type of functions (we call them methods) that you could perform on a person could be to set or get their name, address, and social security number. You could then design a class that represents an employee. It could use class person as a base class, and then extend that implementation to include things such as work location, job title, and salary. Class employee has all of the capabilities of class person, but you do not have to write a single line of code for that implementation - you just write the code for the employee extension. Polymorphism is the concept that you can have pointers to various classes, and invoke a same named method, such as "print()", and the code selected will be the code for the type of object to which the pointer refers to. For example, person.print() would behave different than employee.print(). While this might seem trite, it becomes important when you have pointers that can refer to multiple types of related classes, and you want to virtualize the interface to classes pointed to by those pointers at run time, instead of at compile time. Data Encapsulation is, perhaps, one of the most important aspects of OOP/OOD. You design a class, such as person. Inside that class, lets say you define the person's name as an array of characters, say of length 25. You hide that attribute from any derived classes, such as employee, and force access to person.name though a method such as person::getName(). Everything is fine until one day, when you get a person with a 27 character name. You refactor the class person. You could make the array be 64 characters but, instead, you make it a pointer to a dynamically sized array. If you designed the interface person::getName() correctly, then the derived class employee does not need to change, you can relink everything and all is well. This capability can not be overestimated. Prior to OOD/OOP, there was a tendency for programmers to write code that used global variables, variables with scope throughout the program. As the program evolves, functions use those variables in ways that depend on the implementation. One day, you change the implementation, and now you need to go on a "hunt and destroy" mission to find all references to that variable. You might miss some. Bad. Worse, some function misunderstands the variable and uses it for something else, damaging it. Later, often much later, the program crashes, but not at a point that makes any sense. Data encapsulation is the concept of making variable (attributes) of a class be totally private to that class. The defined, or exposed, methods and (sometimes) attributes of a class is called the public interface. So long as that public interface does not change, then any user of that class does not need to be changed when the non-public part of the class changes.

What is abrstac class and virctul function in c?

I have to assume the question is for C#, not C, because C does not provide abstract class concept.

public abstract class A1 { public virtual void SayHi() { Console.WriteLine("Hello World"); }

public abstract void DoSomething();

}

The above abstract class A1 contains 1 virtual method and 1 abstract method. [Note that because of the abstract keyword for Dosomething(), A1 must be declared as abstract. An abstract class DOES NOT have to have any abstract methods!!)

The virtual function SayHi() provides a implementation, while the abstract function provides nothing but only the method signature( the name of the method, the return type, and method parameters and their data types). The derived class of A1 has the option to override SayHi() and must implement (or defer to subclasses of this derived class) the method DoSomething()

Write a program to read and print a strings in C plus plus?

#include
#include

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;

int main()
{
string myString = "";
cout << "Enter a string: ";
getline(cin, myString, '#'); //will stop to read string when you press # (Shift + 3)
cout << endl << "You have entered: " << myString << endl;

system("PAUSE");
return 0;
}

What is the meaning of extensiblity in c plus plus?

In C++, extensibility relates to the way in which the language can be extended by adding new features using nothing more than the built-in features of the language itself, or by building upon the existing features found in the standard library or 3rd party libraries.

For instance, an array of int is a built-in data type, known as a C-style array because it was inherited from the C language. However, C-style arrays are difficult to work with because there's no built-in mechanism to determine an array's length. Even if we know the array's length in advance, in multi-threaded code it's all too easy for one thread to modify the length while another thread is operating upon the array, thus creating a race-condition where the pre-determined length no longer holds true for one of the threads which could lead to a (fatal) overflow.

However, C++ allows us to extend the capabilities of the language by creating new data types that can handle this type of problem gracefully. The new type simply needs to encapsulate a C-style array along with its size, treating the two as a single entity. Such a type already exists in the standard template library (STL); std::vector. However, a vector is not a built-in type, it is a completely new type. The STL is not part of the language itself -- it is just a library like any other -- but it provides a host of new types (mostly container types) that extend the language far beyond the capabilities of the built-in types, the majority of which came from C.

The STL types are general purpose; types that are common to the majority of everyday programmer needs, such as vectors, lists, queues, stacks, red/black trees and so on. However, programmer needs are many and varied, so while many of the STL types can be used as-is, there is often a need to specialise these types to suit a more specific task. Thus every new type you create extends the language further. And with a wealth of 3rd party libraries available, there is essentially no limit to the extensibility of the language. Indeed, the STL continues to evolve as a result of this extensibility, as does the language itself.

What is virtual function in c plus plus?

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class

How do you call 2 or more objects in c plus plus?

It's not clear what you mean by "call 2 or more objects". Object's aren't "called", they are instantiated. Once instantiated you may "call" (invoke) the member methods associated with those objects, or invoke functions that operate upon those objects. To invoke the same member method on 2 or more objects, simply place those objects in a vector (by reference), then iterate over the vector:

void f (std::vector<my_object*> objects) {

for (auto foo : objects) foo->bar(); // invoke the bar method for each foo object in objects

}

How do you write absolute value in c plus plus programmig code?

Answer

#include

#include

#include

void main()

{

long double x;

cout<<"\n Input number: ";

cin>>x;

cout<<"\n Number is "<

getch();

}

// smallest program i could figure out!!!! enjoy!!!

Answer

int abs(int num)

{

if(num < 0)

num = -num;

return num;

}

How to write java program without using main method?

Java program without main

We need a main method for executing a program.

But YES we can write a program without using main() method.

TRICK 1 of 2 :: while writing applets in java we don't use main... we use init() method instead.
TRICK 2 of 2 :: using 'static' we can write a program whic will execute successfully and output the desired message on screen. Here it is :: class Mohit{ static { System.out.println("This java program has run without the main method"); System.exit(0); } } -->save the program as Mohit.java to compile::javac Mohit.java (in command prompt) to run ::java Mohit(command prompt) output will be ::This java program has run without the main method

Whoa!!!!! we are done.


;)