What is a block in c plus plus?
Assuming you mean a code block, a group of simple statements enclosed in braces {} is regarded as a code block. In other words, a compound statement. Braces are not required around simple statements except when the statement is the body of a function (where braces are mandatory).
What are the advantage of function prototype?
It allows the compiler to verify that the number and types of the functions parameters are correct when the function is actually called.
If the function were called with the wrong number or type of parameters, and no function prototype were supplied, the error would not be discovered until the program was actually run.
How ploymorphism and inheritance is different from that in Java and c plus plus?
C++ allows multiple inheritance while Java does not. In my opinion, multiple inheritance is not useful because it can get very confusing very quick.
For polymorphism, C++ does early binding by default, while Java does late binding by default. Late binding is more useful than early binding.
Structures are the same as classes in C++. The only difference is that structure members are public by default while class members are private by default, but both define a class of object and both are initialised via a class constructor initialisation list.
struct my_object
{
my_object(const int data): m_data(data) {}
private:
int m_data;
};
The above struct is no different to the following class:
class my_object
{
int m_data;
public:
my_object(const int data): m_data(data) {}
};
Write a c plus plus program to demonstrate call by value and call by reference?
#include<iostream>
void byval(int y)
{
std::cout<<"byval: &y="<<&y<<std::endl;
}
void byref(int& z)
{
std::cout<<"byref: &z="<<&z<<std::endl;
}
int main()
{
int x=42;
std::cout<<"main : &x="<<&x<<std::endl;
byval(x);
byref(x);
return(0);
}
Example output:
main : &x=001DF800
byval: &y=001DF72C
byref: &z=001DF800
When you pass by value, the y argument has a different memory address to that of x. In other words, y is a copy of x (you pass the value of x, not x itself). Thus any changes made to y will have no affect upon x. Often this is desirable, but note that when passing a complex object by value, you will automatically invoke the object's copy constructor, which is often unnecessary. Pass by value should only be used when the function needs to modify the argument's value, but without affecting the argument that was passed.
When you pass by reference, the z argument has the same address as x. Thus z refers to x. In other words, z is an alias, an alternate name, for x, thus any changes made to the value of z will also change the value of x, since they are one and the same variable. If the function does not actually need to alter the value of the reference, then the reference should be declared constant. This assures the caller that the object's immutable members will not be altered by the function.
Note that you can also achieve pass by reference by passing a pointer to the variable. Pointers are always passed by value, thus the pointer will actually be a copy of the pointer you pass, but both pointers will still refer to the same memory address, thus the net effect is a pass by reference. However, if a pointer is guaranteed to be non-NULL, then it is more efficient to pass the address being pointed at by reference, since references can never be NULL (a NULL reference will invalidate your program).
To pass a pointer by reference, you need to add an extra level of indirection. Thus when passing a pointer to an int (int*), you need to pass a pointer to a pointer to int instead (int**). The pointer to pointer is passed by value, but the pointer to int is effectively passed by reference, thus allowing you to change the value of both the pointer and also what it is pointing at (unless one, the other, or both, are declared const).
A multithreaded program that generates the Fibonacci series using Pthreads?
#include<stdio.h>
#include<time.h>
#include<pthread.h>
#include<stdlib.h>
#include<sys/types.h> /* need to calculate which I will implement later */
void *fibr(void *n);
void *fibr_1(void *k);
signed long long int fibonacci(signed long long int);
int main(){
clock_t begin, end;
double time_spent;
pthread_t tid,tid1;
int result,result1;
signed long long int n=6;
signed long long int m=7;
result=pthread_create(&tid,NULL,fibr,&n);
if(result){
perror("pthread_create");
return 1;
}
result1=pthread_create(&tid1,NULL,fibr,&m);
if(result1){
perror("pthread_create");
return 1;
}
if(pthread_join(tid,NULL)){
perror("pthread_join");
return 1;
}
if(pthread_join(tid1,NULL)){
perror("pthread_join");
return 1;
}
printf("Fib value=%lld\n",n+m);
pthread_exit(NULL);
}
void *fibr(void *n){
signed long long int *y=n;
signed long long int x=*y;
pthread_t tid2,tid3;
signed long long int i,j;
/* How do I assign values to i , j in order to
achieve the level viz fib(n-2)....fib(n-4) */
if(pthread_create(&tid2,NULL,fibr_1,&i))
{
perror("pthread_create");
}
if(pthread_create(&tid3,NULL,fibr_1,&j))
{
perror("pthread_create");
}
if(pthread_join(tid2,NULL))
{
perror("pthread_join");
}
if(pthread_join(tid3,NULL))
{
perror("pthread_join");
}
/* How to return the values of i, j combined with *y . if I do *y+i+j, the result
is not coming correctly */
*y=fibonacci(x);
return NULL;
}
void *fibr_1(void *k){
long long int *a=k;
long long int b=*a;
*a=fibonacci(b);
return NULL;
}
signed long long int fibonacci(signed long long int x){
if((x==0)(x==1))
return x;
return fibonacci(x-1)+fibonacci(x-2);
}
What is dereferencing pointer?
They're related but not totally comparable.
A pointer is an address in memory where a variable is located.
An array is a conceptual data representation consisting of a list of more than one item of a particular scalar type (int, float, char, structure, etc.) where each element is accessed by its index. The index can be thought of as a counter or enumerator telling you how many elements you have to skip over to get to the one you're interested in. Here's where addresses come in ... the location of a particular element in memory is offset from the so-called base address (i.e. the address of the starting element) by the value
(sizeof(one element) * index #)
The C compiler is smart enough to take the sizeof() into account when you do pointer arithmetic, so you can find the address of the i-th element as (address of base) + i, rather than having to do the multiplication explicitly.
The idea of element address as offset also accounts for C's use of zero-relative array definitions. Since the first element is offset by 0 positions from the first element its index in C is 0, not 1. The second (ordinal) element is offset from the first by 1 position so its index is 1, and so on.
What is the use of private constructor in c plus plus?
Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.
What are advantages and disadvantages of priority queue?
In CQ we utilize memory efficiently. because in queue when we delete any
element only front increment by 1, but that position is not used later. so when
we perform more add and delete operation, memory wastage increase. But in CQ
memory is utilized, if we delete any element that position is used later,
because it is circular.
How are break points set using c plus plus?
Break points are a feature of the debugger and is implementation specific. For instance, in Visual C++, you place the cursor at the line where you want the breakpoint to be and hit the F9 key to toggle it on and off. When enabled, you can set the properties of the breakpoint, such as what condition(s) must be true in order for the program to break at that point.
Why we make a function friend in c plus plus?
A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class.
class foo {
friend void bar(foo&);
int m_data;
};
void bar(foo& f)
{
f.m_data=42;
}
In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function.
Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.
What is an instance class in C plus plus?
You have a class(i.g. MyClass):
class MyClass{
public:
int MyData;
};
And then you use the class like this:
int main(){
MyClass MyObject;
MyObject.MyData=7;
}
FIFO is the acronym for First In First Out, which means Use (or do) the Oldest Stuff First, or use things in the order of their arrival.
Two common contexts for this are in accounting and computing.
FIFO is also used in inventory and in stocking shelves where the items that are received first should be used first and old items are shelved in front and new items in the back.
It is a materials management technique in Warehousing, Fruit & Vegetable stores, Butcher shops, etc. and handling incoming mail in offices.
It is all about making sure things flow through a system properly, where older items are used before items that have just come in.
Technically, FIFO is a means of describing a queue-like data sequence, where insertions (push operations) occur at the end of the sequence and extractions (pop operations) occur at the beginning of the sequence. As opposed to a LIFO (last-in, first-out) sequence where all pushes and pops occur at the end of the sequence, thus creating a stack-like data sequence. LIFO can also be called FILO (first-in, last-out), which means the same thing.
In other words, with FIFO, we pop objects off the sequence in the same order they were pushed onto it, but with LIFO/FILO we pop objects in the reverse order they were pushed.
A c comma c plus plus program that can accept first name surname and display it?
int main()
{
std::string first, last;
std::cout << "Enter your first name: ";
std::cin >> first;
std::cout << "Enter your last name: ";
std::cin >> last;
}
Advantages of c plus plus than c?
Python is an interpreted high-level language and interpreters are available for Windows, Mac OS X and Unix. Thus the same code can be executed upon these different platforms with no changes to code. By contrast, C++ must be compiled separately for each platform and all machine-specific code must be filtered out of each compilation, which puts an increased workload upon the programmer.
As an interpreted language Python is more direct and convenient, it can be used interactively or can be used to build complete programs, without any of the lengthy compilation and linking procedures involved in C++.
Python's data types are high-level and extremely flexible, unlike the strong data typing that is strictly enforced by C++. As such there is no need to declare variables before using them. However, the flexibility comes at a cost in increased memory consumption.
The flexibility of the language and high-level nature of the data types means Python is much easier to learn than C++, and therefore more accessible to non-programmers.
The main disadvantage of Python compared to C++ is that it doesn't perform well. However, the same can be said of all interpreted languages, including Java. C++ will always perform better because code is compiled to native machine code, but requires more effort on the part of the programmer. However, the results are comparable to that of low-level assembly.
What are Class Constructor and Primitive data types?
class constructor is a function which has the same name as the class name and has no return type.
primitive data types are the fundamental data types which are independent.
eg:int,char,float etc..............
Difference between private and protected specifier in c plus plus language?
In C++, the private specifier means that the item can only be accessed by methods of the class, not including methods of derived classes. Protected, on the other hand, means the item can be accessed by methods of the class, and methods of derived classes. Public, to complete the explanation, means that the item can be acessed by any method, this class, another class, or otherwise.
How do you call base class method using derived class object?
If the base class method is non-private, the derived class can call the base class method implicitly. However, if the derived class overrides or overloads the method, the base class method must be called explicitly. The following demonstrates explicit calls to base class methods:
#include <iostream>
using namespace std;
class Base
{
public:
Base(){}
void Foo(){ cout << "Base::Foo" << endl; }
};
class Derived : public Base
{
public:
Derived(){}
void Foo(){ cout << "Derived::Foo" << endl; Base::Foo(); }
};
int main()
{
Derived derived;
derived.Foo();
derived.Base::Foo(); // Explicit call to base class method.
return(0);
}
Output:
Derived::Foo
Base::Foo
Base::Foo
What is abstract data type for C Plus Plus examples?
An abstract data type (ADT) exists purely to provide a common interface to all the objects that may be derived from it, without providing the full implementation of that interface. It is a concept rather than an actual object.
For instance, an Animal object is a fairly general class of object, while Cat and Dog are specific types of Animal. It does not make sense to allow users to create an instance of Animal, since it would impossible to provide an implementation for every type of animal that that class could possibly represent.
For instance, all animals make a noise, so it makes sense for the Animal class to have a MakeNoise() method. But we cannot implement this method since the Animal class would have to determine what type of animal it actually was before it could produce the correct sound. Animals simply do not do this in real life: a dog does not ask itself whether it is a cat or a dog before deciding that it should bark rather than meow!
So we declare Animal to be an ADT by making the MakeNoise() method pure-virtual. We can then derive a Cat object from our Animal ADT and implement the MakeNoise() method within the Cat object. We can do the same for the Dog class, a Mouse class, an Elephant class, and so on. Each specific object knows what sound it must make.
Since all these specific classes are derived from a common ADT, the Animal class, we can create collections of animals, without regard to their specific type. When we call an animal's MakeNoise() method, a Cat object will meow while a Dog object will bark, the Mouse will squeak and the Elephant will trumpet. We've effectively called a general method but got a specific method instead, without having to work out which specific method to call.
That is the essence of all ADTs; by creating a new data type from an ADT, we can provide a specific implementation for an already existing common interface. Clients of the ADT (such as a queue or stack) needn't know what data types it contains, nor what data types it could contain in the future because they already know the common interface of the ADT. If you decide you want a lion in your queue, derive one from the ADT and the client won't care: the MakeNoise() method will make the lion roar!
What is encapsulation in c plus plus?
Encapsulation is the means by which an object contains all the information required to use the object. The object's behaviour is entirely contained within and ultimately determined by the object itself. In other words, an object is a self-contained entity. More importantly, it is not necessary to know how an object works in order to use it, provided you are familiar with the object's interface.
As a real-world example of encapsulation, you probably use a mobile phone every day but unless you are a telephone engineer you are unlikely to know the internal workings of your phone beyond inserting a SIM card and perhaps replacing the battery. For everything else you will generally hire the services of an engineer. The phone's interface alone provides all the features you expect so you can make and receive calls, send texts and e-mail, update your diary, take pictures and videos, browse the internet and so on. Each of these features is itself an example of encapsulation, all of which is encapsulated within the phone itself. Indeed, modern interfaces are so intuitive that you probably didn't need to consult the manual; you probably discovered new features simply by playing with the phone. You can do all that without knowing how your phone actually works, let alone how you are able to send and receive millions of bits of information around the world every day. These are implementation details that are either encapsulated within the phone's software or by your service provider. You do not need to know these details in order to use the phone.
In C++, encapsulation is one of the four cornerstones of object-oriented programming (OOP). The other three are inheritance, polymorphism and abstraction. We usually associate encapsulation with information hiding, however nothing is actually hidden by encapsulation; if you have access to the source code you can easily expose the implementation details. Encapsulation is more about limiting access to the implementation details rather than hiding it. Hiding information is really a function of the compiler; if objects of a class are exposed from a binary library then you probably won't have access to the source code. But you will have access to the class interface through the library's header file and that's all you should really need in order to use an object of that class; the implementation details are safely hidden from view.
Encapsulation also relates to an object's physical representation which usually has very limited access or indirect access. That is, when we extract information from an object, we usually expect a copy of that information, not the information itself. There are exceptions, of course, however information that is vital to the internal workings of an object must never be exposed outside of the class. These are known as the class invariants and virtually all non-trivial classes will have one or more invariants. A string, for instance, has a very important invariant in the form of its internal character buffer (a memory resource). If that buffer were exposed outside of the class, we could easily invalidate the object by taking ownership of that buffer and assigning one of our own. That must never be allowed to happen, thus the buffer is encapsulated by the string and all access to it strictly monitored by the string. It "owns" the resource and only the string should have direct access to it. Even though we have no direct access to the buffer, we can still modify it through the string's interface. The interface ensures that the object remains in a valid state, but allows us to work with the string in a predictable manner. We don't really care how or even where the string is physically stored, only that we can work with it.
Since objects are encapsulated, we can embed objects within other objects (a technique known as composition). Since objects know how to behave themselves and maintain their own validity, we can safely expose them outside of the class, but if we need to control how those objects are manipulated we can declare them private and provide our own more specialised mutators (setters) instead. These act as gatekeepers to ensure the embedded objects remain valid (with respect to our composition) and delegate to the embedded object only when a mutation is deemed valid.
What are the similarities between java and c plus plus?
Java is related to C and C++ in the structure of programs composed with each language. All of them are object-oriented-programming languages (oop languages).
Java is meant primarily for web apps
C is meant primarily for programming servers
C++ is meant primarily for large applications bbb
What is difference between structure and class?
The struct default access type is public. A struct shouldtypically be used for grouping data.
The class default access type is private, and the default mode for inheritance is private. A class should be used for grouping data and methods that operate on that data.
In short, the convention is to use struct when the purpose is to group data, and use classes when we require data abstraction and, perhaps inheritance.
In C++ structures and classes are passed by value, unless explicitly de-referenced. In other languages classes and structures may have distinct semantics - ie. objects (instances of classes) may be passed by reference and structures may be passed by value.
Technically there are only two differences between classes and structures:
Most C++ programmers use structures exclusively for data that doesn't require strict validation while classes are used to encapsulate data and the functions that work exclusively with that data. Although classes and structures can largely achieve the same goals, the lack of encapsulation and data-hiding in a structure make it far less robust than a well-designed class would be, with little or no difference in terms of memory consumption. Encapsulation comes into its own when classes become highly-complex (classes within classes) as each class is responsible only for its own data, not the classes it contains. Structures can be just as complex, but because they don't have the safeguards that can built into classes, it only takes one errant function to unwittingly invalidate data. A well-designed class ensures data validity at all times.
How is polymorphism achieved at compile time?
Polymorphism means "having many forms", and is used to mean that two different object types (ex. LinkedList and ArrayList) can be used as if they were the same type.
In Java polymorphism takes the form of subclassing: Make both LinkedList and ArrayList inherit from List (extend a common superclass or implement a common interface), and the code can call List methods without caring about which particular type of list it is working on.
This is polymorphism since two different types (LinkedList and ArrayList) can be treated as one type (List).
In duck-typing, used for example in Ruby and JavaScript, types need not have a common supertype to be treated the same, they need only have methods with the same name. (Hash, String and Array might all have a method "size". If so, code that uses only this method can treat all three types as the same.)
Both of these mechanisms let the programmer treat different types as the same type, so both are examples of polymorphism.
How do you create our own header file in c plus plus?
1. open word processing software. (Notepad or wordpad)
2. Write the desired function for the header.
3. Save it with .h as extension. Like "myheader.h"
4. Run cpp.
5. It should be like this... #include "myheader.h" 1. A header file is not any different to the compiler than ordinary code
2. Its just a convenient way to have commonly "included" code
3. It is primarily used for prototypes, not actual code, such as declaring an API
4. Be care with redeclarations. The #ifdef / #endif pragmas can help
5. The location of "user defined" headers is different than system headers
6. They are normally in the build directory, whereas system headers are elsewhere
How do you make a C plus plus program that arrange the numbers from highest to lowest?
Use the std::vector template class (in header <vector>) to store the numbers, then apply the std::vector.sort() algorithm. The default sort order is ascending. The elements must support the less-than operator (<). All the built-in primitives (int, char, double and float) already support this operator.