Skip to main content

Posts

Showing posts from September, 2011

Integer Numbers in Programming Language

An integer variable may be defined to be of type short , int , or long . The only difference is that an int uses more or at least the same number of bytes as a short , and a long uses more or at least the same number of bytes as an int . For example, on the author’s PC, a short uses 2 bytes, an int also 2 bytes, and a long 4 bytes. short age = 20; int salary = 65000; long price = 4500000; By default, an integer variable is assumed to be signed (i.e., have a signed representation so that it can assume positive as well as negative values). However, an integer can be defined to be unsigned by using the keyword unsigned in its definition. The keyword signed is also allowed but is redundant. unsigned short age = 20; unsigned int salary = 65000; unsigned long price = 4500000; A literal integer (e.g., 1984 ) is always assumed to be of type int , unless it has an L or l suffix, in which case it is treated as a long . Also, a literal integer can be specified to be...

Memory, Memory Address in Programming Language

A computer provides a Random Access Memory (RAM) for storing executable program code as well as the data the program manipulates. This memory can be thought of as a contiguous sequence of bits , each of which is capable of storing a binary digit (0 or 1). Typically, the memory is also divided into groups of 8 consecutive bits (called bytes ). The bytes are sequentially addressed. Therefore each byte can be uniquely identified by its address. Figure: Bits and bytes in memory. The C++ compiler generates executable code which maps data entities to memory locations. For example, the variable definition int salary = 65000; causes the compiler to allocate a few bytes to represent salary . The exact number of bytes allocated and the method used for the binary representation of the integer depends on the specific C++ implementation, but let us say two bytes encoded as a 2’s complement integer. The compiler uses the address of the first byte at which salary is alloca...

Comments in Programming Language

A comment is a piece of descriptive text which explains some aspect of a program. Program comments are totally ignored by the compiler and are only intended for human readers. C++ provides two types of comment delimiters: · Anything after // (until the end of the line on which it appears) is considered a comment. · Anything enclosed by the pair /* and */ is considered a comment. Comments should be used to enhance (not to hinder) the readability of a program. The following two points, in particular, should be noted: · A comment should be easier to read and understand than the code which it tries to explain. A confusing or unnecessarily-complex comment is worse han no comment at all. · Over-use of comments can lead to even less readability. A program which contains so much comment that you can hardly see the code can by no means be considered readable. · Use of descriptive names for variables and other entities in a program, and proper indentation of ...

Variables In Programming Language

A variable is a symbolic name for a memory location in which data can be stored and subsequently recalled. Variables are used for holding data values so that they can be utilized in various computations in a program. All variables have two important attributes: · A type which is established when the variable is defined (e.g., integer, real, character). Once defined, the type of a C++ variable cannot be changed. · A value which can be changed by assigning a new value to the variable. The kind of values a variable can assume depends on its type. For example, an integer variable can only take integer values (e.g., 2, 100, -12).

Compilation Process of a C++ Program

Compiling a C++ program involves a number of steps (most of which are transparent to the user): · First, the C++ preprocessor goes over the program text and carries out theinstructions specified by the preprocessor directives (e.g., #include ). The result is a modified program text which no longer contains any directives. · Then, the C++ compiler translates the program code. The compiler may be a true C++ compiler which generates native (assembly or machine) code, or just a translator which translates the code into C. In the latter case, the resulting C code is then passed through a C compiler to produce native object code. In either case, the outcome may be incomplete due to the program referring to library routines which are not defined as a part of the program. For example, Listing 1.1 refers to the << operator which is actually defined in a separate IO library. · Finally, the linker completes the object code by linking it with the object code of any lib...

A simple C++ Program and Explanation of it

A simple C++ Program and Explanation of it: #include <iostream.h> int main (void) { cout << "Hello World\n"; }             #include <iostream.h> 1.       This line uses the preprocessor directive #include to include the contents of the header file iostream.h in the program. Iostream.h is a standard C++ header file and contains definitions for input and output. int main (void) 2.       This line defines a function called main . A function may have zero or more parameters ; these always appear after the function name, between a pair of brackets. The word void appearing between the brackets indicates that main has no parameters. A function may also have a return type ; this always appears before the function name. The return type for main is int (i.e., an integer number). All C++ programs must have exactly one main function. Program...

Polymorphism, Encapsulation, and Inheritance Work Together

Polymorphism, Encapsulation, and Inheritance Work Together: When properly applied, polymorphism, encapsulation, and inheritance combine to produce a programming environment that supports the development of far more robust and scaleable programs than does the process-oriented model. A well-designed hierarchy of classes is the basis for reusing the code in which you have invested time and effort developing and testing. Encapsulation allows you to migrate your implementations over time without breaking the code that depends on the public interface of your classes. Polymorphism allows you to create clean, sensible, readable, and resilient code. Of the two real-world examples, the automobile more completely illustrates the power of object-oriented design. Dogs are fun to think about from an inheritance standpoint, but cars are more like programs. All drivers rely on inheritance to drive different types (subclasses) of vehicles. Whether the vehicle is a school bus, a Mercedes sedan, a...

Polymorphism In JAV A Programming Languagea

Polymorphism In JAV A Programming Language: Polymorphism (from the Greek, meaning “many forms”) is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. Consider a stack (which is a last-in, first-out list). You might have a program that requires three types of stacks. One stack is used for integer values, one for floating-point values, and one for characters. The algorithm that implements each stack is the same, even though the data being stored differs. In a non– object-oriented language, you would be required to create three different sets of stack routines, with each set using different names. However, because of polymorphism, in Java you can specify a general set of stack routines that all share the same names. More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” This means that it is possible to design a generic inter...

Inheritance in JAVA Programming Language

Inheritance in JAVA Programming Language: Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification. As mentioned earlier, most knowledge is made manageable by hierarchical (that is, top-down) classifications. For example, a Golden Retriever is part of the classification dog, which in turn is part of the mammal class, which is under the larger class animal. Without the use of hierarchies, each object would need to define all of its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case. Let’s take a closer look at this process. Most people naturally view the world as made up of objects that are re...

Encapsulation in JAVA Programming Language

Encapsulation: Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface. To relate this to the real world, consider the automatic transmission on an automobile. It encapsulates hundreds of bits of information about your engine, such as how much you are accelerating, the pitch of the surface you are on, and the position of the shift lever. You, as the user, have only one method of affecting this complex encapsulation: by moving the gear-shift lever. You can’t affect the transmission by using the turn signal or windshield wipers, for example. Thus, the gear-shift lever is a well-defined (indeed, unique) interface to the transm...

Abstraction in JAVA Programming Language

Abstraction in JAVA Programming Language: An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behavior. This abstraction allows people to use a car to drive to the grocery store without being overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission, and braking systems work. Instead they are free to utilize the object as a whole. A powerful way to manage abstraction is through the use of hierarchical classifications. This allows you to layer the semantics of complex systems, breaking them into more manageable pieces. From the outside, the car is a single object. Once inside, you see that the car consists of several subsystems: steering, brakes, sound system, seat belts, heating, cellular phone, and...

Object-Oriented Programming and JAVA Programming Language

  Object-Oriented Programming and:   Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, olymorphism, and inheritance. Many modern programming languages now support OOP, at least as an option. JAVA Programming Language: Object-oriented programming is at the core of Java. In fact, all Java programs are object-oriented, this isn’t an option the way that it is in C++, for example. OOP is so integral to Java that you must understand its basic principles before you can write even simple Java programs. Therefore, this chapter begins with a discussion of the theoretical aspects of OOP.

Adding two integer array element wise.

Here given an program that adds two integer array element wise. #include <iostream> using namespace std; int* sum(int*, int, int*, int); void print(int*, int); int main() {     int a[] = {11,22,33,44,55};     int b[]={66,77,88};     print(a,5);     print(b,3);     int* c= sum(a,5,b,3);     print(c,5); } int* sum(int* a, int m, int*b, int n) {     int* aa, *bb;     int mm, nn;     if(m<n) aa = a, bb=b, mm=m, nn=n;     else aa=b, bb=a, mm=n, nn=m;     int* c = new int[nn];     for(int i=0; i< mm; i++)     c[i]= aa[i] + bb[i];     for (int i=mm;i<nn; i++)     c[i]=bb[i];     return c; } void print(int* a, int n) {     cout <<"{" << a[0];     for (int i=1;i<n;i++)     cout << "," << a[i];     cout << "} \n...

Appends one array to another and print them in C++

In the following program, show how to append or add one array to another. #include <iostream> using namespace std; int* append(int*, int, int*, int); void print(int*, int); int main() {     int a[] = {11,22,33,44,55};     int b[]={66,77,88,99};     print(a,5);     print(b,5);     int* c= append(a,5,b,5);     print(c,9); } int* append(int* a, int m, int*b, int n) {     int* c= new int[m+n];     for (int i=0; i<m;i++)     c[i]=a[i];     for(int i=m;i<m+n;i++)     c[i]=b[i-m];     return c; } void print(int* a, int n) {     cout <<"{" << a[0];     for (int i=1;i<n;i++)     cout << "," << a[i];     cout << "} \n"; }

Finding maximum value of an array in C++

In the following program you can find maximum value of an array: #include <iostream> using namespace std; int min(int*, int); int main() {     int array[]={66,44,88,11,77,33,99,55,22};     cout<< "min(array,9)=" << min(array,9); } int min(int* array, int n) {     int m=array[0];     for(int i=1;i<n;i++)     if (array[i]>m) m=array[i];     return m; }

Finding minimum value of an array in C++

In the following program you can find minimum value of an array: #include <iostream> using namespace std; int min(int*, int); int main() {     int array[]={66,44,88,11,77,33,99,55,22};     cout<< "min(array,9)=" << min(array,9); } int min(int* array, int n) {     int m=array[0];     for(int i=1;i<n;i++)     if (array[i]<m) m=array[i];     return m; }