What is the main purpose of using comments in c plus plus?
The main advantage of the newer C++ comment syntax is that you only need to start the comment, and you can expect that it will last only until the end-of-line. With the older syntax, you needed to also stop the comment. any statement; /* old comment syntax */
any statement; // new comment syntax
What is casting in java and c plus plus?
Implicit casting is done automatically by the compiler and virtual machine.
Explicit casting is needed to convert types of data when Java is not sure if the result will be valid. There are two times when you will need to perform explicit casts: casting between primitives and casting between objects.
Examples between primitives:
byte b; short s;
s = b; //valid
b = s; //invalid
b = (byte)s; //valid
b = (byte)Short.MAX_VALUE; //valid, but overflow will occur.
Examples between objects:
Object o = s; //upcast - no explicit casting required
String s = (String)new Integer(); //always a compile error
String s = (String)getObject(); //assume that getObject() returns type Object. This will only work if the value stored in getObject() is a String or a subclass of String. Otherwise a runtime error will occur.
What are the 42 keywords in turbo c?
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Explain static data member with the help of example?
Yes. Static data members are local to the class in which they are declared. Thus all instances of the class share the same variables (unlike non-static data members where each instance of the class has its own set of variables). Moreover, since static data members do not belong to any instance of the class, they are accessible without the need to instantiate an instance of the class, and like all other static variables, remain in scope for the entire duration the program is running. Also, as with all other static variables, they must be initialised at compile time from outside of the class declaration. Usually this is done from the class CPP file.
Static member functions are similar to static data members in that they are local to the class, rather than to an instance of the class. Since they do not belong to any instance of the class, they do not inherit an implicit this pointer. As a result, they are accessible without the need to instantiate an instance of the class and will remain in scope for the entire duration the program is running.
It is not unusual for a class to have both static data members and static member functions. They can be likened to global variables and global methods, but scoped to the class. However, their visibility can be restricted by the access specifiers enforced upon them (public, protected or private). Although static member functions cannot access instance methods and instance variables (unless an instance is physically passed to them as an argument) they have unrestricted access to the static data members of the class, as do all instances of the class and friends of the class.
A classic example of static member functions and static data members in the same class is when one needs to maintain a count of all instances of a class. All the class constructors must increment the static counter while the destructor must decrement it. A static member function such as GetCount() can then report the number of instances currently instantiated, even when there are no instances.
There are many other uses, but the golden rule is that they must be related to the class in which they are declared. If their purpose is simply to provide global functionality then declare them as such, or (better) limit their scope by declaring them in a separate class specifically for that purpose with private constructors to prevent any instances from being created (since none would be required).
#include<iostream>
#include<vector>
template<class T>
void insertion_sort (std::vector<T>& v)
{
if (v.size()<2U)
return;
for (size_t index=1; index<v.size(); ++index)
{
T value = v[index];
size_t gap = index;
size_t prev = index-1;
while (gap && value<v[left] )
v[gap--]=v[left--];
v[gap] = value;
}
}
int main()
{
std::vector<int> vect {42, 1, 27, 8, 15, 4};
for (auto v : vect) std::cout << v << '\t';
std::cout << std::endl;
insertion_sort (vect);
for (auto v : vect) std::cout << v << '\t';
std::cout << std::endl;
}
How do you use C plus plus code in Java Program?
You don't. There are two possible workarounds.
How to calculate sum of two complex number in c plus plus?
typedef struct complex {
double real, imag;
} complex;
...
complex x, y, z;
...
/* add */
z.real = x.real + y.real;
z.imag = x.imag + y.imag;
/* sub */
z.real = x.real - y.real;
z.imag = x.imag - y.imag;
/* mul */
z.real = x.real*y.real - x.imag*y.imag;
z.imag =x.imag*y.real + x.real*y.imag;
/* div */
double d = y.real*y.real + y.imag*y.imag;
z.real = (x.real*y.real + x.imag*y.imag)/d;
z.imag = (x.imag*y.real - x.real*y.imag)/d;
What is the importance of using an array in C plus plus programming?
When there is a need of storing a list of same type of large no. of data in linear manner,it is ridiculous to use large no. of different variables.It is more complex also.So in c and c++ array is used.
If the ans helps you,plz increase the trust point.
An object is an instance of a class. A class is a user-defined data type from which we can instantiate objects of that class. We often use the terms object and variable interchangeably, however the term variable specifically refers to a named object (objects instantiated at compile time), as opposed to anonymous objects (instantiated at runtime). Built-in data types such as int, double and pointer types are not classes, thus instances of these types are simply known as variables. Built-in types are also part of the language (hence they are built-in) thus we don't need to include a header or a type definition in order to use them; they are immediately available. But to use an object we must first define its class or include the appropriate header that defines the class.
How do you write a c plus plus program to calculate number of days between given date's?
#include<stdio.h>
#include<conio.h>
#include<time.h>
time_t time(time_t *t);
main()
{
int y2,m2,d2,y,m,d;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("Time now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
y2=tm.tm_year+1900;
m2=tm.tm_mon+1;
d2=tm.tm_mday;
printf("Enter the Reference Date in DD MM YY format\n");
scanf("%d%d%d",&d,&m,&y);
int loop,total_days = 0,days_in_a_year;
int day,month,year;
int month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
printf("\n%d\n%d\n%d\n",(y2-y),(m2-m),(d2-d));
printf("%d",month_days[m]);
if(((y2-y)==0&&(m2-m)<1&&(d2<d))(y2<y)(m<1)(m>12)(d<0)(d>month_days[m-1]))
{
printf("\nInvalid Reference Date\n");
}
else
{
total_days=month_days[m-1]-d;
if((m==2)&&((y%4)==0))
total_days+=1;
for(int i=m;i<12;i++)
{
if((y%4==0)&&i==1)
total_days+=1;
total_days+=month_days[i];
}
for(int year=y+1;year<y2;year++)
{
if(year%4==0)
total_days+=366;
else
total_days+=365;
}
for(int i=0;i<m2-1;i++)
{
if((y2%4==0)&&i==1)
total_days+=1;
total_days+=month_days[i];
}
total_days+=d2;
if((y2==y)&&(y%4==0))
total_days-=366;
if((y2==y)&&(y%4!=0))
total_days-=365;
}
printf("\nThe date difference is %d\n",total_days);
getch();
}
What is public in c plus plus?
public is an access-modifier that allows you to access methods or properties of a class from outside of the class. A method or property set to protected can only be accessed from within the creating class or subclasses, while a private method or property can only be accessed from within that class
Write a C plus plus program to interchange two numbers by using function?
#include<iostream>
template<typename _Ty>void swap (_Ty& a, _Ty& b)
{
_Ty temp = a;
a = b;
b = temp;
}
int main()
{
int x = 42;
int y = 0;
std::cout << "Before swap:" << std::endl;
std::cout << "x = " << x << ", y = " << y << std::endl;
swap (x, y);
std::cout << "After swap:" << std::endl;
std::cout << "x = " << x << ", y = " << y << std::endl;
}
Example Output
Before swap:
x = 42, y = 0
After swap:
x = 0, y = 42
Write a program that sorts an array of integers using pointers?
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX 50
#define N 2000
void sort_words(char *x[], int y);
void swap(char **, char **);
int main(void)
{
char word[MAX];
char *x[N];
int n = 0;
int i = 0;
for(i = 0; scanf("%s", word) == 1; ++i)
{
if(i >= N)
printf("Limit reached: %d\n", N), exit(1);
x[i] = calloc(strlen(word)+1, sizeof(char));
strcpy(x[i], word);
}
n = i;
sort_words(x, n);
for(i = 0; i < n; ++i)
printf("%s\n", x[i]);
return(0);
}
void sort_words(char *x[], int y)
{
int i = 0;
int j = 0;
for(i = 0; i < y; ++i)
for(j = i + 1; j < y; ++j)
if(strcmp(x[i], x[j]) > 0)
swap(&x[i], &x[j]);
}
void swap(char **p, char **q)
{
char *tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
#include<iostream>int main () {
int age, female=0, male=0, fcount=0, mcount=0;
char sex;
for (int voter=0; voter<10; ++voter) {
std::cout << "Enter the voter's age and gender: ";
std::cin >> age >> sex;
switch (sex) {
case 'm':
case 'M': ++mcount; male += age; break;
case 'f':
case 'F': ++fcount; female += age; break;
}
}
std::cout << "Average male age: " << male / mcount << std::endl; std::cout << "Average female age: " << female / fcount << std::endl;
}
Is C plus plus a query language?
No. Standard C++ has no dependencies whatsoever, unlike Java which is entirely dependent upon the Java Virtual Machine, and Visual Basic, C# and F# which are all dependent upon Windows, the Common Language Runtime and the .NET Framework. Platforms may impose dependencies upon C++, but C++ itself has none of its own.
What is a type cast in c plus plus?
A type cast is an override within an expression that causes the compiler to generate conversion code and to treat the item as if it had a different type.
int a = 13;
int b = 4;
float c;
c = a / b; /* result is 3, the integer value of 13 divided by 4 */
c = (float) a / b; /* result is 3.1, the floating value of 13 divided by 4 */
In this case, the (float) keyword was the typecast. Note also that it was not necessary to typecast b, because the compiler recognizes the mixed mode expression.
What is public access specifier?
An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:
1. Public
2. Protected
3. Default and
4. Private
Private is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object.
Java programming does not run by just a single piece of class that has the whole functionality. You have hundreds of classes that interact with one another, passing data between them and returning output to the user of the system. So it is very important for members of one class to access members of another. Here members may refer to variables, methods and even classes. So, this is where the access modifiers come into picture. The modifier associated with every member of the class determines what level of visibility that member has.
What is the constructors in class?
A class is simply the definition of a data type. A constructor is a special method of a class that is automatically invoked whenever an object of that type is instantiated, and is used to initialise the object's data members.
What is the difference between static and non static class members in java?
Static data members are different from automatic ones in the way that their lifetime is equals to the lifetime of your program. Even if you have declared static members inside of function (class) other than main();
What is the difference between a class and a function in C plus plus?
You cannot point at a class, you can only point at an instance of a class, which is simply another term for an object. The class is essentially the object's type; it define's the object's behaviour, but is not the object in and of itself. The class also defines a pointer's type, so we can point at instances of a class and access the the object it represents through indirection.
What is the importance of visual C plus plus?
C++ is simply a specification of the language while VC++ is a specific implementation of the language that should follow the specification but doesn't. The differences are relatively minor, but can cause problems porting code between compilers. Implementations that follow the specification are generally considered better as they are more compliant with each other. However, if all you want to do is write Windows programs, VC++ is as good a choice as any.
What is the difference between structure oriented and object oriented programming language?
the main difference is that structured programming deals with the flow of execution, and not, primarily, with the data. The mathematical basis for structured programming has to do with the elimination of arbitrary jumps (GOTOs) in favor of code blocks and functions. In particular, "information hiding" as it relates to data isn't fully developed in structured programming; structured programming has to do with the organization of the code, rather than the data, and pure structured programming passes data around in the form of function arguments (conceptually, "on the stack").
In contrast, object oriented programming primarily deals with data issues. The object/class paradigm promotes clean, flexible organization of data in the same way that structured programming promotes clean, flexible organization of code. In a pure object oriented approach, the flow of program execution is treated as bits of behavior associated with the packets of data that are "objects".
What programming framework does C plus plus use?
C++ doesn't use a framework; it is a general purpose, object oriented programming language derived from the C programming language. Specific implementations, such as Microsoft Visual C++, make use of frameworks.
How to write a program that converts inches into centimeters feet yards and meters in C plus plus?
#include <iostream>
using namespace std;
int main()
{
double meter, cent;
cout<<"Enter value in meters";
cin>>meter;
cent=meter*100;
cout<<meter<<" meters"<<" = "<<cent<<" centimeters;
system("pause");
return 0;
}
to convert more than one number put the whole program in a for loop