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

Write a C program using dynamic memory allocation to subtract two matrices?

The easiest way would be to write a function that accepts two arrays and returns one.

Lets say you are working with two 2-dimensional arrays.

array<int,2>^ SubtractMatrices(int arr1[][2], int arr2[][2]);

void main()

{

int arr1[2][2] = {0,1,2,3};

int arr2[2][2] = {0,1,2,4};

array<int,2>^ newarr;

// array<int^,2>^ newarr[2][2] = SubtractMatrices(arr1, arr2);

}

array<int,2>^ SubtractMatrices(int arr1[][2], int arr2[][2])

{

array<int,2>^ newarr;

//Insert subtraction algorithm here

return newarr;

}

In this scenario you must pass the function 2 matrices, and then return a pointer to a new matrix.

Hmm, still seems to be an error in there with the return type. I'm not sure if that's the correct way to do it, I did this in Visual Studio's managed C++. .

What is the difference between a class method and object in object-oriented C?

Class methods are the member functions that act upon member variables. An object is an instance of a class. C does not support object-oriented programming, but C++ does.

Write a program in C plus plus that shows the following series 1 1 2 3 5 8 13 Using for Loop?

void main()

{

int a=0,b=1,c;

clrscr();

printf("%d",a);

printf("%d",b);

for(;c<=20;)

{

c=a+b;

a=b;b=c;

printf("%d",c);

}

getch();

}

What are the Application of tree and graph in data file structures?

Using binary tree, one can create expression trees. The leaves of the expression tree are operands such as constants, variable names and the other node contains the operator (binary operator). this particular tree seems to be binary because all the operators used are binary operators. it is also possible for a node to have one node also, in case when a unary minus operator is used.

we can evaluate an expression tree by applying the operator at the root to the values obtained by recursively evaluating the left and right sub trees.

What are the backslashes that are use in turbo C plus plus?

Backslashes are used to mark the start of an escape sequence which can be used within character arrays (strings) or as a single character. Thus the escpae sequence '\t' is the TAB character, '\n' is the newline character and '\r' is the carriage-return character. To print a literal backslash, you use a double backslash, '\\'. Note that the backslash and the escaped character that follow are treated as being one character (two bytes of UNICODE, or one byte of ASCII).

This convention is not unique to Turbo C. It was inherited from ISO C.

What type of logical access control method allows you to define who can access an object and type of access that they will have to that object?

That functionality is not available in generic C++, it is a function of the operating system and is therefore platform-specific. Even so, user-credentials cannot be used to determine who can access an object unless you employ some form of biometric security such as fingerprint identification, or a physical security system such as keycards. In the absence of these facilities, you would have to force the user to confirm their credentials every time the object was accessed. After all, the user who originally logged into the system is not necessarily the user currently using the system.

Why you use flag in programming?

We can use flag for many reasons depending upon ur logic but normally people use flag to check which control flow led to this o/p...hmmm to make it more clear EX : if(i%2==0) flag = 1 ; else flag = 0 ; ... ... ... if(flag) print "the number is even" Like this u can use to flag to de-bug ur code or to check the control flow

What does Microsoft Visual C plus plus Runtime Library mean?

The runtime library is a collection of routines that implements basic functionality of the platform. Routines such as I/O, memory control, startup, shutdown, common system functions, etc. are located in the runtime library.

What are the advantages of a class in C over a structure in C?

A C struct only has public member variables whereas a C++ class combines member variables with member functions; the methods that operate upon the data. Moreover, each member of a class can be assigned a different level of access from public, protected or private access, thus limiting the member's exposure. This allows classes to hide data and implementation details from outside of the class, exposing only as much as is necessary in order to use the class. Thus the class becomes entirely responsible for the integrity of its data, while its methods act as the gatekeepers to that data.


Note that in C++, a struct is exactly the same as a class, other than the fact that the members of a struct are public by default while members of a class are private by default, unless explicitly declared otherwise. Aside from that they operate in exactly the same way. In other words, a C++ struct is not the same as a C struct.



What is the code to convert digits in to words in C plus plus?

#include<iostream>

class expand

{

public:

expand(unsigned long long num):value(num){}

std::ostream& operator()(std::ostream& out)const;

private:

unsigned long long value;

static const char * const units[20];

static const char * const tens[10];

};

const char * const expand::units[20] = {"zero", "one", "two", "three","four","five","six","seven",

"eight","nine", "ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",

"eighteen","nineteen"};

const char * const expand::tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",

"eighty","ninety"};

std::ostream &operator<< (std::ostream &out, expand number)

{

return(number(out));

}

std::ostream &expand::operator()(std::ostream &out) const

{

const unsigned long long quintillion=1000000000000000000;

const unsigned long long quadrillion=1000000000000000;

const unsigned long long trillion=1000000000000;

const unsigned long long billion=1000000000;

const unsigned long long million=1000000;

const unsigned long long thousand=1000;

const unsigned long long hundred=100;

const unsigned long long twenty=20;

const unsigned long long ten=10;

unsigned long long multiple=quintillion;

unsigned long long remain;

if(value>=thousand)

{

while(multiple>value&&(multiple!=quintillionmultiple!=quadrillion

multiple!=trillionmultiple!=billionmultiple!=millionmultiple!=thousand))

multiple/=1000;

out<<expand(value/multiple);

switch(multiple)

{

case(quintillion):out<<"-quintillion"; break;

case(quadrillion):out<<"-quadrillion"; break;

case(trillion):out<<"-trillion"; break;

case(billion):out<<"-billion"; break;

case(million):out<<"-million";break;

case(thousand):out<<"-thousand";break;

}

if(remain=value%multiple)

{

if(remain<hundred)

out<<"-and";

out<<"-"<<expand(remain);

}

}

else if(value>=hundred)

{

out<<expand(value/hundred)<<"-hundred";

if(remain=value%hundred)

out<<"-and-"<<expand(remain);

}

else if(value>=twenty)

{

out<<tens[value/ten];

if(remain=value%ten)

out<<"-"<<expand(remain);

}

else

out<<units[value];

return(out);

}

int main()

{

for(unsigned long long ull=0; ull<0xffffffffffffffff; ++ull)

std::cout<<expand(ull)<<std::endl;

return(0);

}

How function declared in C plus plus?

Functions are declared by specifying the function return type (which includes any use of the const qualifier), the name of the function, and the function's argument types enclosed in parentheses (round brackets), which also includes any use of the const qualifier. The declaration ends with a semi-colon. Arguments may also be given default values but if any argument has a default value, all arguments that follow must also have default values.

[const] type name([[const] type [=value][, ...]]);

Declarations may also contain definitions in which case you must include the formal argument names that will be used by the definition. The definition (or implementation) must be enclosed in braces (curly brackets) and the semi-colon must be omitted.

[const] type name([[const] type arg1 [=value][, ...]])

{

statement;

}

Functions must be declared before they can be used. In most cases it is necessary to forward declare functions in a header. In these cases, default values must be omitted from the definition.

Functions that do not return a value must return void. Functions that do not accept arguments must still include the argument parentheses. Use of the void keyword to indicate no arguments is optional. Thus the following declarations are exactly the same (although declaring both in the same program would be invalid):

void f();

void f(void);

As well as the return type and argument types, class member functions (methods) can also be modified with the const qualifier:

struct obj

{

void f(void) const;

};

The const keyword assures the caller that the object's immutable members will not be modified by the function (that is, the internal state of the object's immutable data will remain unaltered). Also, when working with constant objects, only constant methods can be called.

Functions can also be declared static. In the case of external functions, the static keyword limits the visibility of the function to its translation unit. In the case of class methods, static member functions are local to the class as opposed to instances of the class. Static member functions do not have access to an implicit this pointer since they are not associated with any instance of the class but are accessible even when no instances of the class exist.

Difference between function overloading and default arguments?

A default constructor is one where no arguments are declared or required. Thus if all arguments have defaults then it is still a default constructor, but one that can also serve as an overloaded constructor.

Consider the following which has two constructors, one with no arguments (the default) and one with two arguments (an overloaded constructor):

struct A

{

A () : x (42), y (3.14) {}

A (const int a, const double b) : x (a), y (b) {} int x;

double y;

// ...

};

We can invoke these two constructors as follows:

A a; // invokes default constructor (a.x is 42, a.y is 3.14).

A b (0, 1.0); // invokes overloaded constructor (b.x is 0, b.y is 1.0).

Since both constructors are essentially doing the same thing, they can be combined into a single constructor -- we simply make the 'magic numbers' the default values of the overloaded constructor:

struct A

{

A (const int a=42, const double b=3.14): x (a), y (b) {}

int x;

double y;

// ...

};

A a; // a.x is 42, a.y is 3.14.

A b (0, 1.0); // b.x is 0, b.y is 1.0.

As far as the calling code is concerned, nothing has changed, but the class declaration is simplified by removing a redundant constructor.

What is the logical abstrect base class for a class called CricketPlayer?

A logical abstract base class for a class called CricketPlayer could be Batsman class or Bowlerclass.

Return type of malloc?

void * (If you used your help/manual system, you would get an answer much sooner.)

What is difference between C and C plus plus programming?

THE DIFFERENCE BETWEEN c&c++ IS JST THAT c++ IS MODIFIED LANGUAGE AND IT IS A HOGH LEVEL LANGUAGE AS COMPARED c IS A LOW LEVEL LANGUAGE

C++ was developed as a better 'C' language. In addition, C is a procedural language only, and C++ has both procedural language features and object oriented features.

Both are considered high level languages.

C++ is an addition to C. C only allows you to write programs as a list of instructions (procedure), while C++ allows you to write separate objects, and link them together to produce a piece of software.

What is the need of parameterized constructor?

Parametrized constructors are used to increase the flexibility of a class, allowing objects to be instantiated and initialised in more ways than is provided by the default and copy constructors alone. If you define any parametrized constructor, including a copy constructor, you will lose the default constructor generated by the compiler and must declare your own if you need one. The default constructor can also be parametrized, but each parameter must include a default value in the declaration, so that it can be called without any parameters.

What are the disadvantages of function in C?

In comparison to Strawberry Cheesecake, the main disadvantage of a function in C is that you can't eat it.

In comparison to a mature Brandy, the main disadvantage of a function in C is that you can neither smell nor drink it.

In comparison to functions in some, but not many, other programming languages, one limitation of functions in C is that they may not be nested: In C, a function definition cannot contain another function definition.

In comparison to some modern interpreted language, e.g. Python, one limitation of functions in C is that a function cannot generate another function.

Difference between vector and list in c plus plus?

Although they share many of the same features, there are many differences. For instance, a list does not have an index operator [] while a vector does not have a merge method. If in doubt, simply look at the variable's declaration -- it will explicitly state whether the variable is a list or a vector (or indeed some other STL container), along with the type of data that it contains. Ultimately a vector is just an array, ideally suited to random access, whereas a list is ideally suited to sequential access.

What is 10 plus 9 plus 7 plus 6 plus 6 plus 6 plus 5 equals?

It's a simple example of addition.

The solution looks like this:

10 + 9 + 7 + 6 + 6 + 6 + 5 = 49

What is the repetitive control structure in c plus plus?

C provides two sytles of flow control:

  • Branching
  • Looping

Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching: Branching is so called because the program chooses to follow one branch or another.

if statementThis is the most simple form of the branching statements.

It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.

? : OperatorThe ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions.

? : is a ternary operator in that it takes three values, this is the only ternary operator C has.

switch statement:The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read. Using break keyword:If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword.

Try out given example Show Example

What is default condition:If none of the listed conditions is met then default condition executed. Looping Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way. while loopThe most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statements get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false. for loopfor loop is similar to while, it's just written differently. for statements are often used to proccess lists such a range of numbers: do...while loopdo ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once. break and continue statements C provides two commands to control how we loop:
  • break -- exit form loop or switch.
  • continue -- skip 1 iteration of loop.

What is system defined default constructor in java?

System defined constructor or Default constructor is the constructor that the JVM would place in every java class irrespective of whether we code it manually or not. This is to ensure that we do not have compile time issues or instantiation issues even if we miss declaring/coding the constructor specifically. Ex: public class Test { public String getName() { return "Rocky"l } Public static void main(String[] args){ Test obj = new Test(); String name = obj.getName(); } } Here we were able to instantiate the class Test even though we did not declare a no argument constructor. This is the default constructor that gets called when we try to instantiate it.

What are the advantages of Turbo C plus plus?

The only reason to use Turbo C++ as opposed to, say, Visual C++ is because you chose to use a Borland IDE rather than a Microsoft IDE. If the question is why we use C++ rather than a specific IDE, then it is because we want high performance without all the the complexity of a low-level symbolic language such as assembler. If performance were not an issue, we would choose to use a more abstract, high-level language such as Java instead, which is ideally suited to rapid application development, but unsuitable for high-performance applications.

What is global object in c plus plus?

A global object is any object instantiated in the global namespace. The global namespace is anonymous, so if we don't explicitly specify a namespace prior to instantiating an object, that object will be instantiated in the global namespace:

int x; // global

namespace n {

int x; // non-global

};

To refer to the non-global, we must use namespace resolution:

x = 42; // assign to the global

n::x = 42; // assign to the non-global

Is the default access specifier same as protected?

A private member of a class can only be accessed by methods of that class.

A protected member of a class can only be accessed by methods of that class and by methods of a derived class of that class.