What is concession constructor?
Constructors are basically used to evoke methods of a class creating its object..and as far as i know there is no constructor called concession constructor..
Virtual inheritance is used in multiple inheritance hierarchies whenever two or more classes inherit from the same base class. For example, consider the following minimal example:
struct A {int x;};
struct B : A {};
struct C : A {};
struct D : B, C {};
Here, A is a common base class of both B and C, thus B::A and C::A are independent instances of A. However, D inherits from both B and C thus indirectly inherits both B::A and C::A. This introduces an ambiguity when implicitly referring to A from D, as in the following example:
D d; d.x = 42; // error: ambiguous
When the compiler sees this it won't know whether we are referring to B::A.x or C::A.x, thus we must be explicit when referring to A via D:
d.B::x = 42; // ok
The problem with this is that we still have two separate instances of x so unless we are extremely careful about which x we are referring to we can easily end up with two different values for x with no way of knowing which was the correct value with respect to D.
What we really need is for B and C to share the same instance of A and we achieve that through virtual inheritance:
struct A {int x;};
struct B : virtual A {};
struct C : virtual A {};
struct D : B, C {};
D d;
d.x = 42; // ok
Note that d.x can be referred to as d::A.x, d::B::A.x or d::C::A.x because they all refer to the same instance of x, thus the ambiguity is eliminated. In effect, the most-derived class in the hierarchy (D) is now a direct descendant of A rather than an indirect descendant of both B::A and C::A.
Note also that the virtual keyword has no effect on independent instances of either B or C because they then become the most-derived classes within their own hierarchy and therefore behave exactly as they would had the virtual keyword been omitted. The virtual keyword only comes into play when we derive from a class with a virtual base.
It is also possible to have a base class that is both virtual and non-virtual in the same hierarchy:
struct A {int x;};
struct B : virtual A {};
struct C : virtual A {};
struct D: A {}
struct E : B, C, D {};
Here, A is a virtual base of both B and C, as before, but not of D. Thus E::A implicitly refers to the virtual A while E::D::A must be explicitly referred to as E::D::A because it is a separate instance. Such classes are best avoided, but if we don't have access to the source code we sometimes don't have a choice in the matter.
However, if a base class holds no data it really doesn't matter whether it is declared virtual or not because a class that has no data consumes just one byte regardless of how many instances there are.
Write a c plus plus program to print prime numbers up to 50?
#include <iostream>
bool isPrime(int p)
{if( p<=1 ( p>2 && p%2==0 ))return( false );int max = (int)sqrt(( double ) p ) + 1;
for( int i=3; i<=max; i+=2 )if( p%i==0 )return( false );return( true );
}
int main()
{for( int i=1; i<50; i>2?i+=2:++i )if( isPrime(i) )std::cout << i << std::endl;return( 0 );
}
How can you make a splash screen in c or c plus plus?
Many people use a modified version of the program's About dialogue window as a splash screen, but you can also create a dedicated splash screen window that contains an image of the company logo along with some program information. The choice is yours but ultimately a splash screen is nothing more than a window that is shown while a program loads in the background. When the program has loaded, the splash screen is closed automatically.
The simplest method of displaying your splash screen is to use a program loader function. Typically this will be your program's main function or a function called from your main function. The program loader first loads and displays the splash screen, then loads the rest of your program in the background before closing the splash screen window and finally showing the main window.
Some splash screens display progress information during the load sequence, thus your program loader should send messages to the splash screen and give time to the system to allow messages to be processed. This will increase the time it takes to load your program, but if the load process is lengthy to begin with, it is better to give some indication of the progress.
How can access an interface from another interface and how to access from a class?
Interfaces are designed to do exactly that: to interface or to interact. In object-oriented programming languages such as C++, you can incorporate up to three different interfaces per class. The private interface is accessible only to the class itself and to friends of the class. The protected interface is the same as the private interface but is also accessible to derivatives of the class. The public interface is accessible to any code.
For one interface to interact with another interface, the first must have access to the second. If the first is a friend of the second or both are members of the same class, the first has unrestricted access to the private, protected and public interfaces of the second. If the first is derived from the second but is not a friend, the first only has access to the protected and public interfaces of the second. If the first is completely separate from the second, the first only has access to the public interfaces of the second.
What is the need for indentation in while writing a c plus plus program?
Indenting is a good habit to inculcate while writing any type of code
Although indentation is not mandatory and will never affect the working of your programme in C++ it makes the code more easy to read and debug especially for larger programmes. Most IDE's eg eclipse automatically indent the code as you type.
What are attributes and methods in class?
Attributes are the class member variables, the data, fields or other properties that define the class state. Methods are the functions of a class, the operations that define its behaviour, typically working in conjunction with the class member attributes to either alter the class state (mutators) or query the class state (accessors). Special methods such as the class constructors, its destructor and conversion operators are invoked indirectly through compiler-generated code while all others are called directly via programmer-generated code.
Why the return type of all the function by default is int in c?
Because int is the most common data-type in C. Don't rely on this though, always specify the return type explicitly.
Which function should be used to free the memory allocated by calloc?
Use the free function to release memory that was previously allocated by malloc, calloc or realloc.
How a string can be handled by array?
A string is an array. To be precise, a string is an array where every element is a character type. The term string simply relates to the fact the array (like any other type of array) resides in contiguous memory, thus forming a string of characters in memory.
There are in fact two types of string array. The most common is the ASCII string, where each element is 1 byte in length (an array of type char). The other is the UNICODE string where each element is 2 bytes in length (an array of type wchar_t).
In essence, ASCII strings are constructed from character codes 0-255 in the standard ASCII character set, while UNICODE can represent up to 65,536 different character codes, the first 256 being the same as the ASCII character set. UNICODE is generally used to provide character codes that are not available in ASCII, however many systems (Microsoft in particular) handle all strings as UNICODE strings (converting to and from ASCII as required). If you do a lot of string manipulation, then it is often best to use UNICODE at all times to reduce the amount of conversions going on in the background (check your compiler's documentation for specifics).
When dealing with strings we often talk about string buffers. A buffer is simply an array of a specific size (allocated dynamically or statically) within which we can store a sequence of character codes. Since characters are consistently either one byte or two bytes long, it's easy to determine how many characters we can place in these buffers.
However, just to confuse matters, there is actually a third type of string known as a variable-width encoded string. Microsoft calls these MBCS strings, but this really means multibyte character set, which is not the same. The width of a character is determined by the encoding, not the character set. The details don't really concern us, but suffice to say, each character has variable-width and there are many different encoding schemes available. As a general rule, if you must deal with variable-width encoded strings, use UNICODE instead, and only encode as variable width when you must. It'll make your life a lot simpler in the long run.
You will undoubtedly encounter other types of string, such as std::string (and its UNICODE equivalent, std::wstring). This is really just an object that encapsulates a character array so you can use it just as you would a standard character array. However, do not let this fool you -- it is not a character array per se, it is merely a wrapper that mimics an array. There is an underlying array within the object's members, and several useful methods to manipulate the array (such as concatenating two strings), however memory management is handled by the object. While this simplifies things for programmers, it is not the most efficient way to store a string when that is all you want to do. But if you want to actually manipulate the string, then std:string is as good a way as any.
To make use std:string you must include
Write a program to count number of spaces in a string using array?
#include<iostream>
#include<string>
size_t count_spaces(std::string& str)
{
size_t spaces=0;
for(size_t i=0; i<str.size(); ++i)
if( str[i]==32 )
++spaces;
return( spaces );
}
int main()
{
std::string str("This is a string with some spaces.");
size_t spaces = count_spaces(str);
std::cout<<"The string ""<<str.c_str()<<"" has "<<spaces<<" spaces.\n"<<std::endl;
}
class Book
{
public:
Book(std::string title, std::string author, std::string publisher, double price) : m_title(title), m_author(author), m_publisher(publisher), m_price(price) {}
std::string get_title()const{return(m_title);}
std::string get_author()const{return(m_author);}
std::string get_publisher()const{return(m_publisher);}
int get_price()const{return(m_price);}
private:
std::string m_title;
std::string m_author;
std::string m_publisher;
double m_price;
}
What do you mean by protected derivation of a sub class from base class?
When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class.
By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class.
Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected.
Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.
What is persistence in oops explain with example?
The phenomenon where the object outlives the program execution time & exists between execution of a program is known as persistance
What is the function of thymine?
One of four nucleobases in the nucleic acid of DNA, thymine is also known as 5-methyluracil. Thymine creates thymidine when combined with deoxyribose.
How many bits in a bool variable?
It depends on the implementation. Technically, only one, because that's all you need to store a true/false value. From an implementation standpoint, however, the compiler may well allocate a full byte, or even a word, for a boolean variable, because the cost of doing bit manipulation is too much, both in terms of time and of space.
Why is priority configured in 4096 increments when using PVST plus?
Use of the extended bridge ID leaves only four bits for the bridge priority.
Can a conditional operator replace an if statement always?
No. An if statement does not require an elseclause and the expression(s) do not return anything to the caller, whereas the conditional operator always executes one of two expressions and always returns the value of the executed expression back to the caller. The executed expression may be yet another conditional operator, thus allowing simulation of nested ifs and if...else if... else statements.
Consider the following example:
int x = rand();
if( x > 100 ) x = 100;
We can achieve the same result with a conditional operator:
int y = rand();
y = y>100 ? 100 : y;
However, if we were to expand this statement to an if statement we would not get the original if statement shown above:
int z = rand();
if( z > 100 ) z = 100;
else z = z;
The else clause is clearly unnecessary in this case, so the original if statement would be the statement of choice here.
As a general rule, if you can make use of the return value in a conditional operator, and must return one of at least two values, then use the conditional operator. Otherwise use an if statement.
Write a Program to implement kruskal's algorithm in c?
/* Program for creating a minimum spanning tree from Kruskal's
algorithm */
#include
#define MAX 20
struct edge
{
int u;
int v;
int weight;
struct edge *link;
}*front = NULL;
int father[MAX]; /*Holds father of each node */
struct edge tree[MAX]; /* Will contain the edges of spanning tree */
int n; /*Denotes total number of nodes in the graph */
int wt_tree=0; /*Weight of the spanning tree */
int count=0; /* Denotes number of edges included in the tree */
/* Functions */
void make_tree();
void insert_tree(int i,int j,int wt);
void insert_pque(int i,int j,int wt);
struct edge *del_pque();
main()
{
int i;
create_graph();
make_tree();
printf("Edges to be included in spanning tree are :\n");
for(i=1;i<=count;i++)
{
printf("%d->",tree[i].u);
printf("%d\n",tree[i].v);
}
printf("Weight of this minimum spanning tree is : %d\n", wt_tree);
}/*End of main()*/
create_graph()
{
int i,wt,max_edges,origin,destin;
printf("Enter number of nodes : ");
scanf("%d",&n);
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d(0 0 to quit): ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) )
break;
printf("Enter weight for this edge : ");
scanf("%d",&wt);
if( origin > n destin > n origin<=0 destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
insert_pque(origin,destin,wt);
}/*End of for*/
if(i
{
printf("Spanning tree is not possible\n");
exit(1);
}
}/*End of create_graph()*/
void make_tree()
{
struct edge *tmp;
int node1,node2,root_n1,root_n2;
while( count < n-1) /*Loop till n-1 edges included in the tree*/
{
tmp=del_pque();
node1=tmp->u;
node2=tmp->v;
printf("n1=%d ",node1);
printf("n2=%d ",node2);
while( node1 > 0)
{
root_n1=node1;
node1=father[node1];
}
while( node2 >0 )
{
root_n2=node2;
node2=father[node2];
}
printf("rootn1=%d ",root_n1);
printf("rootn2=%d\n",root_n2);
if(root_n1!=root_n2)
{
insert_tree(tmp->u,tmp->v,tmp->weight);
wt_tree=wt_tree+tmp->weight;
father[root_n2]=root_n1;
}
}/*End of while*/
}/*End of make_tree()*/
/*Inserting an edge in the tree */
void insert_tree(int i,int j,int wt)
{
printf("This edge inserted in the spanning tree\n");
count++;
tree[count].u=i;
tree[count].v=j;
tree[count].weight=wt;
}/*End of insert_tree()*/
/*Inserting edges in the priority queue */
void insert_pque(int i,int j,int wt)
{
struct edge *tmp,*q;
tmp = (struct edge *)malloc(sizeof(struct edge));
tmp->u=i;
tmp->v=j;
tmp->weight = wt;
/*Queue is empty or edge to be added has weight less than first edge*/
if( front NULL) /*Edge to be added at the end*/
tmp->link = NULL;
}/*End of else*/
}/*End of insert_pque()*/
/*Deleting an edge from the priority queue*/
struct edge *del_pque()
{
struct edge *tmp;
tmp = front;
printf("Edge processed is %d->%d %d\n",tmp->u,tmp->v,tmp->weight);
front = front->link;
return tmp;
}/*End of del_pque()*/
A=8
B=4
C=14
Yes, I know I'm a genius.
Mak - the all knowing one.
MWAHAHAH!
Which operand should be passed in the binary overloaded operator function as a second operand?
The right-hand operand; the r-value of the operator.
Unary operators have one operand while tertiary operators have three operands. All binary operators have two operands, the l-value and the r-value. The l-value is the operand to the left of the operator while the r-value is the operand to the right of the operator.
Thus, in the expression x + y, x is the l-value while y is the r-value.
When overloading binary operators in a class, you need only specify the r-value. The l-value is the instance of the class to which the operator applies and therefore does not need to be specified. For instance:
class MyClass
{
public:
MyClass(int data=0):m_data(data){} // default constructor
int operator+ (const int rhs) const {return(m_data+rhs);}
private:
int m_data;
};
While this allows you to return the sum of your class instance and an integer, it does not allow you to return the sum of an integer and an instance of your class. For example:
MyClass obj(5);
int x = 10;
int y = obj + x; // OK! y is 15.
int z = x + obj; // Compiler error! No operator exists that accepts an r-value of type MyClass.
To fix this error and allow for two-way addition, you must declare a binary operator overload outside of the class. You cannot do it inside the class because the l-value is an int, not an instance of MyClass.
The external overload requires two parameters, the l-value and the r-value of the operator:
int operator+(const int lhs,const MyClass& rhs) {return(rhs+lhs);}
Note that the implementation simply reverses the operands. This is functionally equivalent to making the following explicit call:
return(rhs.operator+(lhs));
Note also that since MyClass::operator+ is a public operator of MyClass, this overload does not need to be declared a friend of MyClass (a common misconception). However, the overload must be declared in the same file where the class is declared since it is only of relevance to MyClass but should be made available wherever MyClass is accessible.
the decryprtion code function is
void decrypt_mono(char word[],char cypher[])
Are structures and pointers related in c plus plus?
Not really, but you can have:
- a pointer pointing to a structure (FILE * is an example)
- a pointer pointing to a structure-member (eg: struct tm tm; int *ip= &tm.tm_year)
- a structure-member that is a pointer (any type)
Example:
typedef struct TreeNode {
struct TreeNode *left, *right;
int data;
} TreeNode;
TreeNode *root = (TreeNode *)calloc (sizeof (TreeNode), 1);