Difference between function overloading and default arguments?
A default constructor is one where no arguments are declared or required. Thus if all arguments have defaults then it is still a default constructor, but one that can also serve as an overloaded constructor.
Consider the following which has two constructors, one with no arguments (the default) and one with two arguments (an overloaded constructor):
struct A
{
A () : x (42), y (3.14) {}
A (const int a, const double b) : x (a), y (b) {} int x;
double y;
// ...
};
We can invoke these two constructors as follows:
A a; // invokes default constructor (a.x is 42, a.y is 3.14).
A b (0, 1.0); // invokes overloaded constructor (b.x is 0, b.y is 1.0).
Since both constructors are essentially doing the same thing, they can be combined into a single constructor -- we simply make the 'magic numbers' the default values of the overloaded constructor:
struct A
{
A (const int a=42, const double b=3.14): x (a), y (b) {}
int x;
double y;
// ...
};
A a; // a.x is 42, a.y is 3.14.
A b (0, 1.0); // b.x is 0, b.y is 1.0.
As far as the calling code is concerned, nothing has changed, but the class declaration is simplified by removing a redundant constructor.
What is the logical abstrect base class for a class called CricketPlayer?
A logical abstract base class for a class called CricketPlayer could be Batsman class or Bowlerclass.
void * (If you used your help/manual system, you would get an answer much sooner.)
What is difference between C and C plus plus programming?
THE DIFFERENCE BETWEEN c&c++ IS JST THAT c++ IS MODIFIED LANGUAGE AND IT IS A HOGH LEVEL LANGUAGE AS COMPARED c IS A LOW LEVEL LANGUAGE
C++ was developed as a better 'C' language. In addition, C is a procedural language only, and C++ has both procedural language features and object oriented features.
Both are considered high level languages.
C++ is an addition to C. C only allows you to write programs as a list of instructions (procedure), while C++ allows you to write separate objects, and link them together to produce a piece of software.
What is the need of parameterized constructor?
Parametrized constructors are used to increase the flexibility of a class, allowing objects to be instantiated and initialised in more ways than is provided by the default and copy constructors alone. If you define any parametrized constructor, including a copy constructor, you will lose the default constructor generated by the compiler and must declare your own if you need one. The default constructor can also be parametrized, but each parameter must include a default value in the declaration, so that it can be called without any parameters.
What are the disadvantages of function in C?
In comparison to Strawberry Cheesecake, the main disadvantage of a function in C is that you can't eat it.
In comparison to a mature Brandy, the main disadvantage of a function in C is that you can neither smell nor drink it.
In comparison to functions in some, but not many, other programming languages, one limitation of functions in C is that they may not be nested: In C, a function definition cannot contain another function definition.
In comparison to some modern interpreted language, e.g. Python, one limitation of functions in C is that a function cannot generate another function.
Difference between vector and list in c plus plus?
Although they share many of the same features, there are many differences. For instance, a list does not have an index operator [] while a vector does not have a merge method. If in doubt, simply look at the variable's declaration -- it will explicitly state whether the variable is a list or a vector (or indeed some other STL container), along with the type of data that it contains. Ultimately a vector is just an array, ideally suited to random access, whereas a list is ideally suited to sequential access.
What is 10 plus 9 plus 7 plus 6 plus 6 plus 6 plus 5 equals?
It's a simple example of addition.
The solution looks like this:
10 + 9 + 7 + 6 + 6 + 6 + 5 = 49
What is the repetitive control structure in c plus plus?
C provides two sytles of flow control:
Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching: Branching is so called because the program chooses to follow one branch or another.
if statementThis is the most simple form of the branching statements.It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
? : OperatorThe ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions.? : is a ternary operator in that it takes three values, this is the only ternary operator C has.
switch statement:The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read. Using break keyword:If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword.Try out given example Show Example
What is default condition:If none of the listed conditions is met then default condition executed. Looping Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way. while loopThe most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statements get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false. for loopfor loop is similar to while, it's just written differently. for statements are often used to proccess lists such a range of numbers: do...while loopdo ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once. break and continue statements C provides two commands to control how we loop:What is system defined default constructor in java?
System defined constructor or Default constructor is the constructor that the JVM would place in every java class irrespective of whether we code it manually or not. This is to ensure that we do not have compile time issues or instantiation issues even if we miss declaring/coding the constructor specifically. Ex: public class Test { public String getName() { return "Rocky"l } Public static void main(String[] args){ Test obj = new Test(); String name = obj.getName(); } } Here we were able to instantiate the class Test even though we did not declare a no argument constructor. This is the default constructor that gets called when we try to instantiate it.
What are the advantages of Turbo C plus plus?
The only reason to use Turbo C++ as opposed to, say, Visual C++ is because you chose to use a Borland IDE rather than a Microsoft IDE. If the question is why we use C++ rather than a specific IDE, then it is because we want high performance without all the the complexity of a low-level symbolic language such as assembler. If performance were not an issue, we would choose to use a more abstract, high-level language such as Java instead, which is ideally suited to rapid application development, but unsuitable for high-performance applications.
What is global object in c plus plus?
A global object is any object instantiated in the global namespace. The global namespace is anonymous, so if we don't explicitly specify a namespace prior to instantiating an object, that object will be instantiated in the global namespace:
int x; // global
namespace n {
int x; // non-global
};
To refer to the non-global, we must use namespace resolution:
x = 42; // assign to the global
n::x = 42; // assign to the non-global
Is the default access specifier same as protected?
A private member of a class can only be accessed by methods of that class.
A protected member of a class can only be accessed by methods of that class and by methods of a derived class of that class.
A array within a class in C++ is no different than any other type of attribute in a class...
class myclass {
private:
int someInteger;
int somArray[10];
int *somePointer;
public:
myclass() { ... constructor ...}
myclass(const &myclass()) { ... copy constructor ...}
~myclass() { ... destructor ...}
mymethods() { ... methods, etc. ...}
private:
myhelpers() {... helpers, etc. ...}
}
Recall that arrays and pointers are tightly related. Well, in classes, also recall that their memory must be explicitly handled or you are going to have all sorts of problems. You need constructors and destructors, as well as copy constructors, to properly make deep copies and deallocations if arrays area in the form of pointers.
What are the differences between thread and function in C plus plus?
As people do computer has head to think and hands to work. Under the control of it's head(CPU), you can consider threads as its hands. A computer has more than one hand, every hand would do one thing independently but have to wait its order. Function, as it demonstrate by the name, means what task a software can conduct.
What is the abstract feature in c plus plus?
Abstraction generally refers to abstract classes. An abstract class is one that cannot itself be instantiated, but can be derived from. Abstract classes are conceptual classes rather than concrete classes, and are intended to provide a common interface to the concrete classes derived from them. A class becomes abstract when it contains one or more pure-virtual methods, which must be implemented in the derived classes otherwise they become abstract classes themselves. A concrete class is one that provides a complete implementation for is abstract base classes, or inherits implementations from base classes other than those that originally declared the function to be pure-virtual.
An example of an abstract class is Shape. All shapes can be drawn, therefore Shape will have a Draw() method. However, it cannot implement this method unless it knows what type of shape it actually is. Therefore the Draw() method should be declared pure-virtual, thus rendering the Shape class to be abstract. You can then derive concrete shapes from the Shape class, such as Circle and Square classes, which will each provide the actual implementation for the Draw() method. Since Square and Circle are types of Shape, you can create collections of Shape objects and call their Draw() methods without regard to their actual type. If the object is really a Square, then calling Shape.Draw() will actually invoke Square.Draw(). There is no need to determine the actual type of the shape because every shape has a common interface.
This is really no different to overriding virtual methods in non-abstract classes. The main difference is that one would not be expected to instantiate a Shape (because it is abstract), other than through derivation, and the pure-virtual methods MUST be implemented by the derived classes, whereas normal virtual methods are simply expected to be overridden but it is not a requirement to do so unless you require an enhancement to the base class method.
Abstract classes typically have few if any member variables and little or no implementation. Ultimately, the class exists purely to provide a common interface to all its derivatives, and only functionality and member variables that are common to all its derivatives should be placed within it. Even if the abstract class provides a complete implementation for all its pure-virtual methods, you cannot instantiate an abstract class -- it must be derived from -- and all pure-virtual methods must still be implemented in the derivatives, even if only to call the base class methods explicitly.
Making multiplication table in c plus plus using do while loop?
Um, not sure how to do that, but you can create a sort of "table" in C++ by using multidimensional arrays. Below is an example of how to create a two-dimensional array:
int myArray[10] [10];
You can add more dimensions to the array in order to increase its storage capacity.
Difference between array and pointers in c plus plus?
A pointer is simply a variable that can store a memory address and has the same purpose in both languages. The only real difference is that C++ pointers can point at objects (instances of a class) and indirectly invoke their methods, whereas pointers in C (which is not object oriented) cannot.
How reliable is visual c plus plus?
That depends on what you mean by reliable. If you mean does it conform to the C++ standard then no, it is not reliable. Look up the 'Microsoft specific' headings under each topic in the help file. In most cases these are enhancements to the standard, but there are a few diversions from the standard you need to watch out for, particularly if you intend to port your code to another C++ implementation.
If you mean can it be used to write reliable programs then yes, it can. However, reliability in this sense is mostly your own responsibility as programmer. The code provided by Microsoft is, for the most-part, reliable. Microsoft use the same code in-house so if a problem can be replicated then a hotfix or workaround is usually not far behind.
There are three ways to sort a two-dimensional array. We can sort each row so that the rows remain where they are but the elements within each row are sorted. Or we can sort the rows themselves so the elements within each row remain where they are but the rows are sorted. Or we can do both, sorting the elements in each row and then sorting the rows. The following program demonstrates all three methods using the same bubble-sort method (see the sort function near the top of the code).
Keep in mind that a two-dimensional array is nothing more than a one-dimensional array of one-dimensional arrays. That is, array A[2][3] is a one-dimensional array of 2 elements where each element is a one-dimensional array of 3 elements. In other words, 2 rows with 3 elements per row. However, while static two-dimensional arrays must have the same number of elements in each row, dynamic two-dimensional arrays can have variable length rows. This is achieved by maintaining a one-dimensional array of pointers on the stack or on the heap, where each pointer refers to a separate row. The elements within each row must be contiguous, but the rows themselves can reside anywhere in memory, non-contiguously, and each row can have a different length. If the rows are the same length then you can access the individual elements just as you would with a static array using array subscripts. However, with variable length rows, you must dereference the row and determine its length before accessing the elements within that row.
The program below uses vectors rather than arrays, but a vector is really nothing more than a one-dimensional dynamic array that knows its own size at all times. Vectors are therefore much easier to work with than arrays because all the memory allocations are done for us behind the scenes. A two-dimensional vector is nothing more than a vector of vectors which is easier to imagine as begin a vector of rows, where each row has variable length. However, the example below uses fixed-length vectors to implement an array of 5 rows with 4 elements per row.
Due to the fact that every row must be contiguous within itself and that rows may have variable length, it is always going to be easier to sort two-dimensional arrays by row rather than by column. If you need to sort by column rather than row, rather than write a separate and needlessly complex function to cater for this, it is simpler to just orient your array in memory or on disk so that you can sort by row instead. Note that it is trivial to change the orientation of an array whilst printing the array (simply swap the inner and outer loops) so there is no need to physically store the data in the same orientation as the output. The only thing that actually changes is how you visualise the data: whether as a vector of horizontal rows (actual rows) or as a vector of vertical rows (actual columns).
#include<iostream>
#include<iomanip>
#include<vector>
#include<random>
#include<time.h>
template<typename T>
void sort (std::vector<T>& A)
{
unsigned unsorted = A.size();
if (unsorted > 1)
{
do
{
unsigned new_size = 0;
for (unsigned index=1; index<unsorted; ++index)
{
if (A[index]<A[index-1])
{
std::swap (A[index], A[index-1]);
new_size = index;
}
}
unsorted = new_size;
} while (unsorted);
}
}
template<typename T>
void print_array(std::vector< std::vector<T> >& arr)
{
for (unsigned row=0; row<arr.size(); ++row)
{
for (unsigned col=0; col<arr[row].size(); ++col)
{
std::cout << '\t' << arr[row][col];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
template<typename T>
void randomise_array(std::vector< std::vector<T> >& arr)
{
// Pseudo-random number generator (range: 1 to 50).
std::default_random_engine generator;
generator.seed ((unsigned) time (NULL));
std::uniform_int_distribution<unsigned> distribution (1, 50);
for (unsigned row=0; row<arr.size(); ++row)
{
for (unsigned col=0; col<arr[row].size(); ++col)
{
arr[row][col] = distribution (generator);
}
}
}
int main()
{
// Instantiate a 5x4 array
std::vector< std::vector<unsigned> > original (5);
for (unsigned row=0; row<5; ++row)
original[row].resize (4);
randomise_array(original);
// Always work from a copy of the original
std::vector< std::vector<unsigned> > copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort each row individually:\n" << std::endl;
for (unsigned row=0; row<copy.size(); ++row)
sort (copy[row]);
print_array(copy);
copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort the rows:\n" << std::endl;
sort(copy);
print_array(copy);
copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort each row individually and then sort the rows:\n" << std::endl;
for (unsigned row=0; row<copy.size(); ++row)
sort (copy[row]);
sort (copy);
print_array(copy);
}
Example output:
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort each row individually:
5 8 14 43
9 12 15 49
4 27 31 35
6 11 15 37
4 32 35 42
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort the rows:
15 11 37 6
15 12 49 9
31 27 4 35
32 42 35 4
43 14 8 5
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort each row individually and then sort the rows:
4 27 31 35
4 32 35 42
5 8 14 43
6 11 15 37
9 12 15 49
What is meant by this pointer in C plus plus?
The this pointer is a hidden argument that is automatically generated by the compiler to refer to the current instance of an object.
Consider the following snippet:
class foo{};
void bar(foo* f){
std::cout< } int main() { foo f; bar(&f); return(0); } The function, bar, knows which instance of foo it is working on because we must explicitly pass a pointer to that instance (the address of f). However, if bar were a member of foo, we do not need to pass a pointer to the instance because the compiler does it automatically for us: class foo { void bar(){std::cout< }; int main() { foo f; f.bar(); return(0); } Although the second example is what we would physically write, it's really just sugar-coating. Behind the scenes, it's as if bar() were a non-member function and is actually implemented as per the first example, where we passed the address of the instance into the function. This is because there is only ever one instance of the bar function (regardless of how many instances of foo we create), and the compiler needs some way of determining which instance of foo the bar function should work with -- thus it generates a this pointer for us and hides the actual implementation behind sugar-coated code. The hidden this pointer is also used implicitly (behind the scenes) whenever you call member methods from within other member methods of the same class. However, you can invoke methods explicitly by dereferencing the this pointer if you want to. For instance, to invoke the bar() method from within another method of foo, you'd use the following call: (*this).bar(); More typically, you'd use the points-to operator instead: this->bar(); But since the this pointer is implicit, you don't need to explicitly dereference members like this unless it helps to clarify your code (such as when a member method operates upon other instances of the same class, and specifying this helps to clarify exactly which members you are invoking upon which instances). However, the this pointer really comes into its own when you actually need to refer to the current instance, such as when performing self-reference checks and returning references to the current instance. A classic example is the assignment operator which typically requires both: class foo { public: foo& operator=(const foo& rhs) { if( this != &rhs ) { // perform assignment } return(*this); } };
What is the only function all C plus plus programs must contain?
Every C plus plus program that is a main program must have the function 'main'.
Why need to declare header files compulsory in c plus plus but not in c?
The need to declare header files is not compulsory in C++. You may place all your code in a single source file if you so desire. However, header files are useful in that they separate interface from implementation and aid in hiding information.
How does virtual function support run time polymorphism?
Virtual functions are used to suport runtime polymorphism.In C++,if we have inheritance and we have overridden functions in the inherited classes,we can declare a base class pointer and make it to point to the objects of derived classes.When we give a keyword virtual to the base class functions,the compiler will no do static binding,so during runtime ,the base class pointer can be used to call the functions of the derived classes.Thus virtual functions support dynamic polymorphism.