C plus plus file processing that will input 12 integers?
#include<iostream> #include<vector>
int main()
{
std::vector<int> integers (12);
for (size_t loop=0; loop<integers.size(); ++loop)
cin >> integers[loop];
}
What is difference between direct addressing and indirect addressing in c plus plus?
Indirect addressing uses a pointer. Indirectly accessing the memory being pointed at is known as dereferencing. Direct addressing uses a variable's name or a reference to obtain the value.
Can a constructor be declared as virtual?
A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.
##
Constructor called implicitly not explicitly so constructor is not virtual.
How many bytes are occupied by declaring following array of characters?
A single-byte type of array has 1 byte per character; a wide-character array has 2 bytes per character of storage. Without seeing the exact definition it cannot be determined what the actual size of the array would be.
Why are Logical errors harder to locate in C plus plus programs?
Logic errors in any computer language may be difficult to find. Languages that are object oriented can make it harder because you have to follow the object activations, which are separate from plain structural code.
However, it isn't any more difficult in C++ than some other OO languages to find logic errors; in all cases it can be difficult.
Why does an abstract class often define methods WITHOUT actually implementing them?
Because, it wants the implementing or child class to take care of the actual implementation details.
How do you check address of char variable?
Use the address-of operator:
char c=32; // space character
std::cout<<&c<<std::endl;
The cin and cout objects are iostream objects somewhat equivalent to stdin and stdout.
The equivalent of printf ("Hello World\n"); is cout << "Hello World" << endl;
The equivalent of scanf ("%d", &i); is cin >> i;
Is it possible to make a program that combines a lot of other programs?
Yes. One program can execute another program very easily -- you can even do it via scripting languages and batch programming. Morevoer, programs that provide shared code via one or more libraries make it possible for another program to execute code within those libraries, just as if that code were part of the program itself (no need to execute another program). Such programs often provide application programming interfaces (APIs) to simplify the process of integration.
An arity is a number of arguments or operands a function or operation takes.
How do you merge 2 Visual C programs to get a single output?
You cannot. A C program can only have one global main function but you'd be trying to compile a program that has two main functions. The only way to merge the two programs is to modularise both programs so that neither is dependent upon their main functions. Once modularised, you can include those modules in any program. Alternatively, you can create binary libraries from the modules and link them into any program.
What is platform dependency of c plus plus?
C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives.
For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation:
#ifdef __unix__
#include <unistd.h>
#elif defined _WIN32
#include <windows.h>
#endif
The definition of __unix__ and _WIN32 must be mutually exclusive.
Write any small program that will compile in C but not in C plus plus?
#include
int main (void)
{
char new [3] = "NEW";
printf ("new='%.3s'\n", new);
return 0;
}
in C++:
int main (void)
{
char newstr [4] = "NEW";
printf ("new='%.3s'\n", newstr);
return 0;
}
How do you calculate the total salary of an employee in c plus plus?
Not to be pedantic, but an employee's salary does not have to be calculated. It is a fixed rate, paid monthly but calculated annually. If you are contracted to work for £24,000 a year, then your monthly salary, before tax/insurance, will be £2000, regardless of how many hours you actually worked. At the end of each year you may be offered a bonus for exceeding your annual targets, but this is over and above your agreed salary.
Wages, on the other hand, are worked out by the number of hours worked, with increased rates for overtime and unsociable hours. In manufacturing environments, you may be paid an agreed "piece" rate for each item you produce. To work out the annual pay of waged employees you need to add their weekly or monthly pay over a 12 month period.
How you actually achieve that in C++ depends on how the data is collected. A database back-end will often be used to track the employee payroll, so you will generally need an SQL or similar query to collate the information for a specific employee by their payroll number (or NI number).
#include<iostream>
#include<sstream>
unsigned input_num (std::string prompt, unsigned min, unsigned max)
{
unsigned num = 0;
while (1)
{
std::cout << prompt << " (range " << min << " - " << max << "): ";
std::string input = "";
std::getline (std::cin, input);
std::stringstream ss (input);
if (ss >> num)
{
if (num >= min && num <= max)
break;
else if (num<min)
std::cout << "Index must be greater than or equal to " << min << std::endl;
else
std::cout << "Index must be less than or equal to " << max << std::endl;
}
else
std::cout << "Invalid input." << std::endl;
}
return (num);
}
void print_substring (std::string input, unsigned left, unsigned right)
{
std::cout << input.substr (left, right - left + 1) << std::endl;
}
int main()
{
std::cout << "Input string:\t";
std::string input = "";
std::getline (std::cin, input);
unsigned left = input_num ("Enter the start index of the substring", 0, input.size()-1);
unsigned right = input_num ("Enter the end index of the substring", left+1, input.size()-1);
print_substring (input, left, right);
}
#include<iostream>
struct shape
{
virtual double area () const = 0;
};
struct triangle : shape
{
triangle (double b, double h): base (b), height (h) {}
double base, height;
double area () const override { return base * height / 2; }
};
struct circle : shape
{
circle (double r): radius (r) {}
double radius;
double area () const override { return 4 * atan(1) * radius * radius; }
};
struct rectangle : shape
{
rectangle (double w, double h): width (w), height (h) {}
double width, height;
double area () const override { return width * height; }
};
int main()
{
triangle t (10, 5);
std::cout << "triangle with base " << t.base << " and height " << t.height << " has area " << t.area() << std::endl;
circle c (5);
std::cout << "circle with radius " << c.radius << " has area " << c.area() << std::endl;
rectangle r (10, 5);
std::cout << "rectangle with width " << r.width << " and height " << r.height << " has area " << r.area() << std::endl;
}
Why certain features of C plus plus may be version and machine dependent?
C and C++ allow access to low-level concepts such as pointers and manual memory allocation. It also runs on a wide variety of operating systems and architectures.
For example, an int on a 32-bit machine is usually 4 bytes, but on a 64 bit machine it may be 8 bytes.
Whether or not this matters depends on what tricks the programmer is playing. If a portion of the code relies on ints being 32 bits, and it's, say, 56 bits on your machine for some bizarre reason, that code may not work properly.
What should a function include?
A function must include an interface and an implementation. In some programming languages the two may be declared separately, particularly if the language is declarative.
The interface typically consists of the function's return type followed by the function name and the type of its arguments, if any, usually enclosed in parenthesis (often round brackets). In untyped languages, the return type and the type of arguments may be optional, but the arguments must be formally named while the return value usually has the same name as the function. In typed languages the types must be specified but the names are optional unless the interface and implementation are combined.
In typed languages that support function overloading, the function name and the number and type of the arguments form the function's unambiguous signature. The signature also includes the constness of the function and its arguments where supported, but does not include the return type nor any argument default values that may be provided.
The function's implementation must duplicate the interface (if declared separately) but must also formally name the arguments. The function body is usually parenthesised (often in curly braces).
That depends on what you want to use it for. An array offers constant-time random access to any element in the array, but is restricted by the fact it must be re-allocated in order to cater for new elements or to remove redundancy. Linked lists overcome this limitation at the expense of random access, which is linear-time other than for the first and/or last nodes which remain constant-time.
A C developer designs and writes programs using the C programming language. A C++ developer does the same but uses the C++ programming language. A C/C++ developer uses both C and C++.
What is the base class for all swing components?
The JComponent class of the javax.swing package serves as the base class.