Why people think C++ is Complicated

Why would someone be using C++ in the first place? 

Article By: John D. Cook

Most likely because they need performance or fine-grained control that they cannot get somewhere else. A Ruby programmer, for example, can make a design decision that makes code 10% slower but much easier to use. “Hey, if you want the best performance possible, why are you using Ruby? Didn’t you come here because you wanted convenience?” But the C++ programmer can’t say that. It’s not turtles all the way down. Often C++ is the last human-generated language in a technology stack before you hit metal.

From The Register

Apple’s Mac OS X, Adobe Illustrator, Facebook, Google’s Chrome browser, the Apache MapReduce clustered data-processing architecture, Microsoft Windows 7 and Internet Explorer, Firefox, and MySQL — to name just a handful — are written in part or in their entirety with C++.

Certainly there is a lot of software implemented in higher-level languages, but those high-level languages are almost always implemented in C or C++. When there’s no lower-level language to appeal to, you have to offer a lot of options, even if 90% of users won’t need those options.

But, that doesn’t mean all C++ libraries have to be complicated. The argument above says that the lowest layers have to be complicated and they’re written in C++.  But why couldn’t the next layer up also be written in C++?
Some time in the 90’s I ran across an article called “Top C++.” I’ve tried unsuccessfully since then to find a copy of  it. As I recall, the article proposed dividing C++ conceptually into two languages: Top C++ and Bottom C++. Explicit memory management, for example, would be in Bottom C++. Top C++ would be a higher-level language. You could, for example, tell a compiler that you intend to write Top C++ and it could warn you if you use features designated as Bottom C++.
Of course you could slip from Top C++ into Bottom C++ if you really needed to, and that’s the beauty of using one language for high-level and low-level code. Application code in C++ would use Top C++ features by default, but could deliberately drop down a level when necessary. A section of Bottom C++ could be marked with comments or compiler pragmas and justified in a code review. Instead of having to cross a language barrier, you simply cross a convention barrier.
I thought this was a great idea. I’ve followed this approach throughout my career, writing high-level C++ on top of low-level C++ libraries. To some extent I put on my Bottom C++ hat when I’m writing library code and my Top C++ hat when I’m writing applications.
But most of the world has decided that’s not the way to go. If you’re writing C++, it might as well be Bottom C++. Instead of Top C++, write VB or some other higher-level language. There’s very little interest in high-level C++.
I imagine this decision has more to do with management than technology. It’s easier to have people either write C++ or not. If you’re writing C++, then use any language feature any time. I suppose this is a stable equilibrium and that the Top/Bottom C++ partition is not.
Source: John D. Cook

Data Types in C++

The basic in any programming language is the variables where you store the data. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

You may like to store information of various data types in c++ like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
 

Primitive Built-in Types:

 
Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
Several of the basic types can be modified using one or more of these type modifiers:
  • signed
  • unsigned
  • short
  • long

Size of each Data Type:

Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int Range 0 to 65,535
signed short int Range -32768 to 32767
long int 4bytes -2,147,483,647 to 2,147,483,647
signed long int 4bytes same as long int
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character

NOTE: The sizes of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.

typedef Declarations:

You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef:
 

For example, the following tells the compiler that feet is another name for int:
 

Now, the following declaration is perfectly legal and creates an integer variable called distance:
 


Enumerated Types:

An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration.
 
To create an enumeration requires the use of the keyword enum. The general form of an enumeration type is:
 

Here, the enum-name is the enumeration’s type name. The list of names is comma separated.
 
For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value “blue”.
 

By default, the value of the first name is 0, the second name has the value 1, the third has the value 2, and so on. But you can give a name a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5.
 

Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.

Operator Overloading in C++

We can redefine or overload most of the built-in operators available in C++. Thus a programmer can use operators with user-defined types as well, this process is operator overloading.
Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded operators may be defined as ordinary non-member functions or as class member functions. In case we define above function as non-member function of a class then we would have to pass two arguments for each operand as follows:

Following is the example to show the concept of operator over loading using a member function. Here an object is passed as an argument whose properties will be accessed using this object, the object which will call this operator can be accessed using this operator as explained below:

OUTPUT:

Following is the list of operators which can be overloaded:
+ * / % ^
& | ~ ! , =
< > <= >= ++
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new [] delete delete []
Following is the list of operators, which can not be overloaded:
:: .* . ?:

Answer to Sum Array Challenge

CODE:

Sum Array challenge

In this challenge, given an array of integers, the goal is to efficiently find the subarray that has the greatest value when all of its elements are summed together. Note that because some elements of the array may be negative, the problem is not solved by simply picking the start and end elements of the array to be the subarrray, and summing the entire array.

For example, given the array

The maximum sum of a subarray is 4. It is possible for the subarray to be zero elements in length (if every element of the array were negative).

Before you write the code, take some time to think of the most efficient solution possible; it may surprise you. The major goal of this challenge is to test your algorithmic skills rather than merely your ability to write code quickly.

Answer


Click to see Answer

Different ways to pass Array into Function in C++

There are many ways in which you can pass array into function. I’ve discussed 3 different ways to do this.

How programs works:

1-  Declare array of size 10
2- Initialize it using for loop
3- Calling “UpdateArray” function and passing array as argument
4- Take squares of all values and update it
5- Display final result in main function

FIRST WAY:

As we know array name is a constant pointer if we can get the this pointer we can access all values of array.
declaring  array in function parameter
Prototype: void  UpdateArray(int array[] );
 

CODE:

 

SECOND WAY:

Declaring an integer pointer in function parameter
This pointer will receive arrays name(constant pointer) and we can access all values and update too.
Prototype: void  UpdateArray(int *array );
 

CODE:

 

 
THIRD WAY:

In this way we pass two arguments one is arrays name and second is its size.
In function receiving parameters we declares an array or integer pointer (your choice) to receive address of array and another integer variable size which contains the size of array.Prototype:  void  UpdateArray(int *array, int size );

CODE: