What is the difference between C++ and Java ?

Both C++ and Java use similar syntax and are Object Oriented, but:

  • Java does not support pointers. Pointers are inherently tricky to use and troublesome.
  • Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.
  • Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.
  • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework
  • All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions.
  • C++ requires explicit memory management, while Java includes automatic garbage collection.

Formula to Convert CMYK to RGB values

It is possible to convert CMYK values to RGB values. We can see how to do this in the following section.

Consider the cyan, magento, yellow and black values of CMYK as c, m, y and k respectively. Now we can find the equivalent RGB values using the following formulas :

r = (1-k)-(1-k)*c;

g = (1-k)-(1-k)*m;

b = (1-k)-(1-k)*y;

Here, r, g and b will have the corresponding red, green and blue values.

Way to Find Maximun Digit in C#

int max_value = std::max({a, b, c});

It makes use of an overload of std::max which takes std::initializer_list<T>

It also means that we can pass more than three argument.

int max_value = std::max({1,2,3,4,5,6,98,10});

 

Adding elements to a List in C#

This is achieved using Add command.

Eg:

List<int> listname = new List<int>();
listname .Add(9);
listname .Add(12);
listname .Add(11);  // 9,12,11 are the elements added to  list name

We can mention list in other datatypes such as string etc. by mentioning that datatype instead of int type.

String to Byte Array Conversion in C#

// C# to convert a string to a byte array.
 public static byte[] StrToByteArray(string str)
 { 
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
 return encoding.GetBytes(str); 
}

// C# to convert a byte array to a string.
 byte [] dBytes = ... 
string str;
 System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
 str = enc.GetString(dBytes);

Calculator in C

The  code given  is a calculator in  C. The scanf which scans the input as a number symbol just as in case of normal calculator.

#include<stdio.h>
float add(float,float);
float sub(float,float);
float product(float,float);
float divide(float,float);

void main()
{
float n1,n2;
char sym,choice;
printf("This Program is a program for calculator\n\n");
scanf("%f%c%f",&n1,&sym,&n2);
if(sym=='+')
printf("\n%f",add(n1,n2));
if(sym=='-')
printf("\n%f",sub(n1,n2));
if(sym=='*')
printf("\n%f",product(n1,n2));
if(sym=='/')
printf("%f",divide(n1,n2));
printf("\nDo you wish to continue[y/n]");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
main();
}

float add(float m1,float m2)
{
return(m1+m2);
}

float sub(float m1,float m2)
{
return(m1-m2);
}

float product(float m1,float m2)
{
return(m1*m2);
}

float divide(float m1,float m2)
{
return(m1/m2);
}

file manupulation in C

io.input( [file] )

Example:
local tmp = io.input()    -- save current file handle
io.input( "newfile.txt" ) -- open new file in text mode
print( io.read() )        -- read and display the file
io.input():close()        -- close the file
io.input( tmp )           -- restore the previous file