Write a program to concatenate two strings in C plus plus?
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main()
{
string firstLine = "";
cout << endl << "Enter first line: ";
getline(cin, firstLine);
string secondLine = "";
cout << endl << "Enter second line: ";
getline(cin, secondLine);
cout << endl << "Two strings concatenated together: " << firstLine + secondLine << endl;
system("PAUSE");
return 0;
}
How data hiding is possible within a class in c plus plus?
A class is a description of an object, its attributes, and its methods. Its not a lot different than primitive types, such as int. int a; /* instantiate object of type int and call it a */ person b; /* instantiate object of type personand call it b */ The difference is that person is declared in a class specification, and is potentially much more complex than int. Still, you can think of both in the same context. The attributes (member variables) of a class contain the state of each instance of that class. You can (and usually do) declare those attributes as private, which means that only methods of the class can access them. This is data hiding. With data hiding, you can encapsulate the functionality of a class, exposing only the needed public interface. This way, if you need to change the way a class works internally, such as storing the person's name in unicode string instead of char string, that change can, if the interface is correct, be totally transparent to the user of the class.
What is the difference between C programming and CPP programming?
C has no object oriented support. C++ is a superset of C that adds object-oriented support, but retains the concept of primitive variables. C# is a superset of C++, developed by Microsoft, that removes the concept of primitives, making it fully object-oriented but, unlike C and C++, is non-generic and is only useful for .NET programming. C# is not unlike Java, but Java is fully cross-platform and therefore has much wider support.
What are virtual classes in c plus plus?
A local class refers to a class which has been defined within a function.
For example, consider a function A :
int A(int d)
{
class X
{
int a,b;
void func();
public :
int func2()
{
//do something
}
}
//rest of the function.
}
Class X is local to the function A, and can only be instantiated inside the function A. Use of such functions are usually indicative of poor function design.
Write a program to add two integers using c plus plus?
#include<iostream>
int main()
{
int a=40;
int b=2;
std::cout<<a<<'+'<<b<<'='<<a+b<<std::endl;
}
Output:
40+2=42
How do you access memory dynamically in c plus plus?
A pointer is a variable that can be used to store any memory address, including the null address (represented by the nullptr value). To access any non-null memory address via a pointer, simply dereference the pointer.
template<typename T>
void f (T* p) {
if (p==nullptr) return; // sanity-check
std::cout<<"1. The address of p: 0x" << std::hex << &p << std::endl; std::cout<<"2. The address pointed to by p: 0x" << std::hex << p << std::endl;
std::cout<<"3. The value pointed to by p is: " << *p << std::endl;
}
In the above example, output 3 shows how to dereference a pointer.
Note that this example will only compile if std::ostream::operator<< is overloaded to handle a type T. All primitive data types such as int and float are supported by default but all user-defined types require an explicit overload.
What is the difference between static data member and ordinary data member?
Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.
Which loop does not have an entry condition in C plus plus?
The only loop that does not require an entry condition is the procedural goto loop:
again:
/*...*/
goto again;
Although a do-while loop has no entry condition per-se, it still requires a mandatory entry condition into the second and all subsequent iterations.
do { /*...*/} while (true); // mandatory entry condition into all but the 1st iteration
And although a for loop's condition is optional, it is implicit:
for (;;) {/*..*/} // implicit for ever loop
for (;true;) {/*...*/} // explicit for ever loop
Write a program to print all two digit twin prime numbers?
#include
#include
void
main()
int prime(int n)
{
int i, j=n/2;
clrscr();
for(i=2; i<=j; i++)
{
if(n%j==0)
return 0;
}
return 1;
}
void
main()
{
int a,b,i;
clrscr();
printf("\n enter the twin prime numbers");
scanf("d",&a,&b);
printf("\n twin prime within this range \n");
if(a%2!==0)
i=a+1;
else;
i=a;
while(i+2<=b)
{
if(prime (i) &&prime(i+2) )
printf("\n%d\t%d",i,i+2);
i+1=2;
}
getch();
}
#include
#include
void main()
{
Int i, j, C;
printf("Enter the no");
Scanf("%d", &n);
for(j=2; j { If(n%j==0) { c++; } } if(c==0) { printf(" Entered no is prime no"); } getch(); } #include #include #include void main() { int i,j; clrscr(); for(i=3;i<=1000;i++) { for(j=2;j<=i;j++) { if(i%j==0) break; } if(i==j) cout< } getch(); // this is the easiest method for finding prime nos made by Taabi } B: To write the c version: i.e not C++, you write: A: /****************************** * THIS IS THE EASIEST * METHOD OF GENERATING * PRIME NUMBERS USING C * MADE BY: githambo@gmail.com *******************************/ #include
When was c plus plus developed?
Microsoft VC++ 1.0 was released in February 1993, about 10 years after Microsoft C 1.0 first appeared. The 32-bit version was released in December 1993 while the 64-bit version wasn't released until Visual Studio 2005 came out.
C program to swap three variables?
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=3;
swap(a,b);
printf("%d%d",a,b);
getch()
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("%d%d",x,y);
}
What is a simplex data communication mode?
One way data communication.
http://en.wikipedia.org/wiki/Simplex_communication
Parts of the program C plus plus?
parts of a program
Structure of C++ program
Documentation Section
Preprocessor Section
Definition Section
Global Declaration Section
main()
{
Declaration part;
Executable part;
}
sub program section
{
Sub program execution part
}
Write a C function to sort two dimensional integer array in ascending order?
Here's an example using a bubble sort. Note that this is not a very efficient sort, but it works weel enough with small data sets.
#include
#include
int main(void)
{
int item[100];
int a, b, t;
int count;
/* read in numbers */
printf("How many numbers? ");
scanf("%d", &count);
for(a = 0; a < count; a++)
scanf("%d", &item[a]);
/* now, sort them using a bubble sort */
for(a = 1; a < count; ++a)
for(b = count-1; b >= a; --b) {
/* compare adjacent elements */
if(item[ b - 1] > item[ b ]) {
/* exchange elements */
t = item[ b - 1];
item[ b - 1] = item[ b ];
item[ b ] = t;
}
}
/* display sorted list */
for(t=0; t
return 0;
}
Which is java default access specifier?
There is no such thing as an access specifier in Java. There are access modifiers.
The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'
How much a base class members can access of derived class?
Ideally, none. Base classes should have no knowledge whatsoever of their derived classes and therefore should have no access to any members declared in the derived classes. However, derived class methods are accessible via virtual functions declared in the base class. The correct methods (overrides) are called via the virtual table, but the base class itself requires no specific knowledge of the derived class in order to call these methods. This makes sense since a base class cannot know in advance all the classes that may or may not derive from it in the future.
If a method must be guaranteed to be implemented by a derived class, a pure-virtual method can be declared in the base class instead. This renders the base class abstract, meaning you cannot instantiate an object from the base class, only from a derivative that fully implements (or inherits) all the pure-virtual methods.
Conversely, a derived class can access all the public and protected members of its base class.
How do you write a program in C in alphabetical order?
#include<iostream>
#include<list>
struct item
{
item(const char ch):chr(ch), count(1){}
char chr;
size_t count;
};
int main()
{
const size_t size=50;
size_t idx;
std::list<item> freq;
std::list<item>::iterator iter;
std::string test;
for(idx=0; idx<size; ++idx)
test.push_back('a'+rand()%26);
for(idx=0; idx<size; ++idx)
{
for(iter=freq.begin(); iter!=freq.end() && (*iter).chr!=test[idx]; ++iter);
if( iter!=freq.end() )
++(*iter).count;
else
freq.push_back(item(test[idx]));
}
std::cout<<"Frequency table of the string:\n""<<test.c_str()<<""\n"<<std::endl;
for(iter=freq.begin(); iter!=freq.end(); ++iter)
{
item& itm=*iter;
std::cout<<itm.chr<<" = "<<itm.count<<std::endl;
}
std::cout<<std::endl;
}
How can a value from the derived class be assigned to a variable in base class?
Remember that derived classes can access the public and protected members of their base class, but none of the private members. If the member variable in the base class is protected (rather than private), then the derived class can assign it directly. However, this is bad style in OOP as it undermines data-hiding. A protected mutator (set accessor) would be a better option.
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
const int numOfElements = 5;//defines how many elements are in your array
double arr[numOfElements] = {0.0};
cout << endl << "Enter " << numOfElements << " for your array";
for (int i = 0; i < numOfElements; i++)
{
cout << endl << (i + 1) << " element: ";
cin >> arr[i];
}
double sum = 0.0;
for (int i = 0; i < numOfElements; i++)
{
sum += arr[i];
}
cout << endl << "Average is: " << (sum/numOfElements) << endl;
system("PAUSE");
return 0;
}
How do you write a program in c plus plus to search a number in an array?
The following function performs a non-recursive linear search of the given vector, returning a constant iterator to the element containing the given value. If the value does not exist, the "one-past-the-end" iterator is returned instead.
std::vector<int>::const_iterator find (int val, const std::vector<int>& v){
for (std::vector<int>::const_iterator it=v.begin(); it!=v.end(); ++it)
if (*it==val) return it;
return v.end();
}
What is the constraint of scope resolution operator?
Wahen we say scope we're referring to the enclosing context of a member. In C++, the scope or context of a member is defined by its enclosing namespace. A namespace allows us to completely separate all the enclosed members from all the members of all other namespaces. Namespaces can also enclose other namespaces. Members that do not have an enclosing namespace of their own are said to exist within the global namespace -- effectively a namespace with no name. However, the global namespace also provides the enclosing context for all other namespaces. That is, namespaces create a hierarchy or "family-tree" where the global namespace serves as the root.
Note that although namespaces are typically created by using the namespace keyword, we also create namespaces whenever we declare a class, struct, union or enum. That is, a class name is a namespace in its own right. If that class is not defined within the context of any other namespace then it implicitly exists within the global namespace.
Namespaces literally allow us to separate names (variable names, function names and class names, etc) into separate "spaces". That is, two namespaces can share the same name provided they exist within separate namespaces. However, it is often necessary for the members of one namespace to refer to the members of another namespace. This is achieved by using the scope resolution operator. If we do not use scope resolution, the compiler will search for the name within the current namespace and if no such name exists, it will search the global namespace. If the name cannot be found in either, a coimpiler error occurs.
With regards to global variables, we do not need to use scope resolution unless the global variable has a name that also exists within the current namespace. But since the global namespace has no name, we simply omit the namespace that would normally preceed the scope resolution operator. For instance, if the global variable were named matrix and the current namespace also happened to contain the same name, we can refer to the global instance as ::matrix.
Of course we could easily avoid such problems by choosing more appropriate names. Variables in the global namespace should always be given the prefix "g_", thus our global matrix becomes g_matrix. By the same token, member variables should be given the prefix "m_", thus our namespace's matrix becomes m_matrix.
While this resolves any potential name-clashes or ambiguity with regards member data and global data, prefixing global functions and member functions in this way would be considered quite unsatisfactory. In these cases scope resolution is the ideal solution.
Of course, it would be better to avoid global data altogether, but that's a different topic entirely.
The one that most commonly causes a foul odor is vaginal trichomoniasis.
mostly bacteria infections- and U T I's
The this pointer is an implicit, compiler generated pointer to the current object that a method is manipulating. It is used to differentiate scope between parameters and members, in the case where they have the same name. Example...
class myclass {
...
int data1;
...
mymethod(int data1) {
data1 = data1; /*ambiguous */
this->data1 = data1; /* not ambiguous */
}
...
secondmethod(int data2) {
data1 = data2; /* not ambiguous */
}
...
}
Many coders use some prefix, such as underscore, to mark member variables. This works, but is not necessary.
Minimum number of queues needed to implement the priority queue?
Separated queue for every possible priority value.