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.

500 Questions

What is the advantages of virtual base class?

User Avatar

Asked by Wiki User

Virtual classes are useful when you have a derived class that has two base classes which are themselves derived from a common base class. That is, if class A is derived from classes B and C, and classes B and C are both derived from class D, then class A would inherit two instances of class D (one from B, the other from C). This introduces an ambiguity when referring directly to D from A, because there is no way to determine which instance of D you are referring to. By declaring class D to be a virtual base class of both B and C, they will both share the same instance of D, virtually, thus eliminating the ambiguity.

C program for swap two numbers without using temporary variables?

User Avatar

Asked by Wiki User

Using a temporary variable:

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,temp;

clrscr():

printf("Enter the first number a\n");

scanf("%d",&a);

printf("Enter the second number b\n");

scanf("%d",&b);

printf("Before swapping a=%d b=%d",a,b);

temp=a;

a=b;

b=temp;

printf("The swapped values are: a=%d b=%d",a,b);

getch();

}

Without using a temporary variable:

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

clrscr();

printf("Enter the first number a\n");

scanf("%d",&a);

printf("Enter the second number b\n");

scanf("%d",&b);

printf("Before swapping a=%d b=%d",a,b);

a=a+b;

b=a-b;

a=a-b;

printf("The swapped values are: a=%d b=%d\n");

getch();

}

What is the purpose of template functions?

User Avatar

Asked by Wiki User

Template functions provide the ability to write a single function that can accept different types of inputs and produce different types of output, while still using the same logic. For example, a template function can accept an int, long, or float and perform the same logic on any of the three types of parameters. The compiler ensures that the template function will make sense when compiled with a particular type. This was an early attempt at polymorphism (using the same code on different types of objects), and has since been superseded in newer languages using objects and inheritance.

Does window 7 come with a c plus plus compiler already installed?

User Avatar

Asked by Wiki User

Not in its standard form, but there are modified versions available that will allegedly work under Windows 7. But if you really want to work with Windows 7 programs then you'd best avoid Turbo C++. Use a more generic and up to date version such as GCC.

What is stacks in data structure using c?

User Avatar

Asked by Wiki User

queue is an abstract data type that which entities in a collection are kept in order, this makes the sense of fifo(first in first out).

stack is a container of the object which works on the principle of lifo(last in first out)

What is compilation in c plus plus?

User Avatar

Asked by Wiki User

Compilation is the process of translating source files into object files.

What are the advantages of distributed memory?

User Avatar

Asked by Wiki User

Advantages

  • Can drastically increase processing speed
  • Can be infinitely expandable - just keep adding computers
  • Security through redundancy
  • Collaborative processing
  • Distributed Database

Disadvantages

  • Not all situations are suitable for distributed computing
  • A lot of extra programming is required to set up a distributed system

Why you use function prototype and function definition in c language program?

User Avatar

Asked by Wiki User

Prototypes and definitions are usually kept separate because consumers of functions and classes are not remotely interested in the implementation of the functions or classes, only in their signatures. In some cases, the implementations will be contained in a library (lib) so the implementation source code may not even be available to consumers.

By keeping the prototypes in a separate header, consumers simply need to include the header to make use of the declarations it contains, just as if they'd written all the forward declarations themselves (that is in fact what it means to include a file in your source files). The prototype alone is sufficient to determine how the function or class is used, and the header usually includes documentation either in the header itself or as a separate file. However consumers are only concerned with what a function or class does, not how they do it, so the implementation itself is of little importance.

Write c plus plus program that compare 3 integer numbers and print out the smallest one among them?

User Avatar

Asked by Wiki User

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

int a,b,c,d,g=0,s=0;

a=Integer.parseInt(jTextField1.getText());

b=Integer.parseInt(jTextField2.getText());

c=Integer.parseInt(jTextField3.getText());

d=Integer.parseInt(jTextField3.getText());

if(a>b){

if(b>c){g=a;s=c;}

else{g=a;s=b;}

}

else if(b>c){

if(c>a){g=b;s=a;}

else{g=b;s=c;}

}

else if(c>a){

if(b>a){g=c;s=a;}

else {g=c;s=c;}

}

switch(d){

case 1:JOptionPane.showMessageDialog(this, "the greates no. is"+g);

break;

case 2:JOptionPane.showMessageDialog(this, "the smallest no. is"+s);

break;

default:JoptionPane.showMessageDialog(this," please enter the choice"+"correctly 1 for greatest and 2 for smallest");

}

What is a C plus plus program to demonstrate the use of access specifiers?

User Avatar

Asked by Wiki User

Access specifiers apply to class and struct data types only. If a member is declared before an access specifier is declared, the default access is implied. Once an access specifier is declared, that specifier remains in force until another specifier is declared. Specifiers can be declared in any order and may be repeated as often as required.

The following demonstrates usage and purpose of each specifier.

class X {

friend void f(); // Friends can be declared anywhere.

private: // The default access specifier for class types (implied if omitted).

int a; // Only accessible to members of X and to friends of X.

protected:

int b; // Same as private, also accessible to derivatives of X.

public:

int c; // Accessible to any code where X is visible.

};

struct Y { friend void f(); // Friends can be declared anywhere.

public: // The default access specifier for struct types (implied if omitted).

int a; // Accessible to any code where Y is visible.

protected:

int b; // Same as private, also accessible to derivatives of Y.

private:

int c; // Only accessible to members of Y and friends of Y.

};

struct Z : X

{};

void f()

{

X x;

x.a = 42; // OK! X::a is private and f is a friend of X.

x.b = 42; // OK! X::b is protected and f is a friend of X.

x.c = 42; // OK! X::c is public and X is visible to f.

Y y;

y.a = 42; // OK! Y::a is public and Y is visible to f.

y.b = 42; // OK! Y::b is protected and f is a friend of Y.

y.c = 42; // OK! Y::c is private and f is a friend of Y.

Z z;

z.a = 42; // OK! Z::Y::a is public and Z is visible to f.

z.b = 42; // OK! Z::Y::b is protected and f is a friend of Y.

z.c = 42; // OK! Z::Y::c is private and f is a friend of Y.

}

int main()

{

X x;

x.a = 42; // error! X::a is private and main is not a friend of X.

x.b = 42; // error! X::b is protected and main does not derive from X.

x.c = 42; // OK! X::c is public and is X is visible to main.

Y y;

y.a = 42; // OK! Y::a is public and is Y is visible to main.

y.b = 42; // error! Y::b is protected and main does not derive from Y.

y.c = 42; // error! Y::c is private and main is not a friend of Y.

Z z;

z.a = 42; // OK! Z::Y::a is public and Z is visible to main.

z.b = 42; // error! Z::Y::b is protected and main is not derived from Y.

z.c = 42; // error! Z::Y::c is private and main is not a friend of Y.

}

What are the properties of class in c plus plus?

User Avatar

Asked by Wiki User

A constructor is not a function. A function is a type, as specified by its return type, and must return a value of that type unless the type is void. A constructor does not return anything, not even void.

The purpose of a constructor is to both allocate and initialise memory for an object of the type being constructed. If a valid object cannot be constructed for any reason, the constructor must throw an exception. If the object's class has no data members (attributes), the class does not require a constructor. This is typically the case for most abstract data types and base classes which are used purely as interfaces.

Constructors differ from functions in that all constructors have an initialisation section that is used specifically to initialise non-static data members. The body of the constructor is rarely used except to perform initialisations that cannot be more easily performed by the initialisation section.

A class may have more than one constructor to provide alternative methods of construction based upon the number and type of arguments supplied (if any). When no arguments are required or all arguments have default values then the constructor is known as the default constructor. If the constructor has only one argument the constructor is known as a conversion constructor (because the argument is converted to an object of the class). However, if the constructor argument is a constant reference to an object of the same class, then it is known as a copy constructor, and when the constructor argument is an rvalue reference, it is known as a move constructor. If copy and/or move constructors are provided for a class, the equivalent assignment operators should also be provided for that class. All other constructors are known as user-defined constructors.

How many classes can we write in a single c plus plus program?

User Avatar

Asked by Wiki User

Its limited only by available memory.

What is the meaning of final keyword in Cplusplus language?

User Avatar

Asked by Wiki User

Actually, when you declare a variable as 'final' then the value assingned to the variable does not changes throughout the program.

For example,

class d

{

public final int n=10;

}

In this class d, the value of 'n' would be '10' & its value cannot not be altered by any function declared within the class.

Is C plus plus a procedural or object oriented language?

User Avatar

Asked by Wiki User

There are quite a lot of operators in C and C++, here are the basic ones:

  • && - AND
  • - OR
  • ! - NOT
  • ^ - XOR
  • = - Assign
  • == - Equal to
  • != - Not Equal to
  • + - Add
  • - - Subtract
  • * - Multiply
  • / - Divide
  • % - Modulo
  • < - Less than
  • > - More than
  • <= - Less than or equal to
  • >= - More than or equal to

How do you sort the name alphabetically in c or c plus plus?

User Avatar

Asked by Wiki User

#include

#include

// prints a 2d array of strings

void print2d(const std::string* arr, const size_t rows, const size_t cols)

{

for(size_t row=0; row

{

for(size_t col=0; col

{

std::cout<

}

std::cout<

arr+=cols;

}

}

int main()

{

// example 2d array

const size_t rows = 10;

const size_t cols = 2;

std::string names[rows][cols] = {

"John", "Wayne",

"Johnny", "Depp",

"Michael", "Douglas",

"Anthony", "Hopkins",

"Kirk", "Douglas",

"Daniel", "Craig",

"Tom", "Hanks",

"Sean", "Connery",

"Sean", "Bean",

"Adam", "Baldwin"

};

std::cout<<"\nUnsorted:\n"<

print2d( names[0], rows, cols );

std::cout<

// Sorting algorithm: uses insertion sort

for(size_t row=1; row

{

// copy the current name

std::string copy[2];

copy[0] = names[row][0];

copy[1] = names[row][1];

// store the gap row

size_t gap=row;

// store the compare row (always the row above the gap row)

size_t cmp=gap-1;

// repeat while the gap row is non-zero and the copy name

// is less than the compare name (comparing surname first)

while(gap && (copy[1]

{

// move the compare name into the gap

names[gap][0]=names[cmp][0];

names[gap][1]=names[cmp][1];

// move the gap up one row

--gap;

--cmp;

}

// insert the copy into the gap row

names[gap][0]=copy[0];

names[gap][1]=copy[1];

}

std::cout<<"\nSorted:\n"<

print2d( names[0], rows, cols );

std::cout<

}

Output

Unsorted:

John Wayne

Johnny Depp

Michael Douglas

Anthony Hopkins

Kirk Douglas

Daniel Craig

Tom Hanks

Sean Connery

Sean Bean

Adam Baldwin

Sorted:

Adam Baldwin

Sean Bean

Sean Connery

Daniel Craig

Johnny Depp

Kirk Douglas

Michael Douglas

Tom Hanks

Anthony Hopkins

John Wayne

What is an iterator in c plus plus?

User Avatar

Asked by Wiki User

C++ allows programmers to express ideas in code. Classes are the principal method we use to express those ideas, classifying both data and functions to produce objects that behave in an obvious, intuitive and consistent manner.

If were to define a 3D drawing system we might start with some primitive objects such as spheres, pyramids and cuboids. We could simply define three separate classes for these objects but then we would miss a fundamental aspect: all three share something in common. They are all shapes. While spheres, pyramids and cuboids are all examples of real-world objects, a shape is not a real world object at all, it is merely the concept of an object. Concepts are abstract classes -- we cannot construct objects from them. They don't exist in the real-world but we can certainly imagine they exist and we can easily say that a sphere is a type of shape. If we can imagine it exists then we can express that idea in our code. Our shape class simply describes everything that is common to all shape objects, whether spheres, pyramids, cuboids or any other type of real-world shape. This means we can place all types of shape in containers and treat them as a single entity -- a collection of shape. Each type of shape has its own specialised properties but they also share a common interface: they can all be drawn, erased, moved, rotated, coloured and so on.

Thus we can use the shape class to declare the common interface while each specific type of shape provides the actual implementation. In this way our code does not need to know what specific type of shape it is working with, it is enough to know that it is a shape (of some kind) and we can invoke its draw, erase, move, rotate or colour methods and let the object itself decide how to implement that method.

In short, a concept is an abstraction that allows us to express something that is common between objects that would otherwise be treated separately. It allows us to generalise our code in such a way that we can cater for new objects that do not yet exist. So long as they are conceptually the same, they can be treated the same, without having to alter the code that provides the treatment in any way.

What is simulation recursion in C?

User Avatar

Asked by Wiki User

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) {

if (n==1)

return 1

else

return n* factorial( n-1) ;

}

Does your computer have visual basic c plus plus?

User Avatar

Asked by Wiki User

Basic and C++ are two different languages. You can have them both, but you need to install them. By default Windows OSes do not have it. When Linux based have an option to install C++ compiler.

Why class is called abstract data type?

User Avatar

Asked by Wiki User

an array is a collection of similar elements. abstract data type is a mathematical model which contains a class of data types that have similar behaviour.

so array is an abstract data type.

Write a program to calculate GPA in C plus plus?

User Avatar

Asked by Wiki User

#include

#include

using namespace std;

int main ()

{

cout << endl << "Please enter all the grades you wish to use for your GPA." << endl;

cout << endl << "When you have finished, enter N to indicate that you have no more grades." << endl;

cout << endl << "The highest acceptable grade is 100%, if you got higher than this congratulations but please enter only 100 as grade." << endl;

cout << endl;

float Grade = 0;

int NumClass = 0;

const int Stop = -1;

float GPAVal = 0;

float TotGPAVal = 0;

float GPA = 0;

string SemName;

while (SemName != -1)

{

cout << "Please enter name of the semester of grades you want to enter." << endl;

cin >> SemName;

cout << SemName << endl;

do {

cout << endl << "Please enter grade: ";

cin >> Grade;

if ( Grade <=100 && Grade >= 90 )

{

GPAVal = 4.0;

cout << "Grade: " << Grade << "%; GPA Value: 4.0" << endl;

TotGPAVal = TotGPAVal + GPAVal;

}

else if ( Grade < 90 && Grade >= 80 )

{

GPAVal = 3.0;

cout << "Grade: " << Grade << "%; GPA Value: 3.0" << endl;

TotGPAVal = TotGPAVal + GPAVal;

}

else if ( Grade < 80 && Grade >= 70 )

{

GPAVal = 2.0;

cout << "Grade: " << Grade << "%; GPA Value: 2.0" << endl;

TotGPAVal = TotGPAVal + GPAVal;

}

else if ( Grade < 70 && Grade >= 60 )

{

GPAVal = 1.0;

cout << "Grade: " << Grade << "%; GPA Value: 1.0" << endl;

TotGPAVal = TotGPAVal + GPAVal;

}

else if ( Grade < 60 && Grade >= 0 )

{

GPAVal = 0.0;

cout << "Grade: " << Grade << "%; GPA Value: 0.0" << endl;

TotGPAVal = TotGPAVal + GPAVal;

}

else if ( Grade = Stop )

{

GPA = (TotGPAVal / NumClass);

cout << endl<< "Your overall GPA, after " << NumClass << " courses, is " << GPA << "." << endl << endl;

}

NumClass = NumClass + 1;

}

while (( Grade >= 0 ) && ( Grade <=100 ));

if (SemName = -1)

{

cout << "Thank you for using this program!" << endl;

}

}

return (0);

}

Write a program to overload preincrement and postincrement operator?

User Avatar

Asked by Wiki User

#include<iostream.h>

#include<conio.h>

class num

{

private:

int a,b,c,d;

publ;ic:

num(int j,int k,int m,int l)

{

a=j;

b=k;

c=m;

d=l;

}

void show(void);

void operator++();

};

void num::show()

{

cout<<...................................................................

}

void num::operator++()

{++a;++b;++c;++d;

}

main()

{

clrscr();

num X(3,2,5,7);

cout<<"b4 inc of x";

X.show();

++X;

cout<<"after inc of x";

X.show();

return 0;

}

Changing a word to all uppercase letters in c plus plus?

User Avatar

Asked by Wiki User

To change any string to uppercase, loop through each character in the string. If the character is in the range 'a' through 'z', decrement the character by decimal 32 (the difference between ASCII value 'a' and 'A'). The following function shows an example of this:

void to_upper(std::string& str)

{

for(int i=0; i<str.size(); ++i)

if(str[i]>='a' && str[i]<='z')

str[i]-=32;

}

C plus plus program for inverse of matrix?

User Avatar

Asked by Wiki User

#include<stdio.h>

int main(){

int a[2][2],b[2][2],c[2][2],i,j;

int m1,m2,m3,m4,m5,m6,m7;

printf("Enter the 4 elements of first matrix: ");

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

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

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

printf("Enter the 4 elements of second matrix: ");

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

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

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

printf("\nThe first matrix is\n");

for(i=0;i<2;i++){

printf("\n");

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

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

}

printf("\nThe second matrix is\n");

for(i=0;i<2;i++){

printf("\n");

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

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

}

m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);

m2= (a[1][0]+a[1][1])*b[0][0];

m3= a[0][0]*(b[0][1]-b[1][1]);

m4= a[1][1]*(b[1][0]-b[0][0]);

m5= (a[0][0]+a[0][1])*b[1][1];

m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);

m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=m1+m4-m5+m7;

c[0][1]=m3+m5;

c[1][0]=m2+m4;

c[1][1]=m1-m2+m3+m6;

printf("\nAfter multiplication using \n");

for(i=0;i<2;i++){

printf("\n");

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

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

}

return 0;

}

Sample output:

Enter the 4 elements of first matrix: 1

2

3

4

Enter the 4 elements of second matrix: 5

6

7

8

The first matrix is

1 2

3 4

The second matrix is

5 6

7 8

After multiplication using

19 22

43 50

What is data encapsulation in c plus plus?

User Avatar

Asked by Wiki User

Data encapsulation refers to a means of limiting data access only to those functions that actually require direct access. Object-oriented programming languages such as C++ use access specifiers (public, private and protected) to determine the accessibility (or visibility) of an object's data or, more specifically, its representation.

  • Public member data is accessible to any function (no encapsulation).
  • Private member data is accessible only to the class member functions and to friends of the class (full encapsulation).
  • Protected member data is the same as private member data and is also accessible to implementers of derived classes and to friends of those classes (partial encapsulation).

For most classes, data members are declared private, thus fully encapsulating the data.

How does Prim's algorithm differ from Kruskal's and Dijkstra's algorithms?

User Avatar

Asked by Wiki User

First a vertex is selected arbitrarily. on each iteration we expand the tree by simply attaching to it the nearest vertex not in the tree. the algorithm stops after all yhe graph vertices have been included.. one main criteria is the tree should not be cyclic.