Pascal – Program Structures ”; Previous Next Before we study basic building blocks of the Pascal programming language, let us look a bare minimum Pascal program structure so that we can take it as a reference in upcoming chapters. Pascal Program Structure A Pascal program basically consists of the following parts − Program name Uses command Type declarations Constant declarations Variables declarations Functions declarations Procedures declarations Main program block Statements and Expressions within each block Comments Every pascal program generally has a heading statement, a declaration and an execution part strictly in that order. Following format shows the basic syntax for a Pascal program − program {name of the program} uses {comma delimited names of libraries you use} const {global constant declaration block} var {global variable declaration block} function {function declarations, if any} { local variables } begin … end; procedure { procedure declarations, if any} { local variables } begin … end; begin { main program block starts} … end. { the end of main program block } Pascal Hello World Example Following is a simple pascal code that would print the words “Hello, World!” − Live Demo program HelloWorld; uses crt; (* Here the main program block starts *) begin writeln(”Hello, World!”); readkey; end. This will produce following result − Hello, World! Let us look various parts of the above program − The first line of the program program HelloWorld; indicates the name of the program. The second line of the program uses crt; is a preprocessor command, which tells the compiler to include the crt unit before going to actual compilation. The next lines enclosed within begin and end statements are the main program block. Every block in Pascal is enclosed within a begin statement and an end statement. However, the end statement indicating the end of the main program is followed by a full stop (.) instead of semicolon (;). The begin statement of the main program block is where the program execution begins. The lines within (*…*) will be ignored by the compiler and it has been put to add a comment in the program. The statement writeln(”Hello, World!”); uses the writeln function available in Pascal which causes the message “Hello, World!” to be displayed on the screen. The statement readkey; allows the display to pause until the user presses a key. It is part of the crt unit. A unit is like a library in Pascal. The last statement end. ends your program. Compile and Execute Pascal Program Open a text editor and add the above-mentioned code. Save the file as hello.pas Open a command prompt and go to the directory, where you saved the file. Type fpc hello.pas at command prompt and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line and would generate hello executable file and hello.o object file. Now, type hello at command prompt to execute your program. You will be able to see “Hello World” printed on the screen and program waits till you press any key. $ fpc hello.pas Free Pascal Compiler version 2.6.0 [2011/12/23] for x86_64 Copyright (c) 1993-2011 by Florian Klaempfl and others Target OS: Linux for x86-64 Compiling hello.pas Linking hello 8 lines compiled, 0.1 sec $ ./hello Hello, World! Make sure that free pascal compiler fpc is in your path and that you are running it in the directory containing source file hello.pas. Print Page Previous Next Advertisements ”;
Category: Computer Programming
Pascal – Discussion
Discuss Pascal ”; Previous Next Pascal is a procedural programming language, designed in 1968 and published in 1970 by Niklaus Wirth and named in honour of the French mathematician and philosopher Blaise Pascal. Pascal runs on a variety of platforms, such as Windows, Mac OS, and various versions of UNIX/Linux. This tutorial will give you great understanding of Pascal to proceed with Delphi and other related frameworks etc. Print Page Previous Next Advertisements ”;
Pascal – Overview
Pascal – Overview ”; Previous Next Pascal is a general-purpose, high-level language that was originally developed by Niklaus Wirth in the early 1970s. It was developed for teaching programming as a systematic discipline and to develop reliable and efficient programs. Pascal is Algol-based language and includes many constructs of Algol. Algol 60 is a subset of Pascal. Pascal offers several data types and programming structures. It is easy to understand and maintain the Pascal programs. Pascal has grown in popularity in the teaching and academics arena for various reasons: Easy to learn. Structured language. It produces transparent, efficient and reliable programs. It can be compiled on a variety of computer platforms. Features of the Pascal Language Pascal has the following features − Pascal is a strongly typed language. It offers extensive error checking. It offers several data types like arrays, records, files and sets. It offers a variety of programming structures. It supports structured programming through functions and procedures. It supports object oriented programming. Facts about Pascal The Pascal language was named for Blaise Pascal, French mathematician and pioneer in computer development. Niklaus Wirth completed development of the original Pascal programming language in 1970. Pascal is based on the block structured style of the Algol programming language. Pascal was developed as a language suitable for teaching programming as a systematic discipline, whose implementations could be both reliable and efficient. The ISO 7185 Pascal Standard was originally published in 1983. Pascal was the primary high-level language used for development in the Apple Lisa, and in the early years of the Mac. In 1986, Apple Computer released the first Object Pascal implementation, and in 1993, the Pascal Standards Committee published an Object-Oriented Extension to Pascal. Why to use Pascal? Pascal allows the programmers to define complex structured data types and build dynamic and recursive data structures, such as lists, trees and graphs. Pascal offers features like records, enumerations, subranges, dynamically allocated variables with associated pointers and sets. Pascal allows nested procedure definitions to any level of depth. This truly provides a great programming environment for learning programming as a systematic discipline based on the fundamental concepts. Among the most amazing implementations of Pascal are − Skype Total Commander TeX Macromedia Captivate Apple Lisa Various PC Games Embedded Systems Print Page Previous Next Advertisements ”;
OAuth 2.0 – Architecture
OAuth 2.0 – Architecture ”; Previous Next In this chapter, we will discuss the architectural style of OAuth 2.0. Step 1 − First, the user accesses resources using the client application such as Google, Facebook, Twitter, etc. Step 2 − Next, the client application will be provided with the client id and client password during registering the redirect URI (Uniform Resource Identifier). Step 3 − The user logs in using the authenticating application. The client ID and client password is unique to the client application on the authorization server. Step 4 − The authenticating server redirects the user to a redirect Uniform Resource Identifier (URI) using authorization code. Step 5 − The user accesses the page located at redirect URI in the client application. Step 6 − The client application will be provided with the authentication code, client id and client password, and send them to the authorization server. Step 7 − The authenticating application returns an access token to the client application. Step 8 − Once the client application gets an access token, the user starts accessing the resources of the resource owner using the client application. OAuth 2.0 has various concepts, which are briefly explained in the following table. Sr.No. Concept & Description 1 Terminology OAuth provides some additional terms to understand the concepts of authorization. 2 Web Server Web server delivers the web pages and uses HTTP to serve the files that forms the web pages to the users. 3 User-Agent The user agent application is used by client applications in the user”s device, which acts as the scripting language instance. 4 Native Application Native application can be used as an instance of desktop or mobile phone application, which uses the resource owner password credentials. Print Page Previous Next Advertisements ”;
OAuth 2.0 – Overview
OAuth 2.0 – Overview ”; Previous Next What is OAuth 2.0? OAuth is an open authorization protocol, which allows accessing the resources of the resource owner by enabling the client applications on HTTP services such as Facebook, GitHub, etc. It allows sharing of resources stored on one site to another site without using their credentials. It uses username and password tokens instead. OAuth 2.0 is developed by the IETF OAuth Working Group, published in October 2012. Why Use OAuth 2.0? You can use OAuth 2.0 to read data of a user from another application. It supplies the authorization workflow for web, desktop applications, and mobile devices. It is a server side web app that uses authorization code and does not interact with user credentials. Features of OAuth 2.0 OAuth 2.0 is a simple protocol that allows to access resources of the user without sharing passwords. It provides user agent flows for running clients application using a scripting language, such as JavaScript. Typically, a browser is a user agent. It accesses the data using tokens instead of using their credentials and stores data in online file system of the user such as Google Docs or Dropbox account. Advantages of OAuth 2.0 OAuth 2.0 is a very flexible protocol that relies on SSL (Secure Sockets Layer that ensures data between the web server and browsers remain private) to save user access token. OAuth 2.0 relies on SSL which is used to ensure cryptography industry protocols and are being used to keep the data safe. It allows limited access to the user”s data and allows accessing when authorization tokens expire. It has ability to share data for users without having to release personal information. It is easier to implement and provides stronger authentication. Disadvantages of OAuth 2.0 If you are adding more extension at the ends in the specification, it will produce a wide range of non-interoperable implementations, which means you have to write separate pieces of code for Facebook, Google, etc. If your favorite sites are connected to the central hub and the central account is hacked, then it will lead to serious effects across several sites instead of just one. Print Page Previous Next Advertisements ”;
Obtaining an Access Token
OAuth 2.0 – Obtaining an Access Token ”; Previous Next An access token is a string that identifies a user, an application, or a page. The token includes information such as when the token will expire and which app created that token. First, it is necessary to acquire OAuth 2.0 client credentials from API console. Then, the access token is requested from the authorization server by the client. It gets an access token from the response and sends the token to the API that you wish to access. You must send the user to the authorization endpoint at the beginning. Following is an example of a dummy request https://publicapi.example.com/oauth2/authorize?client_id=your_client_id&redirect_uri=your_url &response_type=code Following are the parameters and their descriptions. client_id − It should be set to the client id of your application. redirect_uri − It should be set to the URL. After the request is authorized, the user will be redirected back. response_type − It can either be a code or a token. The code must be used for server side applications, whereas the token must be used for client side applications. In server side applications, you can make sure that the secrets are saved safely. Following table lists the concepts of Client Credentials. Sr.No. Concept & Description 1 Authorization Code The authorization code allows accessing the authorization request and grants access to the client application to fetch the owner resources. 2 Resource Owner Password Credentials The resource owner password credentials include only one request and one response, and is useful where the resource owner has a good relationship with the client. 3 Assertion Assertion is a package of information that makes the sharing of identity and security information across various security domains possible. 4 Refresh Token The refresh tokens are used to acquire a new access tokens, which carries the information necessary to get a new access token. 5 Access Token Response Access token is a type of token that is assigned by the authorization server. 6 Access Token Error Response Codes If the token access request, which is issued by the authorization server is invalid or unauthorized, then the authorization server returns an error response. Print Page Previous Next Advertisements ”;
OAuth 2.0 – Useful Resources
OAuth 2.0 – Useful Resources ”; Previous Next The following resources contain additional information on OAuth 2.0. Please use them to get more in-depth knowledge on this. Useful Links on OAuth 2.0 OAuth 2.0 Wiki − Wikipedia Reference for OAuth 2.0. Useful Books on OAuth 2.0 To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;
Obj-C – Memory Management
Obj-C Memory Management ”; Previous Next Memory management is one of the most important process in any programming language. It is the process by which the memory of objects are allocated when they are required and deallocated when they are no longer required. Managing object memory is a matter of performance; if an application doesn”t free unneeded objects, its memory footprint grows and performance suffers. Objective-C Memory management techniques can be broadly classified into two types. “Manual Retain-Release” or MRR “Automatic Reference Counting” or ARC “Manual Retain-Release” or MRR In MRR, we explicitly manage memory by keeping track of objects on our own. This is implemented using a model, known as reference counting, that the Foundation class NSObject provides in conjunction with the runtime environment. The only difference between MRR and ARC is that the retain and release is handled by us manually in former while its automatically taken care of in the latter. The following figure represents an example of how memory management work in Objective-C. The memory life cycle of the Class A object is shown in the above figure. As you can see, the retain count is shown below the object, when the retain count of an object becomes 0, the object is freed completely and its memory is deallocated for other objects to use. Class A object is first created using alloc/init method available in NSObject. Now, the retain count becomes 1. Now, class B retains the Class A”s Object and the retain count of Class A”s object becomes 2. Then, Class C makes a copy of the object. Now, it is created as another instance of Class A with same values for the instance variables. Here, the retain count is 1 and not the retain count of the original object. This is represented by the dotted line in the figure. The copied object is released by Class C using the release method and the retain count becomes 0 and hence the object is destroyed. In case of the initial Class A Object, the retain count is 2 and it has to be released twice in order for it to be destroyed. This is done by release statements of Class A and Class B which decrements the retain count to 1 and 0, respectively. Finally, the object is destroyed. MRR Basic Rules We own any object we create: We create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” We can take ownership of an object using retain: A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. We use retain in two situations − In the implementation of an accessor method or an init method, to take ownership of an object we want to store as a property value. To prevent an object from being invalidated as a side-effect of some other operation. When we no longer need it, we must relinquish ownership of an object we own: We relinquish ownership of an object by sending it a release message or an autorelease message. In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object. You must not relinquish ownership of an object you do not own: This is just corollary of the previous policy rules stated explicitly. #import <Foundation/Foundation.h> @interface SampleClass:NSObject – (void)sampleMethod; @end @implementation SampleClass – (void)sampleMethod { NSLog(@”Hello, World! n”); } – (void)dealloc { NSLog(@”Object deallocated”); [super dealloc]; } @end int main() { /* my first program in Objective-C */ SampleClass *sampleClass = [[SampleClass alloc]init]; [sampleClass sampleMethod]; NSLog(@”Retain Count after initial allocation: %d”, [sampleClass retainCount]); [sampleClass retain]; NSLog(@”Retain Count after retain: %d”, [sampleClass retainCount]); [sampleClass release]; NSLog(@”Retain Count after release: %d”, [sampleClass retainCount]); [sampleClass release]; NSLog(@”SampleClass dealloc will be called before this”); // Should set the object to nil sampleClass = nil; return 0; } When we compile the above program, we will get the following output. 2013-09-28 04:39:52.310 demo[8385] Hello, World! 2013-09-28 04:39:52.311 demo[8385] Retain Count after initial allocation: 1 2013-09-28 04:39:52.311 demo[8385] Retain Count after retain: 2 2013-09-28 04:39:52.311 demo[8385] Retain Count after release: 1 2013-09-28 04:39:52.311 demo[8385] Object deallocated 2013-09-28 04:39:52.311 demo[8385] SampleClass dealloc will be called before this “Automatic Reference Counting” or ARC In Automatic Reference Counting or ARC, the system uses the same reference counting system as MRR, but it inserts the appropriate memory management method calls for us at compile-time. We are strongly encouraged to use ARC for new projects. If we use ARC, there is typically no need to understand the underlying implementation described in this document, although it may in some situations be helpful. For more about ARC, see Transitioning to ARC Release Notes. As mentioned above, in ARC, we need not add release and retain methods since that will be taken care by the compiler. Actually, the underlying process of Objective-C is still the same. It uses the retain and release operations internally making it easier for the developer to code without worrying about these operations, which will reduce both the amount of code written and the possibility of memory leaks. There was another principle called garbage collection, which is used in Mac OS-X along with MRR, but since its deprecation in OS-X Mountain Lion, it has not been discussed along with MRR. Also, iOS objects never had garbage collection feature. And with ARC, there is no use of garbage collection in OS-X too. Here is a simple ARC example. Note this won”t work on online compiler since it does not support ARC. #import <Foundation/Foundation.h> @interface SampleClass:NSObject – (void)sampleMethod; @end @implementation SampleClass – (void)sampleMethod { NSLog(@”Hello, World! n”); } – (void)dealloc { NSLog(@”Object deallocated”); } @end int main() { /* my first program in Objective-C */ @autoreleasepool { SampleClass *sampleClass = [[SampleClass alloc]init]; [sampleClass sampleMethod]; sampleClass = nil; } return 0; } When we compile the above program, we will get the following output. 2013-09-28 04:45:47.310 demo[8385] Hello, World! 2013-09-28
Objective-C – Categories
Objective-C Categories ”; Previous Next Sometimes, you may find that you wish to extend an existing class by adding behavior that is useful only in certain situations. In order add such extension to existing classes, Objective-C provides categories and extensions. If you need to add a method to an existing class, perhaps, to add functionality to make it easier to do something in your own application, the easiest way is to use a category. The syntax to declare a category uses the @interface keyword, just like a standard Objective-C class description, but does not indicate any inheritance from a subclass. Instead, it specifies the name of the category in parentheses, like this − @interface ClassName (CategoryName) @end Characteristics of Category A category can be declared for any class, even if you don”t have the original implementation source code. Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class. At runtime, there”s no difference between a method added by a category and one that is implemented by the original class. Now, let”s look at a sample category implementation. Let”s add a category to the Cocoa class NSString. This category will make it possible for us to add a new method getCopyRightString which helps us in returning the copyright string. It is shown below. Live Demo #import <Foundation/Foundation.h> @interface NSString(MyAdditions) +(NSString *)getCopyRightString; @end @implementation NSString(MyAdditions) +(NSString *)getCopyRightString { return @”Copyright TutorialsPoint.com 2013″; } @end int main(int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *copyrightString = [NSString getCopyRightString]; NSLog(@”Accessing Category: %@”,copyrightString); [pool drain]; return 0; } Now when we compile and run the program, we will get the following result. 2013-09-22 21:19:12.125 Categories[340:303] Accessing Category: Copyright TutorialsPoint.com 2013 Even though any methods added by a category are available to all instances of the class and its subclasses, you”ll need to import the category header file in any source code file where you wish to use the additional methods, otherwise you”ll run into compiler warnings and errors. In our example, since we just have a single class, we have not included any header files, in such a case we should include the header files as said above. Print Page Previous Next Advertisements ”;
Objective-C – Useful Resources ”; Previous Next The following resources contain additional information on Objective C. Please use them to get more in-depth knowledge on this topic. Useful Video Courses Understanding S.M.A.R.T. Objectives 19 Lectures 1 hours PARTHA MAJUMDAR More Detail Econometrics For Dummies: Learn The Basic 20 Lectures 51 mins Pedro Planas More Detail Master Creating Well-Defined Marketing Objectives in 30 mins 6 Lectures 25 mins Ken Burke More Detail Build Landing Page Design for Higher Sales & Lead Generation 20 Lectures 1.5 hours Being Commerce More Detail Build Landing Page Optimization for higher conversion rate 20 Lectures 1.5 hours Being Commerce More Detail RECRUITMENT: IT basics for IT Recruiters Featured 61 Lectures 5 hours Valeria Frisch More Detail Print Page Previous Next Advertisements ”;