answersLogoWhite

0

📱

C++ Programming

Questions related to the C++ Computer Programming Language. This ranges all the way from K&R C to the most recent ANSI incarnations of C++, including advanced topics such as Object Oriented Design and Programming, Standard Template Library, and Exceptions. C++ has become one of the most popular languages today, and has been used to write all sort of things for nearly all of the modern operating systems and applications." It it a good compromise between speed, advanced power, and complexity.

2,546 Questions

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;

}

What is FIFO approach?

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:

  1. classes are declared using the keyword class while structures are declared using the keyword struct
  2. structures are entirely public, while classes are private by default

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.

Is NET in support of object oriented language?

Yes, every language supported by Microsoft and on the .NET framework is an object oriented language. (OOP)

Why is C plus plus called an object oriented programming language?

Any language that supports class types, private and protected data, inheritance, polymorphism, function overriding, virtual methods is regarded as an object oriented programming language. However, while C++ supports OOP, it does not rely on it. You can mix C++ and C-style code (non-OOP) in the same program.

What is difference between c plus plus and data structures?

Data structure is nothing but a way to organize and

manipulate any given set of data in a specific and reusable

format/structure hence simplifying the manipulation of data.

Some of the commonly and frequently used data structures

are Stack, Queue, List, Set, e.t.c. But this list is not

limited to what we see here, rather we can invent our own

as long as there is a definite structure and better

efficiency by using it than work with raw data.

What are classes and objects in Cpp?

A class is the definition of an user-defined type while an object is an instance of a class.

class foo {}; // definition of a type (implementation omitted for brevity).

struct bar {}; // another definition of a type

foo x; // x is an instance of the foo class, therefore x is an object

bar y; // y is an instance of the bar class, therefore y is an object

Note that in C++, struct and class are exactly the same (they are both classes), the only difference being that members of a class are private by default while members of a struct are public by default. Thus the following definitions are exactly the same:

struct X {

Y(){} // public by default

};

class Y {

public:

X(){} // public by specification

};

As are the following:

struct A {

private:

int m_data; // private by specification

};

class B {

int m_data; // private by default

};

What is different between object and variable in c plus plus?

A variable is simply a named value where the value is not constant -- meaning it can be changed at any time.

A pointer is a variable (or a constant) that can store a memory address and provides indirect access to that memory address. It is called a pointer because it is said to "point to" the memory address it contains. A pointer that doesn't actually point at anything should store the value zero (represented by the constant NULL). When you allocate memory dynamically, you must maintain at least one pointer to that memory in order to release the memory back to the system, which is achieved by deleting the pointer, and then nullifying the pointer, or pointing it to another allocation. Pointers can also be typed, such that the memory address being pointed is treated as if it were that type, thus a pointer to a char can be used to determine the value of each individual byte in a multi-byte value, such as an int, simply by incrementing its stored address to traverse the individual bytes. By the same token an array of 4 bytes can be treated as if it were a single 32-bit value, simply by pointing at the first byte using a pointer with type int. Note that a pointer can only physically point at one byte at a time, but its type allows you to access one or more contiguous bytes according to its type, thus incrementing the pointer's stored address will advance the pointer by the size of its type. Thus a pointer to int will automatically increment (or decrement) by sizeof(int) bytes.

Pointers can also point at other pointers, adding extra levels of indirection. This is most useful when you need to pass a pointer to a function by reference, since all pointers are always passed by value. However, passing a pointer to a pointer is the same as passing the pointer being pointed at by reference, which allows the pointer itself to be manipulated, not just what it points at. Pointer to pointer types are also useful when allocating large dynamic arrays as they allow the array to be allocated non-contiguously as a series of smaller, separate arrays. This uses more memory overall, but allocating many smaller arrays is far more likely to succeed where a large contiguous allocation could easily fail if a large enough block of free memory is not available.

Is there any program used to create c plus plus program?

There are several: an editor, a compiler and linker being the three main programs. Typically you will install an integrated development environment (IDE) that includes all three plus project management and debugging utilities, help compilers, installation helpers, and a raft of plug-ins to suit the platform you are developing for.

What is the use of iostream in c plus plus programming?

iostream.h is deprecated and should not be used. It was originally shipped with early versions of C++ which used CFront to produce C compatible code from C++ source. Later, when C++ was standardised, iostream.h became iostream (without the .h extension), but many implementers shipped with both versions. However, iostream.h is not standards compliant and should not be used today. In most cases the file can simply be deleted unless you have non-compliant legacy code that requires it.

What is friend class in c plus plus?

A friend class is any class that is granted access to the private members of another class. The following minimal example demonstrates a simplistic friend class.

#include<iostream>

class A {

friend class B;

public:

A(int data=0):m_data(data){}

private:

int m_data;

};

class B{

public:

B(const A& a):m_data(a.m_data){}

int getdata()const{return(m_data);}

private:

int m_data;

};

int main()

{

A a(50);

B b(a);

std::cout<<b.getdata()<<std::endl; // prints 50

return(0);

}

In the above example, A's encapsulation is such that A:m_data can be initialised through A's constructor but is otherwise inaccessible outside of the class. The assumption is that the A::m_data is intended for internal use only. However, B's encapsulation is such that it can be constructed from an instance of A, but since there is no public accessor to A::m_data, B requires friend access to A, and only A can grant that access, hence class B is declared a friend of A.

Note that friends can be declared any where in a class declaration since private, protected and public access do not apply. Note also that there is no need for a forward declaration of B before declaring A because B is dependant upon A, not A upon B.

It should also be noted that although B could potentially undermine the encapsulation of A (a common complaint where friends are concerned), the assumption is that since you are in control of both A and B, then it is your responsibility to ensure that B obeys the same encapsulation rules you set out within A. That is, if A provided a private A::getdata() accessor and a corresponding A::setdata() mutator for its own internal use, then B should make use of that same accessor and mutator rather than access A::m_data directly. This ensures that should you alter the implementation of A::m_data, you will not affect the inner workings of B, thus enforcing A's encapsulation rather than undermining it, and therefore minimising maintenance.

Friend classes are often used wherever two classes work closely together. For instance, a parent and child class often work in tandem, thus it can often make sense to for the child class to declare the parent class a friend. However, there are often better ways than declaring an entire class to be a friend of another class. Friend functions (which can include specific class methods) greatly reduce the chances of undermining encapsulation by limiting the exposure of your private class members. Thus instead of declaring the entire parent class to be a friend of the child class, you could simply declare specific functions to be friends of both classes, thus those functions provide the "glue" that binds the two classes together.

Undermining encapsulation is always a danger with friends, thus you should never grant friendship unnecessarily. After all, the onus is upon you to enforce your own encapsulation rules -- the compiler cannot help you any more than it can help you enforce those rules within the class itself. However, if the public interface is sufficient for your needs, then you do not need friends. You should only use friends when the public interface is insufficient and where altering the interface to suit would actually do far more to undermine the encapsulation than a friend would.