What is the difference between a repeat until and a while do loop?
Repeat until loops run until a condition is true. They repeat at the end of the loop and they will always run at least once.
While do loops will only run while a condition is true. They may not run at all.
Cpp program for ascending order using bubble sort?
The most optimal form is as follows:
void bubble_sort( int* arr, size_t len )
{
do{
int n = 0; // used to record the last swap position
for( int i=1; i<len; ++i )
{
if( arr[i-1]>arr[i] )
{
// swap without a temporary...
arr[i-1]^=arr[i]^=arr[i-1]^=arr[i];
n = i;
}
}
len = n;
} while( len );
}
Note that while this form is the most optimal, nobody ever uses bubble sort in the real world, even for small sets of data (which is all it can really handle). Indeed, it has very little value even in the classroom. Its only true value is that it is an example of how not to write an algorithm. Insertion sort is a much better algorithm and is every bit as efficient, just as simple to understand and, paradoxically, much simpler to implement. It can even be used to sort smaller subsets of a much larger subset when combined with the more complex quicksort, heap sort or merge sort.
What is static variable in the class for c plus plus?
Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.
Is c plus plus language case sensitive?
Because when have varialbles var1 and Var1, they are actually different variables. The same ides with operators, data and so on .
How do you make a c plus plus program loop?
#include
using std::cout;
int main()
{
int a = 0;
while (a != 11) //While a does not equal 11
{
a++; //add 1 to a
cout << a << "\n"; //print a and add newline
}
cout<<"The loop has ended";
return 0;
}
What is the c program to find the octal equivalent of an integer?
void Decimal_to_Octal()
{
int n,r[10],i;
cout<<"Enter a number to find it's octal equivalent";
cin>>n;
cout<<"The octal equivalent of "<
{
r[i]=n%8;
n=n/8;
}
i--;
for(;i>=0;i--)
cout<
What is meant by abstraction in c plus plus?
Abstraction is a process by which higher concepts are derived from the usage and classification of literal ("real" or "concrete") concepts, first principles and/or other abstractions.
What are graphic chars in C plus plus?
Graphic chars have nothing whatsoever to do with C++. Graphic chars are better known as ASCII box-drawing characters. They first appeared in the extended ASCII character set of the original IBM PC (now referred to as code page 437), long before C++ was conceived. The extended ASCII character set varies according to which code page is currently active. UNICODE contains the full range of 128 box-drawing characters (from U+2500 to U+257F, inclusive) including all variants of the single-line, double-line and single/double hybrid characters, as well us quarter-arcs (for rounded corners) and diagonals. The extended ASCII character sets contain a small subset of the UNICODE range. Code page 437 provides the largest subset with 40 characters and is the preferred code page for all ASCII box-drawing. Code page 850 reduces the subset to just 22 by eliminating the hybrid characters and should generally be avoided. If you must use box-drawing characters, prefer UNICODE or UTF encodings over the ASCII encodings. They are typically only required within plain-text files or when printing such files to a console/terminal.
Note that the entire C++ programming language is represented using only the standard 7-bit ASCII character set (characters 0x00 through 0x7F) as specified in ISO-8559-1. The extended ASCII character sets play no part in any C++ standard (the high-order bit in all 8-bit encodings is always zero). Although you may include literal box-drawing characters within C-style strings and all std::string types, it's best to use UNICODE or UTF encodings because the literal characters cannot be interpreted consistently across all systems.
Is it possible to immediately run a program without compiling it?
Not in C++, unless you have an interpreter. Most C++ development systems, however, are based on the compiler idiom, so, the answer is no.
However, Java is very close to C++, except for the platform and libraries, so one might consider Java to be an interpreter, however, you still must "compile" Java to byte code, so the answer is still no.
Write a c program that interchanges the odd and even elements of an array?
#include<stdio.h>
int main()
{
int num[]={1,2,3,4,5,6};
int i,temp;
for(i=0;i<=5;i=i+2)
{
temp=num[i];
num[i]=num[i+1];
num[i+1]=temp;
}
for(i=0;i<=5;i=i+2)
printf(''%d",num[i]);
return 0;
}
What does a plus b plus c plus f equals?
That's going to depend on the values of 'a', 'b', 'c', and 'f'.
I think you mean operation overlord??? It is the American, Canadian and British offensive on Europe in World War 2. They landed in Normandy on 6th June 1944 (Commonly called D-Day, Day of Days or Deliverance Day) and progressed throughout France liberating Paris on the 25th August. This allowed the allies a foothold in Europe.
Yes, D requires constructors, because all classes require constructors. However, whether you need to actually define them or not depends on whether B has any constructors other than a default constructor and a copy constructor. To understand why all classes must have constructors you need to understand how the compiler-generated constructors work.
All classes must have at least two constructors, one of which must be a copy constructor. If you do not define any constructors at all, the compiler automatically generates a default constructor (with no parameters) and a copy constructor. If you define any constructor, the compiler only generates the copy constructor unless you define your own. The compiler also generates an assignment operator and a destructor unless you provide your own.
In many cases the compiler-generated constructors will be sufficient for your needs thus there is no need to define your own in these cases. However, if your class contains one or more pointers to memory that is exclusive to each instance of a class (not shared amongst them), then you must provide your own constructors, as well as an assignment operator and a destructor, in order to manage that memory correctly. This is because the compiler-generated versions will treat all pointers as being pointers to shared memory and will simply copy their addresses, not what they contain. You must override this behaviour and ensure that those pointers are deep-copied; copying the memory itself, not just the address.
Constructors exist in order to initialise member variables. Even if your class has no member variables and therefore no real need for constructors, the compiler-generated constructors will still exist, as will the assignment operator and destructor, and it is important that you fully understand how these constructors work so that you will know exactly when you need to define your own and when you do not.
Consider the following example:
#include <iostream>
class B {
public:
B():m_data(0){ std::cout << "B::B()" << std::endl; }
B(const B& b):m_data(b.m_data){ std::cout << "B::B(const B&)" << std::endl; }
B& operator= (const B& b){ m_data = b.m_data; std::cout << "B::operator= (const B&)" << std::endl; return( *this ); }
virtual ~B(){ std::cout << "B::~B()" << std::endl; }
void SetData(const int i){m_data=i; std::cout << "B::m_data=" << m_data << std::endl; }
int GetData(){return m_data;}
private:
int m_data;
};
class D: public B {}; // no members or constructors defined.
int main()
{
D d1; // default constructor
std::cout << "d1.GetData()==" << d1.GetData() << std::endl;
d1.SetData( 100 );
std::cout << "d1.GetData()==" << d1.GetData() << std::endl;
D d2(d1); // copy constructor -- same as: D d2 = *d;
std::cout << "d2.GetData()==" << d2.GetData() << std::endl;
d1.SetData( 200 );
std::cout << "d1.GetData()==" << d1.GetData() << std::endl;
std::cout << "d2.GetData()==" << d2.GetData() << std::endl;
d2 = d1; // assignment operator
std::cout << "d2.GetData()==" << d2.GetData() << std::endl;
std::cout << std::endl;
return(0);
} // All objects fall from scope here.
Output:
B::B()
d1.GetData()==0
B::m_data=100
d1.GetData()==100
B::B(const B&)
d2.GetData()==100
B::m_data=200
d1.GetData()==200
d2.GetData()==100
B::operator= (const B&)
d2.GetData()==200
B::~B()
B::~B()
Since we provided no constructors in D it is difficult to see exactly what is going on here. In the main function we initially declare d1 to be of type D, but the first line of output is B::B(). Clearly the base class constructor is being invoked but it is not clear exactly how it was invoked. You'd be forgiven for thinking that since D inherits from B then it must inherit B's default constructor, but you cannot inherit constructors any more than you can inherit friends, operators or destructors from a base class. They are part of the interface but they are not part of the inheritable interface.
The answer, of course, lies in the fact the compiler has generated constructors for us behind the scenes. But since we don't have access to them we cannot trace them. So let's just clarify exactly what's going on by explicitly declaring constructors exactly as the compiler would have generated them for us. Let's also include some trace code so that we can see how they relate to the existing traces:
class D: public B {
public:
D():B(){ std::cout << "D::D()" << std::endl; }
D(const D& d):B(d){ std::cout << "D::D(const D&)" << std::endl; }
D& operator= (const D& d){ B::operator=( d ); std::cout << "D::operator= (const D&)" << std::endl; return( *this ); }
~D(){ std::cout << "D::~D()" << std::endl; }
};
Output:
B::B()
D::D()
d1.GetData()==0
B::m_data=100
d1.GetData()==100
B::B(const B&)
D::D(const D&)
d2.GetData()==100
B::m_data=200
d1.GetData()==200
d2.GetData()==100
B::operator= (const B&)
D::operator= (const D&)
d2.GetData()==200
D::~D()
B::~B()
D::~D()
B::~B()
Now things are somewhat clearer. Although B::B() looks like it was invoked before D::D(), they were actually invoked the other way around. Here's the default constructor for D:
D():B(){ std::cout << "D::D()" << std::endl; }
Note that before we enter the construction body (which contains the trace) we explicitly call the default constructor of B, hence it appears to be invoked first. However, this is, in fact, the correct order of events since we cannot construct an instance of D unless we first construct an instance of B. Only when B has been fully constructed can we then construct D.
Note also that even if we'd omitted the call to B() in the initialisation list, it would have been invoked automatically for us. That is, if we do not specify a base class constructor, the default constructor is invoked automatically. This fact is vital when it comes to declaring copy constructors:
D(const D& d):B(d){ std::cout << "D::D(const D&)" << std::endl; }
Here, we've called the base class copy constructor from the initialisation list: B(d). We have to do this because if we omit it, we'll end up calling the default constructor instead, which is not what we expect of a copy constructor. Here's the output trace snippet we'd have encountered had we omitted it:
d1.GetData()==100
B::B()
D::D(const D&)
d2.GetData()==0
Clearly, d2 cannot be a copy of d1 if their base class members differ, hence we must explicitly call the base class copy constructor (just as the compiler-generated copy constructor did).
Note that the assignment operator is not a constructor (it applies to existing objects only) and therefore does not have an initialisation list. However, the function body invokes the base class method, just as the compiler-generated version did.
The remainder of the output is fairly self-explanatory but note that destruction occurs in the reverse order of construction, thus D must be destroyed before B can be destroyed. Note also that since D is derived from B, B's constructor must be declared virtual. This is required because if we held a pointer to a derived class' base class and subsequently destroyed it, we would rightly expect the derived class' constructor to be invoked first. But if B's constructor were not virtual, we would end up destroying B alone, leaving an invalid instance of D in memory with no way to destroy it (a memory leak).
Explain a pure virtual function with an example?
There are no pure virtual functions, but there are pure virtual function calls.
In C++, classes can define abstract functions. Those are virtual functions for which no implementation is supplied in the class that defines it. Abstract functions are typically used to define a contract. For example, a base class for any vehicle might guarantee that a move() method exists, but only classes derived from vehicle implement this method according to the properties of the specific vehicle: a bike, a car, a skateboard, etc. Classes containing abstract functions are sometimes called abstract classes.
The C++ compiler does not allow the creation of an object of type vehicle in this example, because the vehicle class contains an abstract function.
Classes derived from vehicle (derived in first or higher degree) must supply an implementation of this method before the compiler allows the creation of an object of this type.
Therefore, any valid object descended from vehicle must have an implementation of the move method. However, since it is possible to create pointers to abstract classes (classes that contain abstract functions which have not yet been given an implementation), pure virtual function calls can occur at runtime.
Consider this example:
class vehicle {
public:
virtual void move(void) = 0; // abstract function
};
class motorizedVehicle : vehicle {
...
};
class car : motorizedVehicle {
public:
virtual void move(void) {
... // implementation
}
};
An application cannot create objects of type vehicle or motorizedVehicle, but it can create pointers to those. Those might give access to classes that have no implementation for the move method, and calling it results in a run-time diagnostic known as a pure virtual function call.
How inheritance helps in building class hierarchies in c plus plus?
Inheritance allows you to derive new objects from existing objects, thus allowing you to re-use the existing code (known as the generic code), augmenting or overriding that existing code in order to provide more specialised functionality (the specialised code).
For instance, if you have a Rectangle class, you might choose to derive a Square class from the Rectangle class. Thus the Square class inherits all the properties of a Rectangle (such as 4 vertices and 4 edges meeting at 90 degrees), but specialises the class to ensure that all four edges are always equal length. In other words, a Square is a more-specialised form of Rectangle, and therefore need only cater for the properties that are unique to Squares, whilst inheriting all the properties that are common to both Squares and Rectangles.
This is not unlike the real-world where you will often adapt an existing object to suit a new purpose, rather than build a new object completely from scratch, thus saving you a lot of time and money. In other words, we don't spend our time continually re-inventing wheels, we simply use an existing wheel and adapt it to suit our needs. The more closely the existing wheel resembles the wheel we actually want, the less work we have to do to adapt it.
when your PC has been inoperation for a while the components inside start to heat up this heat can cause damage to those components a heat sink turns the heat into air and vents it out of the PC making sure your compnents eg processors are still in working order
An abstract class is a class that cannot be directly instantiated. The purpose of such a class is to put some logic in a base class and force derived classes to implement the remaining functionality. Since the full functionality is only available in the derived class, the base class is declared as abstract so that it cannot be instantiated directly.
How many keywords in c plus plus now?
63 asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while
How does friend operator function differ from member operator function?
In C++, we know that private members cannot be accessed from the outside class.
That is a non-member function cannot have an access to the private data of a class.
However there could be a situation where we would like two classes to share a particular function.
For example consider a case where two classes, manager and scientist have been defined.
We would like to use a function income_ tax () to operate on the objects of both these classes.
In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.
To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:
Class ABC
{
……
…..
Public:
……..
……..
Friend void xyz (void); //declaration
};
When the function is logically coupled with the class (like your maze connectedness example)
When the function needs to access private or protected members, it's better to make it a member than a friend. when it's a generic function that can be templatized to naturally work on other classes (look at the
C plus plus programme to compare strings?
#include<iostream>
#include<string>
int compare (const std::string& a, const std::string& b)
{
int n=1;
std::string::const_iterator ai=a.begin(), bi=b.begin();
for (int n=1; ai!=a.end() && bi!=b.end(); ++ai, ++bi, ++n)
{
char ca = *ai;
char cb = *bi;
if (ca<cb) return n * (-1);
if (cb<ca) return n;
}
if (ai==a.end() && bi==b.end()) return 0;
if (ai==a.end()) return n * (-1);
return n;
}
int main()
{
std::cout
<< "When comparing strings, zero indicates the two strings are equal.\n"
<< "A negative value indicates the first string is less than the second string.\n"
<< "A positive value indicates the first string is greater than the second string.\n"
<< "The absolute value indicates the characters that differ in the strings. Thus\n"
<< "-10 or 10 indicate that the 10th characters show a difference.\n"
<< std::endl;
std::string x = "This is a string.";
std::string y = "This is another string.";
std::string z = "This is another.";
std::cout << "String x = "" << x << """ << std::endl;
std::cout << "String y = "" << y << """ << std::endl;
std::cout << "String z = "" << z << """ << std::endl;
std::cout << std::endl;
std::cout << "compare(x,x) = " << compare(x,x) << std::endl;
std::cout << "compare(x,y) = " << compare(x,y) << std::endl;
std::cout << "compare(x,z) = " << compare(x,z) << std::endl;
std::cout << "compare(y,x) = " << compare(y,x) << std::endl;
std::cout << "compare(y,y) = " << compare(y,y) << std::endl;
std::cout << "compare(y,z) = " << compare(y,z) << std::endl;
std::cout << "compare(z,x) = " << compare(z,x) << std::endl;
std::cout << "compare(z,y) = " << compare(z,y) << std::endl;
std::cout << "compare(z,z) = " << compare(z,z) << std::endl;
std::cout << std::endl;
}
Write a programme that calculates length and width of rectangle in c plus plus?
Commented code for a win32 console application:
#include //Include stdio.h for console input/output funcs
#include //Include conio.h for getch function (lol)
void main( )
{
int nWidth, nHeight; //Variables to hold width and height of rectangle
while( 1 ) //Infinite loop - can only be exited using "break;" (among others)
{
printf( "Enter rectangle width: " ); //Print text...
scanf( "%d", nWidth ); //Scan for user input
fflush( stdin ); //Flush user input
printf( "Enter rectangle height: " ); //Print text...
scanf( "%d", nHeight ); //Scan for user input
fflush( stdin ); //Flush user input
printf( "Area of rectangle with width of %d and height of %d is %d\n", nWidth, nHeight, nWidth*nHeight ); //Print text...
fflush( stdout ); //Flush output
//Now program will jump back to start of loop
}
}
Differentiate between overloading unary operator and overloading binary operator?
Unary operators declared as member function take NO arguments; if declared as global function, then take only one argument.
Binary operators declared as member functions take one argument; if declared as global function, then would take two arguments.
Note: The first argument for member function overloaded is always the class type; the class in which the operator is declared.
Is c plus plus is pure object oriented?
No. C is not object-oriented, it is a procedural language.
C++, while object-oriented, is not purelyobject-oriented. One of the requirements for a pure object-oriented language is that everything is an object. C++ still has primitive data types (int, long, double, etc.), and so is not purely object-oriented.