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

What is an iterator in c plus plus?

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?

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?

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?

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?

#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?

#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?

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?

#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?

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?

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.

How do you implement red black trees in c plus plus?

#include<iostream.h>

#include<conio.h>

class add

{

int a,b,c;

public:

void input()

{

cout<<"\n *** Enter the value of a & b *** ";

cin>>a>>b;

}

void output()

{

c=a+b;

cout<<" The value of "<<a<<"+ "<<b<<" = "<<c;

}

};

void main()

{

add a1;

clrscr();

a1.input();

a1.output();

getch();

}

How do you write a C plus plus program that uses Bubble Sort?

template<class T>

void exch( T& x, T& y )

{

T tmp=x;

x=y;

y=tmp;

}

template<class T>

void bubble_sort( T A[], size_t size )

{

size_t last_exch, left, right;

while( size )

{

for( left=0, right=1, last_exch=left; right<size; ++left, ++right)

if( A[right]<A[left] )

exch( A[left], A[last_exch=right] );

size = last_exch;

}

}

Can any one help me to write a program that Print even numbers from 0 to 100 that not a multiple of 6 in C programming?

for (int n = 0; n <=100; n++) // you may start n = 1, same result, 1 less iteration

{

if (n % 6) // or, n % 6 != 0

printf(n);

}

Why is c plus plus regarded as being a hybrid language?

C++ is regarded as hybrid because it is both procedural and objected oriented. A pure c program can be compiled and run on a c++ platform. At the same time, c++ also provides object oriented features like classes, polymorphism, encapsulation, abtraction, etc.

Why upperbound of array in c plus plus overflow during runtime?

An array is simply a contiguous block of memory that is divided into one or more elements of equal size. The array name is itself a reference to the start address of the array, which has the same address as the first element in the array (the element with index 0). The index is essentially an offset from the start of the array, multiplied by the size of an element. However, there is no built-in mechanism in C to prevent you from accessing elements beyond the upper bound of the array at runtime -- essentially overflowing the array. Since C++ inherits from C, the same problem exists in C++.

For instance, in a 10 element array, the upper bound is 9. If you attempt to write to element 10, you are overflowing the array, the buffer, because that memory does not belong to the array. You then introduce undefined behaviour. At best, nothing bad will happen. At worst, people could die. Once you introduce undefined behaviour there's simply no telling what could happen -- it's a time-bomb waiting to go off.

The only way to avoid such problems is to ensure all your array offsets remain within the bounds of the array. That is, the onus is upon the C++ programmer -- just as it still is with the C programmer.

Which version is use of c plus plus in window7?

No. You have to jump through a great many hoops just to get it running on Vista. On windows 7 you can, at best, install the baseline system but you won't be able to apply any of the service packs, rendering the entire package obsolete. Bear in mind that VC++ 6.0 was released in June 1998 and has gone through six major updates since. Moreover, Windows NT4 has been updated five times (Windows 2000, Windows XP, Windows Vista, Windows 7 and now Windows 8) not counting the server editions. If you want to stick with VC++ 6.0 then you'll need to install Windows XP within a virtual machine and target your programs towards the XP platform. However, if you want to write programs for Windows 7 or 8, upgrading your IDE is long overdue. At 15 years old, I think you've had your money's worth.

When a language has the capability to produce new data type is also called?

New, compared to what? I guess you meant user-defined data-types, which exist in almost every modern programming language.

What is string in c plus plus?

A String is a variable that represents a list, or array, of characters. They are normally assigned using double quotes (""). When using strings you must import string header file

#include

and then an example of a string

string mystring = "This is a string";

This give the variable 'mystring' a value of "This is a string".

A string can also be referred to as an array of characters

char string [10];

Would make the variable string to an array of characters with length ten.

Write a c plus plus program to find the largest and second largest number from an array?

#include <iostream.h>

#include <conio.h>

class largest

{

public:

int a[10],i,b,s;

void get();

};

void largest :: get()

{

cout<<"Enter the numbers"<<endl;

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

{

cin>>a[i];

}

b=0;

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

{

if(b>=a[i])

b=b;

else

b=a[i];

}

cout<<"Largest No-"<<b<<endl;

s=0;

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

{

if((s>=a[i])&&(s!=b))

s=s;

else

s=a[i];

}

cout<<"Second Largest No-"<<s<<endl;

}

void main()

{

clrscr();

largest l;

l.get();

getch();

}

When do you need to use default arguments in a function?

We declare (not use) default arguments in a function whenever the default values cover the majority of calls to that function. We use default arguments in order to simplify those calls and thus reduce the verbosity of our calling code, thus making it easier to call the function.

What is the C program for byte stuffing with the output?

//Sender

#include<string.h>

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#define SIZE 40

struct frame

{

char str[SIZE+1];

char tmp[2*SIZE];

char flag;

char e;

char final[2*SIZE];

}s;

main()

{int fd,len1;

int i,j,len;

fd=open("b1",O_WRONLY);

printf("\nEnter flag character and escape character for byte-stuffing:");

scanf("%c %c",& s.flag,&s.e);

printf("\nEnter string:");

scanf("%s",s.str);

len=strlen(s.str);

for(i=0,j=0;i<len;i++,j++)

{

if(s.str[i]==s.flag)

{

s.tmp[j]=s.e;

s.tmp[j+1]=s.flag;

j++;

continue;

}

else if(s.str[i]==s.e)

{

s.tmp[j]=s.e;

s.tmp[j+1]=s.e;

j++;

continue;

}

else

{

s.tmp[j]=s.str[i];

}

}

printf("\nAppended string is==>%s \n",s.tmp);

len1=strlen(s.tmp);

for(i=0,j=0;i<=len1;i++,j++)

{

if((i==0)(i==len1))

{

s.final[j]=s.flag;

s.final[j+1]=s.tmp[i];

j++;

continue;

}

else

{

s.final[j]=s.tmp[i];

}

}

printf("\nFianal string is==>%s\n",s.final);

write(fd,&s,sizeof(s));

}

//Reciver

#include<string.h>

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#define SIZE 40

struct frame

{

char str[SIZE+1];

char tmp[2*SIZE];

char flag;

char e;

char final[2*SIZE];

}r;

main()

{

int fd,len1;

int i,j,len;

mknod("b1",010666,0);

fd=open("b1",O_RDONLY);

read(fd,&r,sizeof(r));

printf("\nFlag character is==>%c\n",r.flag);

printf("\nEscape character is ==>%c\n",r.e);

printf("\nAnd actual message was ==>%s\n",r.str);

printf("\nReceived message is %s\n\n",r.final);

}

/*****************

[mca222@rcclinux mca222]$ cc -os byte_s.c

[mca222@rcclinux mca222]$ cc -or byte_r.c

[mca222@rcclinux mca222]$ ./r&

[1] 1570

[mca222@rcclinux mca222]$ ./s

Enter flag character and escape character for byte-stuffing:#

@

Enter string:sim#andh@ar

Sending message is==>#sim@#andh@@ar#

Flag character is==>#

Escape character is ==>@

And actual message was ==>sim#andh@ar

Received message is #sim@#andh@@ar#

C plus plus program using operator overloading?

class foo

{

private:

int m_data;

public:

foo (int data=0): m_data (data) {}

foo (const foo& source): m_data (source.m_data) {}

foo (foo& source): m_data (std::move (source.m_data)) {}

// operator overloads: assign

foo& operator= (const int source) { m_data = source; return *this; }

foo& operator= (const foo& source) { m_data = source.m_data; return *this; }

foo& operator= (foo& source) { m_data = std::move (source.m_data); return this; }

// compound operator overloads: increment and assign

foo& operator+= (const foo& rhs) { m_data += rhs.m_data; return *this; }

foo& operator+= (const int rhs) { m_data += rhs; return *this; }

};

Write a assembly languagprogram that generates and displays 50 random integers between -20 and plus 20?

There are a few different ways to do this but this one will work just fine for your problem. (note these numbers are not truly random, but they will probably be random enough for your purposes)

you will have to:

import java.util.Random;

then define a new random number generator called Generator:

Random generator = new Random(); Then, make a random integer between 0 and 40 and subtract 20 so it is between -20 and positive 20. int myNumber = generator.nextInt(40) - 20;