When a language has the capability to produce new data type is also called?
New, compared to what? I guess you meant user-defined data-types, which exist in almost every modern programming language.
What is string in c plus plus?
A String is a variable that represents a list, or array, of characters. They are normally assigned using double quotes (""). When using strings you must import string header file
#include
and then an example of a string
string mystring = "This is a string";
This give the variable 'mystring' a value of "This is a string".
A string can also be referred to as an array of characters
char string [10];
Would make the variable string to an array of characters with length ten.
Write a c plus plus program to find the largest and second largest number from an array?
#include <iostream.h>
#include <conio.h>
class largest
{
public:
int a[10],i,b,s;
void get();
};
void largest :: get()
{
cout<<"Enter the numbers"<<endl;
for(i=0;i<10;i++)
{
cin>>a[i];
}
b=0;
for(i=0;i<10;i++)
{
if(b>=a[i])
b=b;
else
b=a[i];
}
cout<<"Largest No-"<<b<<endl;
s=0;
for(i=0;i<10;i++)
{
if((s>=a[i])&&(s!=b))
s=s;
else
s=a[i];
}
cout<<"Second Largest No-"<<s<<endl;
}
void main()
{
clrscr();
largest l;
l.get();
getch();
}
When do you need to use default arguments in a function?
We declare (not use) default arguments in a function whenever the default values cover the majority of calls to that function. We use default arguments in order to simplify those calls and thus reduce the verbosity of our calling code, thus making it easier to call the function.
How many bytes are allocated to an int in C?
16 bit and 32 bit are the most common values. See sizeof.
What is the C program for byte stuffing with the output?
//Sender
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#define SIZE 40
struct frame
{
char str[SIZE+1];
char tmp[2*SIZE];
char flag;
char e;
char final[2*SIZE];
}s;
main()
{int fd,len1;
int i,j,len;
fd=open("b1",O_WRONLY);
printf("\nEnter flag character and escape character for byte-stuffing:");
scanf("%c %c",& s.flag,&s.e);
printf("\nEnter string:");
scanf("%s",s.str);
len=strlen(s.str);
for(i=0,j=0;i<len;i++,j++)
{
if(s.str[i]==s.flag)
{
s.tmp[j]=s.e;
s.tmp[j+1]=s.flag;
j++;
continue;
}
else if(s.str[i]==s.e)
{
s.tmp[j]=s.e;
s.tmp[j+1]=s.e;
j++;
continue;
}
else
{
s.tmp[j]=s.str[i];
}
}
printf("\nAppended string is==>%s \n",s.tmp);
len1=strlen(s.tmp);
for(i=0,j=0;i<=len1;i++,j++)
{
if((i==0)(i==len1))
{
s.final[j]=s.flag;
s.final[j+1]=s.tmp[i];
j++;
continue;
}
else
{
s.final[j]=s.tmp[i];
}
}
printf("\nFianal string is==>%s\n",s.final);
write(fd,&s,sizeof(s));
}
//Reciver
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#define SIZE 40
struct frame
{
char str[SIZE+1];
char tmp[2*SIZE];
char flag;
char e;
char final[2*SIZE];
}r;
main()
{
int fd,len1;
int i,j,len;
mknod("b1",010666,0);
fd=open("b1",O_RDONLY);
read(fd,&r,sizeof(r));
printf("\nFlag character is==>%c\n",r.flag);
printf("\nEscape character is ==>%c\n",r.e);
printf("\nAnd actual message was ==>%s\n",r.str);
printf("\nReceived message is %s\n\n",r.final);
}
/*****************
[mca222@rcclinux mca222]$ cc -os byte_s.c
[mca222@rcclinux mca222]$ cc -or byte_r.c
[mca222@rcclinux mca222]$ ./r&
[1] 1570
[mca222@rcclinux mca222]$ ./s
Enter flag character and escape character for byte-stuffing:#
@
Enter string:sim#andh@ar
Sending message is==>#sim@#andh@@ar#
Flag character is==>#
Escape character is ==>@
And actual message was ==>sim#andh@ar
Received message is #sim@#andh@@ar#
C plus plus program using operator overloading?
class foo
{
private:
int m_data;
public:
foo (int data=0): m_data (data) {}
foo (const foo& source): m_data (source.m_data) {}
foo (foo& source): m_data (std::move (source.m_data)) {}
// operator overloads: assign
foo& operator= (const int source) { m_data = source; return *this; }
foo& operator= (const foo& source) { m_data = source.m_data; return *this; }
foo& operator= (foo& source) { m_data = std::move (source.m_data); return this; }
// compound operator overloads: increment and assign
foo& operator+= (const foo& rhs) { m_data += rhs.m_data; return *this; }
foo& operator+= (const int rhs) { m_data += rhs; return *this; }
};
There are a few different ways to do this but this one will work just fine for your problem. (note these numbers are not truly random, but they will probably be random enough for your purposes)
you will have to:
import java.util.Random;
then define a new random number generator called Generator:
Random generator = new Random(); Then, make a random integer between 0 and 40 and subtract 20 so it is between -20 and positive 20. int myNumber = generator.nextInt(40) - 20;
[CTRL]+C is achieved by holding CTRL while pressing C.
What are the advantages of constructor and destructor?
Without a copy constructor the only way to copy an object would be to instantiate a new object (or use an existing object) and then assign another object's value to it. However, it would be very odd indeed to have a copy assignment operator without a matching copy constructor. If you have one, you must have both. If you do not need to copy an object of a particular class, however, you can simply delete both the copy constructor and the copy assigment operator for that class. Any attempt to copy or copy assign would then result in a compile-time error.
What is the virtual function and friend function?
I have not used friend concept in C#. Actually it may not exist in C#.
Based on OO principles, the friend feature in C++ violates the privacy or encaptualization of private data memeber.
Would you allow your friend (a friend class) to access your wallet or bank account (private member)?
What is static variable in the class in cpp?
Static member variables of a class are variables that are local to the class within which they are declared. That is, each instance of the class (each object) shares the same common member variable. Changing that variable's value in one instance will change it for all instances.
Contrast with a non-static member variable where each instance of the class has its own independent variable, scoped to the object itself (not the class of object).
Where does global variables stored in C?
It depends entirely on what platform you are using.
In an embedded environment, for instance
global/static variables go into different RAM memory segments depending on whether or not they are initialised.
constants are often left in ROM
automatic variables are normally placed of the stack of the currently running task but not always.
How to remove duplicate values in an array?
The simplest way of doing this is not very efficient, but provides a quick development time. You want to take all of the elements of the array, add them to a Set, then retrieve them as an array again. Note that this will probably not preserve the order of elements in the array.
{
Object[] originalArray; // let's pretend this contains the data that
// you want to delete duplicates from
Set newSet = new HashSet();
for (Object o : originalArray) {
newSet.add(o);
}
// originalArray is now equal to the array without duplicates
originalArray = newSet.toArray();
}
Now the efficient, and more correct, solution.
This one will create a new array of the correct size without duplicates.
{
Object[] originalArray; // again, pretend this contains our original data
// new temporary array to hold non-duplicate data
Object[] newArray = new Object[originalArray.length];
// current index in the new array (also the number of non-dup elements)
int currentIndex = 0;
// loop through the original array...
for (int i = 0; i < originalArray.length; ++i) {
// contains => true iff newArray contains originalArray[i]
boolean contains = false;
// search through newArray to see if it contains an element equal
// to the element in originalArray[i]
for(int j = 0; j <= currentIndex; ++j) {
// if the same element is found, don't add it to the new array
if(originalArray[i].equals(newArray[j])) {
contains = true;
break;
}
}
// if we didn't find a duplicate, add the new element to the new array
if(!contains) {
// note: you may want to use a copy constructor, or a .clone()
// here if the situation warrants more than a shallow copy
newArray[currentIndex] = originalArray[i];
++currentIndex;
}
}
// OPTIONAL
// resize newArray (using _newArray) so that we don't have any null references
String[] _newArray = new String[currentIndex];
for(int i = 0; i < currentIndex; ++i) {
_newArray[i] = newArray[i];
}
}
---------
The second version is correct in theory. However, if you deal with large two- or more- dimensional arrays, you are in trouble, as with each new element in the destination array you will have to search through a greater number of elements.
This is especially true if you look for duplicates in more than one element of the array, for example looking in columns 'a' and 'b' of array
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
Drop in performance is unbelievable if you go over approx 1,000 records with majority or records being unique.
I am trying to test a couple of different approaches for large arrays. If anyone is interested, let me know, and I will keep you posted.
What is ide in c plus plus .explain?
There is no single answer to this since everyone has their own opinion on what is the best IDE. For example, a Windows programmer will probably advocate Borland/Embarcadero C++ Builder or Microsoft VC++, while a Linux programmer might favour a more generic implementation such as GC++, which is better suited to cross-platform coding.
When would you use overloading in C plus plus?
A function overload is a function that has more than one implementation. Each implementation must have a unique function signature, only the name remains the same, however functions cannot differ by return type alone.
Many overloads can be implemented as template functions, where the only difference is the type or types being operated upon. Manual overloading is typically required when the number of arguments differ. Generally, we try to implement function overloads such that one overload provides the implementation for the general case, while all others are expressed in terms of the general case. Where this may result in a loss of efficiency, we generalise as much as is possible and provide a limited number of specialised implementations to cater for exceptional cases.
As a typical example, to find the largest of any two values of the same type, regardless of the type, we can use a template function:
template<typename T>
const T& largest (const T& a, const T& b) {
return a>b?a:b;
}
This function provides the general case. From this we can provide specialisations, such as an overload to cater for three arguments which can be expressed directly in terms of the general case:
template<typename T> const T& largest (const T& a, const T& b, const T&c) {
return largest (largest (a, b), c);
}
Difference between C and C Plus programming languages?
C++ allows you to mix object oriented programming with generic programming and C-style programming. C has none of these features.
C-style programming and C programming are broadly similar, with only minor differences, most of which are irrelevant. As such, it is very rarely necessary to use a lower-level language than C++.
The static type system in C is inherently weak; filled with loopholes and inconsistencies. C++ is much more robust by comparison.
What is an interface class and what is an abstract class?
The term interface class does not exist in C#. If this is a term describing a class being an interface to other component (human, subsystems, etc), it is very application specific. The designer of that application should know the abstraction.
However, C# does have another type called interface. An interface is NOT a class. An interface defines the intention of some behaviors that classes may be extended it and provides the implementation. The intention, is nothing but method signatures, which defines the return data type, the method name, and any method arguments and associated data type. The implementation is the code of the method. Interface is used for separating the concern of design and implementation.
Abstract class is a class with abstract keyword. It can be just like a class without that keyword (then, why it is an abstract class?). But it may have some methods or properties defined as abstract. These abstract methods, like the method signatures of an interface, defines the intention.
The subclasses of such an abstract class would need to implement those abstract methods (providing the code).
There are more common, differences between interfaces and abstract classes, please see answer(s) of those related questions in C# category.
What is self referential function in c plus plus?
A self-referential function in C++, or in any other supporting language, is a recursive function.
Why you cannot pass arguments to destructor in C programming?
A more pertinent question is why would you need to? The destructor exists purely to clean up any memory allocated to the class instance variables. If the destructor requires a "parameter" to do so, then there's a clear design flaw in the class. The class is perfectly capable of cleaning up its own instance variables without any need for a parameter.
What is push operation in data structure?
Push inserts a value onto the top of the stack.
Pop extracts the top value from the stack.
These are the two primary operations that can be performed upon a stack. Prior to popping a value, you will first check the stack is not empty, store the top value, then pop the stack. For a stack of type T, you might use the following:
if (!stack.empty()) {
T value {stack.top()}; // copy top value
stack.pop(); // remove value from stack
// use value...
}
What are the main features of OOP in c plus plus?
The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.