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

Features of constructor of a derived class in c plus plus?

class base

{

base():m_data(0){}

base(int i):m_data(i){}

base(const base& b):m_data(b.m_data){}

private:

int m_data;

};

class derived : public base

{

derived():base(){} // call base class default constructor

derived(int i):base(i){} // call base class overloaded constructor

derived(const derived& d):base(d){} // call base class copy constructor

};

When is a friend function compulsory?

A normal member function has the following qualities:

1. Has private access to the class.

2. Is scoped to the class.

3. Can be invoked on an instance of the class.

Static functions have the first two qualities only. Friend functions have the first quality only.

It therefore follows that friend functions are only compulsory when you need quality 1 only.

What are the syntax for one dimensional array in c plus plus?

type_of_data array_name[number_of_elements_ in_array];

For instance,

int myArray[10];

It creates array of type int, and array consists of 10 elements.

What are the different types of function in c plus plus programming?

There are five types of functions and they are:

  1. Functions with no arguments and no return values.
  2. Functions with arguments and no return values.
  3. Functions with arguments and return values.
  4. Functions that return multiple values.
  5. Functions with no arguments and return values.

Functions with no arguments and no return value.

A C function without any arguments means you cannot pass data (values like int, char etc) to the called function. Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C. This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.

Functions with arguments and no return value.A C function with arguments can perform much better than previous function type. This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments. Functions with arguments and return value.This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function. And this type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value. The data returned by the function can be used later in our program for further calculations. Functions with no arguments but returns value.We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful. The best example of this type of function is "getchar()" library function which is declared in the header file "stdio.h". We can declare a similar library function of own. Functions that return multiple values.So far, we have learned and seen that in a function, return statement was able to return only single value. That is because; a return statement can return only one value. But if we want to send back more than one value then how we could do this?

We have used arguments to send values to the called function, in the same way we can also use arguments to send back information to the calling function. The arguments that are used to send back data are called Output Parameters.

It is a bit difficult for novice because this type of function uses pointer

What is function overridding in c?

Function overriding applies to class methods and is only applicable within a derived class. Derived classes inherit all the public and protected members of their base classes, and all inherited methods can be overridden by the derived class to provide implementations that are specific to the derivative. The base class implementations should be thought of as being more generalised implementations that are specific only to the base class itself. However, some or all of the base class implementations may well be sufficient for your derivative in which case there is no need to override those methods at all. You only need to override methods that require more specialised implementation.

Methods that are declared virtual within the base class are expected to be overridden by the derived class. By contrast, all pure-virtual methods must be overridden otherwise the derived class, like its base class, is rendered abstract. You cannot instantiate an abstract class -- it is intended to be a conceptual object (like a shape) rather than an actual object (like a square or a circle).

You may also override non-virtual methods, however the fact they are non-virtual in the first place means that, barring an oversight on the part of the base class designer, it is not expected that you do so. That doesn't mean that you cannot override them, but it does mean that the base class methods (including all overloaded version of the override) are effectively hidden from the derived class. Often this would be undesirable but, occasionally, that's exactly what you want.

Base classes cannot foresee what classes you might derive from them in the future. Fortunately they do not need to know anything about those classes (if they did, you'd have to continually modify the base class every time you created a new derivative). By declaring methods to be virtual, the virtual table ensures that all derived classes will "do the right thing" even when those methods are called implicitly from within the base class implementations. As a result, there is no need for a base class to ever know the runtime information of its derived classes (and if there is, you'll immediately know there's something very wrong with your class design).

Often an override simply needs to augment a base class method rather than override it completely. This is achieved by calling the base class method explicitly from anywhere within the overridden implementation. This reduces the need to duplicate otherwise functional code that already exists in the base class. It should be noted that base class methods should never be called explicitly from within the base class itself; the override is always expected to be called (implicitly) and this behaviour should only ever be over-ruled by the derivative. Calling a base class method explicitly from outwith the derived class may cause unwanted side-effects because the derived class will no longer "do the right thing".

Pure-virtual methods of a base class may or may not provide an implementation, but either way you must provide an implementation within your override. If calling the base class method explicitly causes a compile time error (stating no such method exists) then the base class provides no implementation at all, therefore you are expected to provide a complete implementation. As an example, a shape class may have a pure-virtual Draw() method but it cannot implement it without knowing what type of shape it is. Whereas a circle class that is derived from the shape class will always know how to draw itself and must therefore provide the complete implementation of the Draw() method.

Overridden virtual methods come into their own when dealing with collections of objects. The objects themselves needn't be of the same type, but so long as they all share a common base class, you can build a collection from the base class alone (all the objects have an "is-a" relationship with their common base class). The collection itself is only concerned with what the base class is actually capable of, as defined by the base class interface, but the virtual table ensures that, regardless of the runtime type of the derived classes, every object in the collection exhibits the correct behaviour.

Write a c programm of insertion of element in the array?

#include<stdio.h>

#include<conio.h>

void main()

{

int a[20],i,j,n;

clrscr();

printf("Enter the no of element you want to insert in the array : ");

scanf("%d",&n);

printf(" Enter the Array element : ");

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

{

scanf("%d",&a[i]);

}

printf("The array is : ");

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

{

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

}

printf(" Enter the element which you want to insert");

scanf("%d",&j);

printf("The array is : ");

for(i=0;i<n+1;i++)

{

if(i==n)

a[i]=j;

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

}

getch();

}

What is nested class and what is its use in c plus plus?

A nested structure is simply one structure inside another. The inner structure is local to the enclosing structure.

struct A { struct B {}; };

Here, we can instantiate an instance of A as normal.

A a;

But to instantiate B we must qualify the type because it is local to A:

A::B b;

If B were only required by A, then we can prevent users from instantiating instances of B simply by declaring it private:

struct A { private: struct B {}; };

What is the method used to implement an if else statement in C?

The else statement is an optional part of an if statement that is executed if the primary if condition is false.

if (condition) true_statementelse false_statement

Why are the combined data from all the class members more meaningful than your results alone?

The combined data from all class members provides a broader and more representative view of the research topic, capturing diverse perspectives and experiences that one individual's results may overlook. This collective data can reveal patterns and trends that strengthen the overall findings, enhancing validity and reliability. Furthermore, it allows for more robust statistical analyses and can lead to more comprehensive conclusions, ultimately enriching the learning experience for everyone involved.

What is a recursive call. Which data structure is used in it?

Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls.

Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.

Is the double a modifier in c plus plus?

A double is a floating point type, greater than or equal in size to a float.

Write a C plus plus Program to find the length of the string using pointer?

// to find the length of the string by using array

#include<stdio.h>

#include<string.h>

int main()

{

int i=0,len[21],n;

char str[8][11];

printf("no of name to be enter");

scanf("%d",&n);

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

{

printf("enter the %d string : ",i);

scanf("%s",str[i]);

}

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

{

len[i]=strlen(str[i])-1;

}

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

{

printf("\n lenght of %d string are ",i);

printf("%d",len[i]);

}

return(0);

}

What are the limitations of friend function in c plus plus?

Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.

Write a C plus plus program to read two integers and display them Use cin and cout statements?

#include
#include
void main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Multiplication of %d and %d is = %d ",a,b,a*b);
getch();
}

What is the Operator c plus plus?

c++ is not an operator, it is a programming language (the successor to the c programming language upon which it was based). However, if c were really an object or variable name, then ++ would be the postfix increment operator. c++ returns the value of c before incrementing c. Its counterpart, ++c, invokes the the prefix increment operator, which returns the value of c after incrementing.

++c is a convenient shorthand notation for c=c+1, which can also be written c+=1, all of which return the value of c after incrementing. c++ is also a convenient shorthand for c=c+1 or c+=1, but the return value is the original value of c, not the incremented value.

How do you calculate cgpa using c plus plus program?

#include <stdio.h>

void main()

{

int num,num1,num2, cal;

num=cal=0;

char grade1=cal=0,grade2=cal=0;

printf("\n Enter the number of subjects taken in Spring Semester:");

scanf("%d", &num);

fflush(stdin);//

if(grade1==4){

printf("\n\nEnter the Math Grade(A,B,C): %c",grade1);

do{

printf("\ngrade1=");

scanf("%d",&cal);

}

else if(

printf("\nError!\n\n");

}while(1);

printf("\nEnter the Math Credit hours(1~3):");

num1 = getchar();

grade1=4;

}

else if(grade2==3){

grade2=3;

}

printf("\nEnter the Math Grade(A,B,C):\n");

scanf("%c",&grade1);

printf("Enter the Physics Grade(A,B,C):");

grade2 = getchar();

printf("\nEnter the Physics Credit hours(1~3):");

num2 = getchar();

printf("\nMath Credit hours: %d",num1);

printf("\nPhysics Grade: %c",grade2);

printf("\nPhysics Credit hours:%d\n",num2);

printf("\n <Math Credit hours> \n");

do{

printf("\n 1 + 1 = ");

scanf("%d", &cal);

}while(cal != 3);

printf("\n Error!\n\n");

}

printf("\n <Physics Credit hours> \n");

do{

printf("\n 4 - 1 = ");

scanf("%d", &cal);

}while(cal != 3);

printf("\n Error!\n");

}

printf("\n The End.\n");

system("pause");

}

What is the storage allocation and scope of global extern static local and register variables?

AnswerLocal Variables are stored in Stack. Register variables are stored in Register. Global variables are stored in data segment. The memory created dynamically are stored in Heap And the C program instructions get stored in code segment and the extern variables also stored in data segment.

Nooo Nooo

Static variable will be stored in .BSS segment... (Block Started By Symbol)

Where string arrays belong in a C program?

There is no data type string in C. String is handled as an array of characters. To identify the end of the string, a null character is put. This is called a null terminated character array. So array of strings will be a double dimensioned array of chars. It is implemented as an array of pointers, each pointer pointing to an array of chars.

What is a static binding c plus plus?

In its simplest definition, "binding", when referred to in the context of any computer programming language, describes how a variable is created and used (or "bound") by and within the given program and, possibly, by other programs, as well. However, there are other definitions for "binding" in computer programming languages, which would take too long to list and explain, especially given the broad nature of the question.

What are the different features of c plus plus?

C++ features a fundamental shift away from the structured programming principles of C. It introduces Object-Oriented-Programming(OOP) to the C language. However, C++ is a wholly new language, even though it is not fully object-oriented like Java and Smalltalk. C++ lets the user choose to program in the paradigm of his choice, but it is obviously advantageous to program in OOP, since it is more equipped to tackle real-world and complex problems. Also, code done using OOP can be more easily maintained, debugged, scaled and distributed. Other advantages of C++ are that it adds even more class-libraries to C. The header files of C++ also provide more built-in functions. On the whole, C++ syntax is also much more cleaned up than C, particularly in console I/O, structures and pointers.

What are the arithmetic and logical operator?

AND, OR, and NOT are the most common ones. There are others, too, such as XOR.




AND, OR, and NOT are the most common ones. There are others, too, such as XOR.




AND, OR, and NOT are the most common ones. There are others, too, such as XOR.




AND, OR, and NOT are the most common ones. There are others, too, such as XOR.


Can you add images in c plus plus?

C++ is designed to be as generic as possible. As such, printing is text-mode only, just as the console is designed for text-mode only. There are no graphics routines in the standard library. To gain graphics output, including image printing, you need an API and library for your specific platform and hardware.

How do you find the product of 2 matrices in C plus plus?

To multiply two matrices, the matrices must support the dot product format. That is, given the multiplication of matrices A * B = C, the dimensions of each array must be A[x][y], B[y][z] and C[x][z]. Note that the number of columns in A must be the same as the number of rows in B.

To calculate the results of elements in C, you are effectively summing the products of corresponding elements in a row of A and a column of B, hence there must be as many columns in A as there are rows in B.

This method of multiplication may seem odd at first, however let's begin with a simple example. Imagine you have a matrix containing the prices for a range of products:

Apple: 30p

Banana: 25p

Orange: 29p

This matrix would normally be represented as a one-dimensional array with three elements.

double A[3] {0.3, 0.25, 0.29};

But as a matrix, it is a two-dimensional array with one row:

double A[1][3] {{0.3, 0.25, 0.29}};

Now imagine you have another array that contains the daily sales quantities for each product:

Day: M T W T F S S

Apple: 5 6 4 3 8 6 7

Banana: 6 9 5 7 3 5 4

Orange: 7 9 4 7 3 5 6

This would be represented as a two dimensional array with three rows and seven columns:

size_t B[3][7] {{5, 6, 4, 3, 8, 6, 7},{6, 9, 5, 7, 3, 5, 4},{7, 9, 4, 7, 3, 5, 6}};

What you want is the total sales value of each day. Thus the total sales for Monday would be (0.3*5) + (0.25*6) + (0.29*7). Repeating this for the remaining days would reveal a one-dimensional array of 7 elements. But as a matrix, it is a two-dimensional array with just one row. Thus we have the following array declarations:

double A[1][3] {{0.3, 0.25, 0.29}};

size_t B[3][7] {{5, 6, 4, 3, 8, 6, 7},{6, 9, 5, 7, 3, 5, 4},{7, 9, 4, 7, 3, 5, 6}};

double C[1][7] {};

Where; x=1, y=3 and z=7.

To work out the totals, you need three nested loops:

for (size_t x=0; x!=1; ++x)

{

for (size_t z=0; z!=7; ++z)

{

for (size_t y=0; y!=3; ++y)

{

c[x][z] += a[x][y] * b[y][z];

}

}

}

Note the order of the loops: x, z and y (not x, y and z).

Now let's see a working example of this:

#include<iostream>

#include<random>

#include<time.h>

template<typename T>

void print_matrix (const T& a, const size_t rows, const size_t cols)

{

for (size_t r=0; r!=rows; ++r)

{

for (size_t c=0; c!=cols; ++c)

{

std::cout << a[r][c] << '\t';

}

std::cout << std::endl;

}

std::cout << std::endl;

}

int main()

{

double a[1][3] {{0.3, 0.25, 0.29}};

size_t b[3][7] {{5, 6, 4, 3, 8, 6, 7},{6, 9, 5, 7, 3, 5, 4},{7, 9, 4, 7, 3, 5, 6}};

double c[1][7] {};

for (size_t x=0; x!=1; ++x)

{

for (size_t z=0; z!=7; ++z)

{

for (size_t y=0; y!=3; ++y)

{

c[x][z] += a[x][y] * b[y][z];

}

}

}

std::cout << "\nProduct prices:\nApples\tOranges\tBananas\n"; print_matrix (a,1,3);

std::cout << "\nWeekly sales:\nMon\tTue\tWed\tThu\tFri\tSat\tSun\n"; print_matrix (b,3,7);

std::cout << "\nTotal sales:\nMon\tTue\tWed\tThu\tFri\tSat\tSun\n"; print_matrix (c,1,7);

}

Output:

Product prices:

Apples Oranges Bananas

0.3 0.25 0.29

Weekly sales:

Mon Tue Wed Thu Fri Sat Sun

5 6 4 3 8 6 7

6 9 5 7 3 5 4

7 9 4 7 3 5 6

Total sales:

Mon Tue Wed Thu Fri Sat Sun

5.03 6.66 3.61 4.68 4.02 4.5 4.84

List and explain bitwise operators in C language?

void main()

{

unsigned int word1 = 077u, word2 = 0150u, word3 = 0210u;

printf ("%o ", word1 & word2);

printf ("%o ", word1 & word1);

printf ("%o ", word1 & word2 & word3);

printf ("%o\n", word1 & 1);

getch();

}

What are proxy classes in c plus plus?

A proxy is defined as any entity that acts on behalf of another entity. For instance, a proxy server is a server that you use to make network calls on your behalf. The proxy server effectively hides your identity from the network because the network only sees the proxy server.

A proxy class is a similar concept -- it is simply a class that acts on behalf of another class. Proxy classes are typically used to simplify the interface to a larger, more complex object.

Note that this is not the same as deriving one object from another. Although you can achieve the same sort of thing with derivation, a proxy class contains a member pointer to the class it acts upon, it does not derive from it. Thus it is free to override the class behaviour, but does not inherit any of its underlying complexity.

"Wrapper" classes are a form of proxy. They contain a class member pointer but they expose a limited or simplified interface to that class member, making more complex calls to that class on your behalf.

Proxy classes can also be used as a reference counting mechanism. Rather than having multiple copies of the same complex object, you can have several lightweight proxy classes all pointing to a single instance of an object, each of which acts on its behalf. Copying lightweight objects does not copy the original object, thus reducing the memory footprint of that object, and when all the lightweight classes finally fall from scope, the original object also falls from scope.