A array within a class in C++ is no different than any other type of attribute in a class...
class myclass {
private:
int someInteger;
int somArray[10];
int *somePointer;
public:
myclass() { ... constructor ...}
myclass(const &myclass()) { ... copy constructor ...}
~myclass() { ... destructor ...}
mymethods() { ... methods, etc. ...}
private:
myhelpers() {... helpers, etc. ...}
}
Recall that arrays and pointers are tightly related. Well, in classes, also recall that their memory must be explicitly handled or you are going to have all sorts of problems. You need constructors and destructors, as well as copy constructors, to properly make deep copies and deallocations if arrays area in the form of pointers.
What are the differences between thread and function in C plus plus?
As people do computer has head to think and hands to work. Under the control of it's head(CPU), you can consider threads as its hands. A computer has more than one hand, every hand would do one thing independently but have to wait its order. Function, as it demonstrate by the name, means what task a software can conduct.
What is the abstract feature in c plus plus?
Abstraction generally refers to abstract classes. An abstract class is one that cannot itself be instantiated, but can be derived from. Abstract classes are conceptual classes rather than concrete classes, and are intended to provide a common interface to the concrete classes derived from them. A class becomes abstract when it contains one or more pure-virtual methods, which must be implemented in the derived classes otherwise they become abstract classes themselves. A concrete class is one that provides a complete implementation for is abstract base classes, or inherits implementations from base classes other than those that originally declared the function to be pure-virtual.
An example of an abstract class is Shape. All shapes can be drawn, therefore Shape will have a Draw() method. However, it cannot implement this method unless it knows what type of shape it actually is. Therefore the Draw() method should be declared pure-virtual, thus rendering the Shape class to be abstract. You can then derive concrete shapes from the Shape class, such as Circle and Square classes, which will each provide the actual implementation for the Draw() method. Since Square and Circle are types of Shape, you can create collections of Shape objects and call their Draw() methods without regard to their actual type. If the object is really a Square, then calling Shape.Draw() will actually invoke Square.Draw(). There is no need to determine the actual type of the shape because every shape has a common interface.
This is really no different to overriding virtual methods in non-abstract classes. The main difference is that one would not be expected to instantiate a Shape (because it is abstract), other than through derivation, and the pure-virtual methods MUST be implemented by the derived classes, whereas normal virtual methods are simply expected to be overridden but it is not a requirement to do so unless you require an enhancement to the base class method.
Abstract classes typically have few if any member variables and little or no implementation. Ultimately, the class exists purely to provide a common interface to all its derivatives, and only functionality and member variables that are common to all its derivatives should be placed within it. Even if the abstract class provides a complete implementation for all its pure-virtual methods, you cannot instantiate an abstract class -- it must be derived from -- and all pure-virtual methods must still be implemented in the derivatives, even if only to call the base class methods explicitly.
Making multiplication table in c plus plus using do while loop?
Um, not sure how to do that, but you can create a sort of "table" in C++ by using multidimensional arrays. Below is an example of how to create a two-dimensional array:
int myArray[10] [10];
You can add more dimensions to the array in order to increase its storage capacity.
Difference between array and pointers in c plus plus?
A pointer is simply a variable that can store a memory address and has the same purpose in both languages. The only real difference is that C++ pointers can point at objects (instances of a class) and indirectly invoke their methods, whereas pointers in C (which is not object oriented) cannot.
How reliable is visual c plus plus?
That depends on what you mean by reliable. If you mean does it conform to the C++ standard then no, it is not reliable. Look up the 'Microsoft specific' headings under each topic in the help file. In most cases these are enhancements to the standard, but there are a few diversions from the standard you need to watch out for, particularly if you intend to port your code to another C++ implementation.
If you mean can it be used to write reliable programs then yes, it can. However, reliability in this sense is mostly your own responsibility as programmer. The code provided by Microsoft is, for the most-part, reliable. Microsoft use the same code in-house so if a problem can be replicated then a hotfix or workaround is usually not far behind.
There are three ways to sort a two-dimensional array. We can sort each row so that the rows remain where they are but the elements within each row are sorted. Or we can sort the rows themselves so the elements within each row remain where they are but the rows are sorted. Or we can do both, sorting the elements in each row and then sorting the rows. The following program demonstrates all three methods using the same bubble-sort method (see the sort function near the top of the code).
Keep in mind that a two-dimensional array is nothing more than a one-dimensional array of one-dimensional arrays. That is, array A[2][3] is a one-dimensional array of 2 elements where each element is a one-dimensional array of 3 elements. In other words, 2 rows with 3 elements per row. However, while static two-dimensional arrays must have the same number of elements in each row, dynamic two-dimensional arrays can have variable length rows. This is achieved by maintaining a one-dimensional array of pointers on the stack or on the heap, where each pointer refers to a separate row. The elements within each row must be contiguous, but the rows themselves can reside anywhere in memory, non-contiguously, and each row can have a different length. If the rows are the same length then you can access the individual elements just as you would with a static array using array subscripts. However, with variable length rows, you must dereference the row and determine its length before accessing the elements within that row.
The program below uses vectors rather than arrays, but a vector is really nothing more than a one-dimensional dynamic array that knows its own size at all times. Vectors are therefore much easier to work with than arrays because all the memory allocations are done for us behind the scenes. A two-dimensional vector is nothing more than a vector of vectors which is easier to imagine as begin a vector of rows, where each row has variable length. However, the example below uses fixed-length vectors to implement an array of 5 rows with 4 elements per row.
Due to the fact that every row must be contiguous within itself and that rows may have variable length, it is always going to be easier to sort two-dimensional arrays by row rather than by column. If you need to sort by column rather than row, rather than write a separate and needlessly complex function to cater for this, it is simpler to just orient your array in memory or on disk so that you can sort by row instead. Note that it is trivial to change the orientation of an array whilst printing the array (simply swap the inner and outer loops) so there is no need to physically store the data in the same orientation as the output. The only thing that actually changes is how you visualise the data: whether as a vector of horizontal rows (actual rows) or as a vector of vertical rows (actual columns).
#include<iostream>
#include<iomanip>
#include<vector>
#include<random>
#include<time.h>
template<typename T>
void sort (std::vector<T>& A)
{
unsigned unsorted = A.size();
if (unsorted > 1)
{
do
{
unsigned new_size = 0;
for (unsigned index=1; index<unsorted; ++index)
{
if (A[index]<A[index-1])
{
std::swap (A[index], A[index-1]);
new_size = index;
}
}
unsorted = new_size;
} while (unsorted);
}
}
template<typename T>
void print_array(std::vector< std::vector<T> >& arr)
{
for (unsigned row=0; row<arr.size(); ++row)
{
for (unsigned col=0; col<arr[row].size(); ++col)
{
std::cout << '\t' << arr[row][col];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
template<typename T>
void randomise_array(std::vector< std::vector<T> >& arr)
{
// Pseudo-random number generator (range: 1 to 50).
std::default_random_engine generator;
generator.seed ((unsigned) time (NULL));
std::uniform_int_distribution<unsigned> distribution (1, 50);
for (unsigned row=0; row<arr.size(); ++row)
{
for (unsigned col=0; col<arr[row].size(); ++col)
{
arr[row][col] = distribution (generator);
}
}
}
int main()
{
// Instantiate a 5x4 array
std::vector< std::vector<unsigned> > original (5);
for (unsigned row=0; row<5; ++row)
original[row].resize (4);
randomise_array(original);
// Always work from a copy of the original
std::vector< std::vector<unsigned> > copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort each row individually:\n" << std::endl;
for (unsigned row=0; row<copy.size(); ++row)
sort (copy[row]);
print_array(copy);
copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort the rows:\n" << std::endl;
sort(copy);
print_array(copy);
copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort each row individually and then sort the rows:\n" << std::endl;
for (unsigned row=0; row<copy.size(); ++row)
sort (copy[row]);
sort (copy);
print_array(copy);
}
Example output:
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort each row individually:
5 8 14 43
9 12 15 49
4 27 31 35
6 11 15 37
4 32 35 42
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort the rows:
15 11 37 6
15 12 49 9
31 27 4 35
32 42 35 4
43 14 8 5
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort each row individually and then sort the rows:
4 27 31 35
4 32 35 42
5 8 14 43
6 11 15 37
9 12 15 49
What is meant by this pointer in C plus plus?
The this pointer is a hidden argument that is automatically generated by the compiler to refer to the current instance of an object.
Consider the following snippet:
class foo{};
void bar(foo* f){
std::cout< } int main() { foo f; bar(&f); return(0); } The function, bar, knows which instance of foo it is working on because we must explicitly pass a pointer to that instance (the address of f). However, if bar were a member of foo, we do not need to pass a pointer to the instance because the compiler does it automatically for us: class foo { void bar(){std::cout< }; int main() { foo f; f.bar(); return(0); } Although the second example is what we would physically write, it's really just sugar-coating. Behind the scenes, it's as if bar() were a non-member function and is actually implemented as per the first example, where we passed the address of the instance into the function. This is because there is only ever one instance of the bar function (regardless of how many instances of foo we create), and the compiler needs some way of determining which instance of foo the bar function should work with -- thus it generates a this pointer for us and hides the actual implementation behind sugar-coated code. The hidden this pointer is also used implicitly (behind the scenes) whenever you call member methods from within other member methods of the same class. However, you can invoke methods explicitly by dereferencing the this pointer if you want to. For instance, to invoke the bar() method from within another method of foo, you'd use the following call: (*this).bar(); More typically, you'd use the points-to operator instead: this->bar(); But since the this pointer is implicit, you don't need to explicitly dereference members like this unless it helps to clarify your code (such as when a member method operates upon other instances of the same class, and specifying this helps to clarify exactly which members you are invoking upon which instances). However, the this pointer really comes into its own when you actually need to refer to the current instance, such as when performing self-reference checks and returning references to the current instance. A classic example is the assignment operator which typically requires both: class foo { public: foo& operator=(const foo& rhs) { if( this != &rhs ) { // perform assignment } return(*this); } };
What is the only function all C plus plus programs must contain?
Every C plus plus program that is a main program must have the function 'main'.
Why need to declare header files compulsory in c plus plus but not in c?
The need to declare header files is not compulsory in C++. You may place all your code in a single source file if you so desire. However, header files are useful in that they separate interface from implementation and aid in hiding information.
How does virtual function support run time polymorphism?
Virtual functions are used to suport runtime polymorphism.In C++,if we have inheritance and we have overridden functions in the inherited classes,we can declare a base class pointer and make it to point to the objects of derived classes.When we give a keyword virtual to the base class functions,the compiler will no do static binding,so during runtime ,the base class pointer can be used to call the functions of the derived classes.Thus virtual functions support dynamic polymorphism.
Write a program to concatenate two strings in C plus plus?
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main()
{
string firstLine = "";
cout << endl << "Enter first line: ";
getline(cin, firstLine);
string secondLine = "";
cout << endl << "Enter second line: ";
getline(cin, secondLine);
cout << endl << "Two strings concatenated together: " << firstLine + secondLine << endl;
system("PAUSE");
return 0;
}
How data hiding is possible within a class in c plus plus?
A class is a description of an object, its attributes, and its methods. Its not a lot different than primitive types, such as int. int a; /* instantiate object of type int and call it a */ person b; /* instantiate object of type personand call it b */ The difference is that person is declared in a class specification, and is potentially much more complex than int. Still, you can think of both in the same context. The attributes (member variables) of a class contain the state of each instance of that class. You can (and usually do) declare those attributes as private, which means that only methods of the class can access them. This is data hiding. With data hiding, you can encapsulate the functionality of a class, exposing only the needed public interface. This way, if you need to change the way a class works internally, such as storing the person's name in unicode string instead of char string, that change can, if the interface is correct, be totally transparent to the user of the class.
What is the difference between C programming and CPP programming?
C has no object oriented support. C++ is a superset of C that adds object-oriented support, but retains the concept of primitive variables. C# is a superset of C++, developed by Microsoft, that removes the concept of primitives, making it fully object-oriented but, unlike C and C++, is non-generic and is only useful for .NET programming. C# is not unlike Java, but Java is fully cross-platform and therefore has much wider support.
What are virtual classes in c plus plus?
A local class refers to a class which has been defined within a function.
For example, consider a function A :
int A(int d)
{
class X
{
int a,b;
void func();
public :
int func2()
{
//do something
}
}
//rest of the function.
}
Class X is local to the function A, and can only be instantiated inside the function A. Use of such functions are usually indicative of poor function design.
Write a program to add two integers using c plus plus?
#include<iostream>
int main()
{
int a=40;
int b=2;
std::cout<<a<<'+'<<b<<'='<<a+b<<std::endl;
}
Output:
40+2=42
How do you access memory dynamically in c plus plus?
A pointer is a variable that can be used to store any memory address, including the null address (represented by the nullptr value). To access any non-null memory address via a pointer, simply dereference the pointer.
template<typename T>
void f (T* p) {
if (p==nullptr) return; // sanity-check
std::cout<<"1. The address of p: 0x" << std::hex << &p << std::endl; std::cout<<"2. The address pointed to by p: 0x" << std::hex << p << std::endl;
std::cout<<"3. The value pointed to by p is: " << *p << std::endl;
}
In the above example, output 3 shows how to dereference a pointer.
Note that this example will only compile if std::ostream::operator<< is overloaded to handle a type T. All primitive data types such as int and float are supported by default but all user-defined types require an explicit overload.
What is the difference between static data member and ordinary data member?
Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.
Which loop does not have an entry condition in C plus plus?
The only loop that does not require an entry condition is the procedural goto loop:
again:
/*...*/
goto again;
Although a do-while loop has no entry condition per-se, it still requires a mandatory entry condition into the second and all subsequent iterations.
do { /*...*/} while (true); // mandatory entry condition into all but the 1st iteration
And although a for loop's condition is optional, it is implicit:
for (;;) {/*..*/} // implicit for ever loop
for (;true;) {/*...*/} // explicit for ever loop
Write a program to print all two digit twin prime numbers?
#include
#include
void
main()
int prime(int n)
{
int i, j=n/2;
clrscr();
for(i=2; i<=j; i++)
{
if(n%j==0)
return 0;
}
return 1;
}
void
main()
{
int a,b,i;
clrscr();
printf("\n enter the twin prime numbers");
scanf("d",&a,&b);
printf("\n twin prime within this range \n");
if(a%2!==0)
i=a+1;
else;
i=a;
while(i+2<=b)
{
if(prime (i) &&prime(i+2) )
printf("\n%d\t%d",i,i+2);
i+1=2;
}
getch();
}
#include
#include
void main()
{
Int i, j, C;
printf("Enter the no");
Scanf("%d", &n);
for(j=2; j { If(n%j==0) { c++; } } if(c==0) { printf(" Entered no is prime no"); } getch(); } #include #include #include void main() { int i,j; clrscr(); for(i=3;i<=1000;i++) { for(j=2;j<=i;j++) { if(i%j==0) break; } if(i==j) cout< } getch(); // this is the easiest method for finding prime nos made by Taabi } B: To write the c version: i.e not C++, you write: A: /****************************** * THIS IS THE EASIEST * METHOD OF GENERATING * PRIME NUMBERS USING C * MADE BY: githambo@gmail.com *******************************/ #include
When was c plus plus developed?
Microsoft VC++ 1.0 was released in February 1993, about 10 years after Microsoft C 1.0 first appeared. The 32-bit version was released in December 1993 while the 64-bit version wasn't released until Visual Studio 2005 came out.
C program to swap three variables?
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=3;
swap(a,b);
printf("%d%d",a,b);
getch()
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("%d%d",x,y);
}
What is a simplex data communication mode?
One way data communication.
http://en.wikipedia.org/wiki/Simplex_communication
Parts of the program C plus plus?
parts of a program
Structure of C++ program
Documentation Section
Preprocessor Section
Definition Section
Global Declaration Section
main()
{
Declaration part;
Executable part;
}
sub program section
{
Sub program execution part
}
Write a C function to sort two dimensional integer array in ascending order?
Here's an example using a bubble sort. Note that this is not a very efficient sort, but it works weel enough with small data sets.
#include
#include
int main(void)
{
int item[100];
int a, b, t;
int count;
/* read in numbers */
printf("How many numbers? ");
scanf("%d", &count);
for(a = 0; a < count; a++)
scanf("%d", &item[a]);
/* now, sort them using a bubble sort */
for(a = 1; a < count; ++a)
for(b = count-1; b >= a; --b) {
/* compare adjacent elements */
if(item[ b - 1] > item[ b ]) {
/* exchange elements */
t = item[ b - 1];
item[ b - 1] = item[ b ];
item[ b ] = t;
}
}
/* display sorted list */
for(t=0; t
return 0;
}