What does c plus plus certification mean?
C++ was originally called "C with Classes" in 1979, as that is precisely what C++ was at the time. The name was changed to C++ in 1983. ++ is the increment operator in C, thus C++ literally means C=C+1. Hence C++ is the next "incremental" version of C. By rights it should really be called ++C (prefix increment) because, although C++ (postfix increment) increments C, the evaluation is the original value of C.
Can a static function be made virtual?
No. Virtual functions are invoked according to the runtime type of the object. That is; the most-derived override is automatically invoked even when the runtime type of the object cannot be determined at compile time. This is achieved through the object's virtual table. Static methods do not have any object associated with them; they can be invoked even when no object of the type exists. Without an object, there can be no virtual table. Thus static functions cannot be virtual. They are mutually exclusive concepts.
It means you hold down the <Alt> key, press <C>, and then release them both. Interpretation of this action is dependent on the particular application. Look at the menu bar. If there is a word with a C that is underlined, then <Alt><C> will invoke that item. Otherwise, consult the documentation for keyboard shortcuts. For example, Mozilla Firefox defines <Alt><F><C> as the command to close a tab.
C program for the two dimensional array repesentation of priority queue?
//implement double ended queue using array.
#include<stdio.h>
#include<conio.h>
#define SIZE 20
typedef struct dq_t
{
int front,rear;
int item[SIZE];
}deque;
/********** Function Declaration begins **********/
void create(deque *);
void display(deque *);
void insert_rear(deque *, int);
void insert_front(deque *, int);
int delete_front(deque *, int);
int delete_rear(deque *, int);
/********** Function Declaration ends **********/
void main()
{
int data,ch,x;
deque DQ;
clrscr();
create(&DQ);
printf("\n\t\t Program shows working of double ended queue");
do
{
printf("\n\t\t Menu");
printf("\n\t\t 1: insert at rear end");
printf("\n\t\t 2: insert at front end");
printf("\n\t\t 3: delete from front end");
printf("\n\t\t 4: delete from rear end");
printf("\n\t\t 5: exit. ");
printf("\n\t\t Enter choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
if (DQ.rear >= SIZE)
{
printf("\n Deque is full at rear end");
continue;
}
else
{
printf("\n Enter element to be added at rear end :");
scanf("%d",&data);
insert_rear(&DQ,data);
printf("\n Elements in a deque are :");
display(&DQ);
continue;
}
case 2:
if (DQ.front <=0)
{
printf("\n Deque is full at front end");
continue;
}
else
{
printf("\n Enter element to be added at front end :");
scanf("%d",&data);
insert_front(&DQ,data);
printf("\n Elements in a deque are :");
display(&DQ);
continue;
}
case 3:
x = delete_front(&DQ,data);
if (DQ.front==0)
{
continue;
}
else
{
printf("\n Elements in a deque are :");
display(&DQ);
continue;
}
case 4:
x = delete_rear(&DQ,data);
if (DQ.rear==0)
{
continue;
}
else
{
printf("\n Elements in a deque are :");
display(&DQ);
continue;
}
case 5: printf("\n finish");
return;
}
}
while(ch!=5);
getch();
}
/********** Creating an empty double ended queue **********/
/********** Function Definition begins **********/
void create(deque *DQ)
{
DQ->front=0;
DQ->rear =0;
}
/********** Function Definition ends **********/
/********** Inserting element at rear end **********/
/********** Function Definition begins **********/
void insert_rear(deque *DQ, int data)
{
if ((DQ->front 0)
{
printf("\n Underflow");
return(0);
}
else
{
DQ->rear = DQ->rear -1;
data = DQ->item[DQ->rear];
printf("\n Element %d is deleted from rear:",data);
}
if (DQ->front==DQ->rear)
{
DQ->front =0;
DQ->rear = 0;
printf("\n Deque is empty(rear end)");
}
return data;
}
/********** Function Definition ends **********/
/********** Displaying elements of DEQUE **********/
/********** Function Definition begins **********/
void display(deque *DQ)
{
int x;
for(x=DQ->front;x<DQ->rear;x++)
{
printf("%d\t",DQ->item[x]);
}
printf("\n\n");
}
/********** Function Definition ends **********/
Where can you download a free turbo c plus plus tutorial?
You can download Turbo C++ from the Embarcadero website for free. Embarcadero now own and develop all CodeGear products previously owned and developed by Borland, including the latest version of C++ Builder.
How do write a program in c plus plus for addition and subtraction of two complex numbers?
#include<iostream>#include<complex>
int main () {
using namespace std;
complex a {1, 1};
complex b {42, 2};
cout a << " + " << b << " = " << a + b << endl;
cout a << " - " << b << " = " << a - b << endl;
}
How array elements are retrieved?
Array elements are retrieved by specifying the offset of the element you want. Since array elements are a fixed size, you can also retrieve elements using simple pointer arithmetic. In actual fact, this is the underlying mechanism used when you supply an offset.
Consider the following array declaration:
int myArray[5] = { 1, 2, 3, 4, 5 };
Memory is allocated to myArray according to the type of the element specified. The type is int, therefore each element will be exactly 4 bytes in length. The subscript [5] allocates 5 elements to this array, thus 20 bytes (5 x 4 bytes) are allocated in total.
myArray refers to the first address of the memory location where the memory was allocated. It therefore refers to the first element in the array, which is offset 0. To retrieve the value stored at offset 0 we specify the offset as a subscript:
int x = myArray[0];
This effectively copies the value 1 (the value we stored in the first element) and places it in the memory allocated to x.
If we want to copy the last element, the fifth element, we specify the final offset, which is 4.
int y = myArray[4];
As before, this copies the value 5 and places it in the memory allocated to y.
Note that to access the nth element of an array, we specify the offset n-1. Offsets are zero-based, so the last element of an n dimension array is also offset n-1.
If we use pointers, we can better understand the underlying mechanism. myArray refers to the starting address of the entire array, so we can dereference the array by allocating a pointer to this memory location.
int * p = myArray;
This copies the address of myArray and stores it in the memory allocated to p. Therefore p points at the first element in the the array and we can retrieve the value of that element by using the indirection operator.
int z = *p;
This is the same as saying z = myArray[0].
To access the next element (found at offset 1), we simply increment the pointer's value by 1.
++p;
Note that if the value in p was originally the memory location 0x00123456, it is easy to assume it must now be 0x00123457 -- a difference of 1 byte. But p was declared as a pointer to int, so we actually increment the memory address by 4 bytes. So, p now stores the address of myArray[1].
So when we retrieve the element myArray[1], what we're really doing is accessing the value stored 1 x 4 bytes away (offset) from the memory location referred to by myArray. All this is done behind the scenes, but is no different to allocating a pointer to myArray, incrementing it by the specified offset, and finally returning the value through indirection.
We can also use indirection to change the value of an element:
*p = 10;
This is the same as saying:
myArray[1] = 10;
The only problem to be aware of when working with pointers is that your pointer can end up pointing at memory outside of the array. However the same can be said of any pointer. Always make sure you know what you're pointing at before attempting to access the value stored at that location.
Define object oriented data model?
It is based on collection of objects.An object contain values stored in instance variables within the object.An object also contains the bodies of code that operate on object.These bodies of code is called methods.Collection of instance variables and method is called classes.
How do you create a file in c plus plus?
Is it a file or a folder you want to create? Use open/creat/fopen for the former, mkdir for the latter.
Example:
FILE *f;
int rc;
rc= mkdir ("C:\\AA");
if (rc) perror ("Error in mkdir");
f= fopen ("C:\\AA\\MYNEWFILE.TXT", "w");
if (f==NULL) perror ("Error in fopen");
What is the use of assembler in c plus plus?
an assembler is a program,just like a compiler.that processes staaements written in a particular high level language and produces an object code in a low level language (assembly language) that the computer can understand.
What is formal parameter in c?
A variable declared in the header of a method whose initial value is obtained during method invocation (using the actual parameter). So: int method(/*Formal Parameters*/){ //Method Actions return 0; }
What is the need of virtual base classes in c plus plus?
Virtual functions are advantageous in that they allow us to write code in a more generic fashion more easily than would otherwise be possible. More importantly, without virtual functions, polymorphic behaviour would be impossible to achieve.
Consider the following example:
#include<iostream>
struct instrument
{
virtual ~instrument() {}
virtual void play () =0;
};
void foo (instrument& a, instrument& b)
{
a.play();
b.play();
}
struct drum : instrument
{
void play () override { std::cout << "Playing a drum\n"; }
};
struct guitar : instrument
{
void play () override { std::cout << "Playing a guitar\n"; }
};
int main()
{
drum d;
guitar g;
foo(d, g);
}
Output:
Playing a drum
Playing a guitar
There are several things to note about this program. As you know, in C++, all types must be declared before they can be used. However, note that the foo function was defined before the drum and guitar classes were declared and yet it was perfectly able to invoke their specific methods even without a forward declaration. This is made possible because the function knows about the instrument class and that's all it really needs to know about; the instrument class exposes the precise interface required by the foo function.
Note also that the instrument class has a virtual destructor. Unless a class is marked final, a virtual destructor tells us that we may derive a new class from this class. In other words, it tells us that this class can be used as a base class. It is important to note that every class that declares one or more virtual methods MUST also have a virtual destructor. I shall explain why shortly.
The instrument::play() method is a virtual method. The '=0' suffix denotes that the function is pure-virtual. Not all base classes require pure-virtual methods, but in this case we do because the instrument class is a conceptual class rather than a concrete class. That is, it is an abstract object; one we can imagine exists but cannot physically exist in reality. By declaring at least one pure-virtual method, we prevent end-users from constructing objects from the class.
A pure-virtual method may or may not be implemented by its base class, but it must be implemented by its derivatives otherwise they become abstract themselves. By contrast, virtual methods must be implemented in the base class but need not be implemented by the derived classes.
Whenever we declare a method virtual, we guarantee that the most-derived override for that method will always be invoked. Thus when we construct a drum object, its play() method becomes the most-derived override. This is how the foo function was able to invoke drum::play() despite the fact it only knew of the existence of the instrument::play() method. Indeed, the foo function doesn't even know that a refers to a drum any more than b refers to a guitar. All it needs to know is that they are both types of instrument and that is guaranteed by the compiler because both drum and guitar are derived from instrument.
While it is true that every drum is an instrument, it is not true that every instrument is a drum (it could be a guitar). How does the foo function know which of the two overrides to invoke? That's precisely the type of problem virtual functions help to resolve. Every class in a hierarchy has a virtual function table (or v-table) which maps the virtual functions of the base class to the most-derived overrides of those functions. Thus when play() is invoked against an object, the class v-table determines the correct function address according to the actual runtime type of the object.
While the v-table does consume additional memory over and above the size of the class members, the cost is minimal (approximately two words per virtual method). However, sometimes it becomes necessary to "switch on" the runtime type of an object in order to evoke specific behaviour. For instance, suppose our drum object has the following interface:
struct drum : instrument
{
void play () override { std::cout << "Playing a drum\n"; }
void play_rimshot () { std::cout << "Playing a rimshot\n"; }
void play_side_stick () { std::cout << "Playing a sidestick\n"; }
};
The play_rimshot and play_sidestick methods are too specialised to be placed in the instrument class. The instrument class exists to provide a common interface to all instruments but you cannot play a rimshot or sidestick upon a guitar. If these methods were part of the instrument interface then they'd also be part of the guitar interface.
One way to invoke these specific behaviours is to dynamically cast the instrument to a drum. If the cast succeeds, you can definitely invoke these methods. If not, then you cannot. But runtime type information is expensive and is often the result of poor design. It is not that there's anything wrong with having such specialised methods, it is only that accessing them from a generic interface is generally the wrong way to go about invoking them. In this particular case all we really need to do is derive a new class from the drum class to cater for these more specific methods:
struct rimshot : drum
{
void play() override { "Playing a rimshot\n"; }
};
struct sidestick : drum
{
void play() override { "Playing a sidestick\n"; }
};
Even though these classes did not exist when we first wrote our foo function, the function can still cater for them:
rimshot r;
sidestick s;
foo (r, s);
In other words, virtual functions have not only enabled runtime polymorphism without the need for runtime type information, they have also provided some measure of future-proofing our code; we do not need to continually maintain the foo function in order to cater for new behaviours.
What is multilevel inheritance in C plus plus?
Multi-level inheritance involves at least 3 classes, a, b and c, such that a is derived from b, and b is derived from c. c is therefore the least-derived base class, b is an intermediate base class and a is the most-derived class.
What is the difference between base class and derived class in c plus plus?
There is no difference other than that a derived class inherits from a base class. That is, the derived class inherits all the public and protected members of its base class, and is a more specialised form of its base class. Thus both can be treated as if they really were base classes. The derived class can also override the virtual methods of the base class, thus allowing polymorphic behaviour without the need to know the exact type of the derived class.
#include<iostream>
#include<vector>
#include<cassert>
using namespace std;
// Returns a vector of Fibonacci numbers from start to max.
// Sequence A000045 in OEIS if start is 0.
vector<unsigned> fibonacci (const unsigned start, const unsigned max)
{
// Invariants:
if (1<start)
throw std::range_error
("vector<unsigned> fibonacci (const unsigned start, const unsigned max): start < 1");
if (max<start)
throw std::range_error
("vector<unsigned> fibonacci (const unsigned start, const unsigned max): max < start");
// Empty set...
vector<unsigned> fib {};
if (max)
{
// First term...
fib.push_back (start);
if (1<max)
{
// Second term...
fib.push_back (1);
// All remaining terms...
unsigned next = 0;
while ((next = fib.back()+fib[fib.size()-2]) <= max)
fib.push_back (next);
}
}
return fib;
};
// Return true if the given number is prime.
bool is_prime (const unsigned num)
{
if (num<2) return false;
if (!(num%2)) return num==2;
for (unsigned div=3; div<=sqrt(num); div+=2)
if (!(num%div)) return false;
return true;
}
// Displays all prime Fibonacci numbers in range [1:10,000].
int main()
{
const unsigned max=10000;
vector<unsigned> f = fibonacci (1, max);
cout << "Prime Fibonacci numbers in range [1:" << max << "]\n";
for (auto n : f)
if (is_prime (n))
cout << n << ", ";
cout << "\b\b " << endl; // backspace and overwrite trailing comma
}
What are different ways by which you can access public member functions of an object?
You simply access it. That's what public means. You can access a public member from any other class, or even from non class code, so long as the referenced class is in scope to the referencing code. In order to do that, you need to have a reference (directly or indirectly) to the instance of the class you want to reference. This would generally be passed as an argument to the referencing code.
How-to structure in c plus plus?
A structure in C++ is nearly the same as a structure in C. struct abc {
int item1;
int item2;
... etc...
} mystructure; There are several differences. Two examples are... In C++, you get automatic typedef'ing, so that (in this example) you would not have to say struct abc mysecondstructure; - you can just say abc mysecondstructure;. In C++, structures are very similar to classes. In fact, structures are classes, which is why you get automatic typedef'ing.
... double squareOf_Number(double Number)
{
return (Number*Number);
}
...
int main()
{
...
double Number = 0;
...
printf("Enter a number: ");
cin >> Number;
...
printf("Square of %f is %f\n", Number, squareOf_Number(Number));
...
}
Or you can include #include <math.h> and use the function pow(double a, double b) which returns a^b.
What is the difference between c plus plus and object code?
Short answer: They're the same.
Due to technical limitations, we on WikiAnswers cannot write C++ in the question field. So we must write "C plus plus." Unfortunately, we're also lazy. So we write "cpp" as an abbreviation.
Write a programm for addition of two numbers in C plus plus language?
Addition is an intrinsic operator that applies to all primitive data types, both signed and unsigned. There is no need to program it. The operands can be literal, constant or variable, hard-wired or generated from user-input, it makes no difference whatsoever.
#include<iostream>
int main()
{
std::cout<<"The sum of 1 and 2 is "<<(1+2)<<std::endl;
std::cout<<"The sum of 1.5 and 2.3 is "<<(1.5, 2.3)<<std::endl;
return(0);
}
C is not an object-oriented programming language and therefore has no concept of abstract classes.
In C++, however, an abstract class is a base class that declares one or more pure-virtual functions. An abstract base class is also known as an abstract data type (ADT). Pure-virtual functions differ from virtual functions in that virtual functions are expected to be overridden (but needn't be) while a pure-virtual function must be overridden (but needn't be implemented by the ADT). Once overridden by a derived class, the function reverts to being a virtual function with respect to further derivatives. However, only classes that provide or inherit a complete implementation of the pure-virtual interface can be instantiated in their own right; those that do not are themselves abstract data types.
What is the difference between abstract class and static class?
An ADT is simply a base class that has one or more pure-virtual methods. A pure-virtual method is similar to a virtual method except that the ADT need not provide any implementation for that method. Even if it does provide a generic implementation, all derivatives must override that method, even if only to call the generic base class method. This ensures that all derivatives provide their own specialised implementations. A derivative that does not provide an implementation for all the pure-virtual methods it inherits becomes an ADT itself. However any implementations that it does provide can subsequently be inherited by its derivatives (those methods effectively become virtual methods rather than a pure-virtual method). Only classes that provide or inherit a complete implementation of all pure-virtual methods can physically be instantiated.
Going back to our shape class, the shape::draw() method is an ideal candidate for a pure-virtual function since it is impossible to draw a shape without knowing what type of shape it is. However, a circle class would be expected to know how to draw itself thus it can provide the specific implementation for drawing circles. Similarly, rectangles and triangles will provide their own specific implementations. This then makes it possible to store a collection of generic shapes and have them draw themselves without the need to know their actual type; as with all virtual methods, the derivatives behave according to their actual type.
Note that static classes have no need for any constructors, operators or destructors (they have no this pointer since there can never be any instances of the class), thus the compiler-generated default constructor, copy constructor, destructor and assignment operator must all be declared private. Some languages may do this automatically but in C++ (which has no concept of static classes) you must explicitly declare them as such.
While static classes do have their uses, bear in mind that the point of having a class in the first place is in order to instantiate independent objects from the class. Thus the only time you should ever use a static class is when the class data members must exist for the entire duration of the program and you want to ensure there can only ever be one instance of those members. However, for memory efficiency alone, a singleton class would be the preferred method. Moreover, if some or all of the static methods are closely associated with a generic class, then it makes more sense to encapsulate those methods as static members of that generic class rather than as a completely separate static class.
Write a c plus plus program that multiplies two matrices?
The previous two versions of the program given here were unnecessarily long and complicated. I am providing a much more simple and succinct version. I have constructed it on the basis of two 3*3 matrices. However you can easily change the order by replacing the "3"s of the program with your required order, provided the number of columns of the premultiplier is equal to the number of rows of the postmultiplier. If you want to dynamically allocate the orders of the matrices, you will have to use calloc and malloc, which I don't know as of yet!
#include
int main()
{
int i,j,l;
int matrix[3][3],matri[3][3],matr[3][3]={{0,0,0},{0,0,0},{0,0,0}};
printf("Enter 1st matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf_s("%d",&matrix[i][j]);
}
printf("\n\n");
printf("Enter 2nd matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf_s("%d",&matri[i][j]);
}
for (j=0;j<3;++j)
{
for (i=0;i<3;++i)
{
for (l=0;l<3;++l)
matr[i][j] = matr[i][j] + (matrix[i][l])*(matri[l][j]);
}
}
printf("The resultant matrix is:\n\n");
for (i=0;i<3;++i)
{
for (j=0;j<3;++j)
printf("%4d",matr[i][j]);
printf("\n\n");
}
return( 0 );
}
Recent changes to the above code
30/4/12:
Error: main() must return a value.
Error: matr must be initialised in declaration, not afterwards.
Warning: Variable k is unreferenced (removed).
Warning: Use of scanf is unsafe. Replaced with scanf_s
Benign: Commented code removed for clarity.
An alternate version with dynamic arrays is shown below.
#include
typedef unsigned int UINT;
// Forward declarations.
int ** CreateMatrix( UINT rows, UINT cols );
void InputMatrix( int ** Matrix, UINT rows, UINT cols, char * Title );
void PrintMatrix( int ** Matrix, UINT rows, UINT cols, char * Title );
void ReleaseMatrix( int ** Matrix, UINT rows );
int main()
{
// Initialise some variables.
UINT rows=0, comm=0, cols=0;
UINT i=0, j=0, k=0;
// 2D arrays (pointer-to-pointer-to-int).
int ** M1 = NULL;
int ** M2 = NULL;
int ** M3 = NULL;
printf("Calculate the dot products of two matrices.\n\n");
printf( "Enter the number of rows in the 1st matrix:\t");
scanf_s("%u", &rows );
printf( "Enter the number of common elements:\t\t");
scanf_s("%u", &comm );
printf( "Enter the number of columns in the 2nd matrix:\t");
scanf_s("%u", &cols );
// Allocate the 2D arrays:
M1 = CreateMatrix( rows, comm );
M2 = CreateMatrix( comm, cols );
M3 = CreateMatrix( rows, cols );
// Input values:
InputMatrix( M1, rows, comm, "Matrix 1" );
InputMatrix( M2, comm, cols, "Matrix 2" );
// Calculate the dot product.
for( i=0; i
for( j=0; j
for( k=0; k
M3[i][k] += M1[i][j] * M2[j][k];
// Report.
PrintMatrix( M1, rows, comm, "\nMatrix 1" );
PrintMatrix( M2, comm, cols, "Matrix 2" );
PrintMatrix( M3, rows, cols, "Dot Product of Matrix 1 and Matrix 2" );
printf("\n");
// Release memory.
ReleaseMatrix( M1, rows );
ReleaseMatrix( M2, comm );
ReleaseMatrix( M3, rows );
return( 0 );
}
int ** CreateMatrix( UINT rows, UINT cols )
{
UINT i = 0;
int ** Matrix = (int **) malloc(rows * sizeof( int * ));
for( i=0; i
{
Matrix[i] = (int *) malloc( cols * sizeof( int ));
memset( Matrix[i], 0, cols * sizeof( int ));
}
return( Matrix );
}
void InputMatrix( int ** Matrix, UINT rows, UINT cols, char * Title )
{
printf( "\nValues for %s:\n\n", Title );
UINT i=0, j=0;;
for( i=0; i
{
for (j=0; j
{
printf("Enter value for row %u, col %u:\t", i+1, j+1);
scanf_s("%d", &Matrix[i][j]);
}
}
}
void PrintMatrix( int ** Matrix, UINT rows, UINT cols, char * Title )
{
printf( "\n%s:\n\n", Title );
UINT i=0, j=0;
for( i=0; i
{
for (j=0; j
printf( "%4d", Matrix[i][j] );
printf("\n");
}
}
void ReleaseMatrix( int ** Matrix, UINT rows )
{
UINT i = rows;
while(i)
free( Matrix[--i] );
free( Matrix );
}
Write a program in c to sort an unsorted array using selection sort?
#include<stdio.h> #include<conio.h> int main() { char str[100],temp; int i,j; clrscr(); printf("Enter the string :"); gets(str); printf("%s in ascending order is -> ",str); for(i=0;str[i];i++) { for(j=i+1;str[j];j++) { if(str[j]<str[i]) { temp=str[j]; str[j]=str[i]; str[i]=temp; } } } printf("%s\n",str); getch(); return 0; } Output: Enter the string : syntax syntax in ascending order is -> anstxy
Why is c plus plus a super set of c language?
C++ is a superset of C (rather than a subset of C) because it inherits directly from C and adds object-oriented programming principals to the C language. If it were a subset of C then it would reduce the language, not enhance it. Indeed, the original C++ compiler simply translated the C++ source code into C source code which was then compiled by the C compiler. Modern C++ compilers do the same sort of thing but they do it far more efficiently, without the need to produce any intermediate C source code.