What is compilation in c plus plus?
Compilation is the process of translating source files into object files.
Why you use function prototype and function definition in c language program?
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?
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?
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?
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.
What is the meaning of final keyword in Cplusplus language?
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?
There are quite a lot of operators in C and C++, here are the basic ones:
How do you sort the name alphabetically in c or c plus plus?
#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?
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.
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;
}
}
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.