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

If a data-item is declared as a protected access specifier then it can be accessed?

A class method or attribute (data item) that is declared protected can be accessed only by methods of the same class or by methods of derived classes of the class.

How do you write a C plus plus function lastLargestIndex that returns the index of the last occurrence of the largest element in the array?

int lastLargestIndex(int a[],int n) //a=array, n= number of elements in array

{

int max=a[0],maxp=0; //max=largest no., maxp= position of largest no.

for(int i=0;i<n;i++)

if(a[i]>=max)

{

max=a[i];

maxp=i;

}

return maxp;

}

What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.

What is the value of separating interface from implementation in object oriented programming?

Say for example you had the following files:

  • interface.h
  • interface.c
  • interface_user.c
The code in interface.c can be altered in any way you want without having to change interface_user.c, as long as it complies with the specification in interface.h.

How can you display a circle on computer screen with the help of c plus plus programming language?

You cannot. At least not with generic C++. Graphics are platform-dependent and therefore requires a suitable API and library for your specific platform and its hardware.

What math levels do i need to become a good computer programmer?

If your programming is purely recreational then you don't really need specific maths levels though something equivalent to a Maths GCSE would help. If it's something you want to do further than purely fun like at a university/college level then many places ask for a Maths A-Level. Really it depends on what you are programming though some degree of higher Maths education will really help.

Purpose of void database in c plus plus?

There is no such term in C++. You probably meant void datatype. Void simply means "no type" and is primarily used as a place-holder for functions that do not return a value, since all functions must return something even when they return nothing at all. Not to be confused with void* which is a pointer to any type which, if non-null, must be cast to the correct type before being dereferenced.

What is the difference between basic data types and derived data types and user defined data types in C plus plus?

By basic types you presumably mean primitive types or built-in types. These include char, int, long, short, wchar_t, float, double and bool, amongst others. Most are simply variations of each other, but their lengths are implementation dependant. The only exception is char which is always 1 byte in length.

User-defined types are those you yourself define or are defined for you. These include typedefs, enums, classes, structs and unions, but can also include some implementation-specific built-in types and all third-party types. Regardless, all user-defined types build upon the primitive data types or other user-defined types. In the case of class and struct types, methods (or member functions) can be associated with those types, thus combining data and the specific methods that act upon that data into a single entity. Objects are specific instances of a class or struct.

A derived type is a class (or struct) which inherits from another class (or struct). A derivative cannot inherit from a primitive, enum or union. Derived types are also, by definition, user-defined types.

How will you fix your codes in c plus plus when you don't have the program?

Editing code is easy, they're simply plain text files you can edit with any text editor. However, you lose the syntax engine and all the external headers and libraries required by the code, not to mention the compiler and the linker that are required to actually build the code. Migrating code to another compiler is the only option if you don't have access to the original compiler.

Create a class rectangle The class has attributes length and width each of which is defaults to 1 It has member functions that calculates the perimeter and the area of the rectangle it has set?

public class Rect {

private int width, height;

public Rect() {

width = height = 1;

}

public Rect(int w, int h) {

width = w;

height = h;

}

public int getWidth() {

return width;

}

public int getHeight() {

return height;

}

public void setWidth(int w) {

width = w;

}

public void setHeight(int h) {

height = h;

}

public int getPerimeter() {

return 2 * (width + height);

}

public int getArea() {

return width * height;

}

}

What is the return type of and operator in c?

There are two AND operators in C, logical AND (&&) and bitwise AND (&).

The return type for logical AND is always bool, however logical AND only works when both operands can be implicitly cast to bool. The value 0 is always regarded as being false while all non-zero values are regarded as being true.

The return type for bitwise AND is that of the largest of its two operands. For instance, the return type of int & char is int (same as the l-value) while the return type of char & int is also int (same as the r-value).

Why you use loop in arrays in c plus plus?

We use loops with arrays because it's one the simplest methods of traversing an array. since each element in an array is the same size, every element can be access via an offset from the start of the array, using the array suffix operator []. Arrays employ zero-based offsets because the first element is always at the same address as the array itself. Thus the first element is at offset [0], while the third is at offset [2]. What this means is that if we multiply the size of an element by its offset index, we can determine the address of that element and therefore access that element's value. The subscript operator does this for us automatically, thus giving us constant-time random access to any element in the array. We can also use pointers to manually calculate the address of an element (which is what actually goes on behind the scenes). However, when we wish to traverse the array, one element at a time, a loop is the simplest method of doing so. The loop simply iterates through all the offset indices, beginning with offset 0, then 1, and 2, and so on. The final index is always 1 less than the number of elements in the array, because arrays are zero-based.

What do you meant by static and dynamic modeling?

Static modeling is used to specify structure of the objects that exist in the problem domain. These are expressed using class, object and USECASE diagrams. But Dynamic modeling refers representing the object interactions during runtime. It is represented by sequence, activity, collaboration and statechart diagrams

Who discovered MS Visual C plus plus 6.0?

Nobody discovered MS VC++ 6.0. It was developed by Microsoft (hence the abbreviation MS) and was first released by them in 1998. It has been superseded several times since then, with VC++ version 7 (2002), 7.1 (2003), 8 (2005), 9 (2008), 10 (2010) and now version 11 (2012).

Difference between structure and class?

Structure members are public by default while class members are private by default. Classes encapsulate the data and the methods that operate upon that data into a discrete package (an object), exposing only as much or as little interface as is required by the class itself, to ensure the data remains in a valid state at all times. Structures have no such protection.

Program to find odd number up to range in c plus plus?

#include <iostream>

using namespace std;

int main()

{

for(int i = 0; i <= 100; i++)

{

if(i % 2 != 0)

{

cout << i << endl;

}

}

char wait;

cin >> wait;

return 0;

}

Write a c plus plus program to create a class float that contain one float data member overload all 4 arithmetic operator so that they operate an the object float?

/ circarea.cpp // demonstrates floating point variables #include <iostream> //for cout, etc. using namespace std; int main() { float rad; //variable of type float const float PI = 3.14159F; //type const float cout << "Enter radius of circle: "; //prompt cin >> rad; //get radius float area = PI * rad * rad; //find area cout << "Area is " << area << endl; //display answer return 0; } */ void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; float rad; do{ cout<<"Enter radius of circle: "; cin>> rad; cout <<"Area is "<<circarea(rad); cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); } float circarea(float radius) { return 3.14159F*radius*radius;} Code:

/**2. Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power() that takes a double value for n and an int value for p, and returns the result as a double value. Use a default argument of 2 for p, so that if this argument is omitted, the number n will be squared. Write a main() function that gets values from the user to test this function.*/ #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; double n; int p=2; do{ cout<<"Enter n: "; cin>> n; cout<<"Enter p: "; cin>> p; cout <<"The power is "<<power(n, p); cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); } double power(double n, int p){ for(int ret=1; p>0; p--) ret*=n; return ret;}

Code:

/**3. Write a function called zeroSmaller() that is passed two int arguments by reference and then sets the smaller of the two numbers to 0. Write a main() program to exercise this function.*/ #include<iostream> #include<conio.h> using namespace std; bool zeroSmaller(int& n1, int& n2); void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; int n1, n2;

What are the types of data type?

  • Character or small integer
  • Short Integer
  • Integer
  • Long integer
  • Boolean
  • Floating point numbers
  • Double precision floating point number
  • Long double precision floating point number
  • Wide character

To get a better idea on C++ data types, see related links below.