Monday 27 April 2015

C# 5.0


  • Object Orientation - C# is a rich implementation of the object orientated paradigm.
  • Type Safety - C# is primarily a type safe language. i.e prevents you from interacting with a string type as though it were a integer.
  • Memory Management - C# relies on the run-time to perform automatic memory management has a garbage collector..


Monday 20 April 2015

9.0 Sub Programs

9.2 Fundamentals of subprograms
- allow process abstractions
- each has a single entry point.
- the calling program unit is suspended during the execution of the call subprogram.
- parameters are a way of giving a subprograms access to the data they are to process

there are two types of subprograms - function and sub procedures

A Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments, it doesn't require to return a value, but it cannot be used in an expression. (expressed with void in C#)

A Function
procedure, is a procedure that can take arguments, perform a series of statements, and change the value of its arguments, but it also must return a value and it can be used in an expression.

- local variables = Variables that are defined inside subprograms.

subprograms type check parameters

9.5 Parameter passing methods

9.5.1 Pass by value 
- creates a deep copy of the object passed in from the caller, not a reference to the original object.

- if the deep copy gets changed with the subprogram, the callers object will not be changed.


9.5.2 Pass-by-Result
Pass-by-result is an implementation model for out mode parameters.
- helps assigning new values to multiple value-types.

ie. void Fixer(out int x, out int y)

 9.5.3 Pass-by-Value-Result
 - a combination of pass-by-value and pass-by-result

ie. void Fixer(int x, out int y)


 9.5.4 Pass-by-Reference
 - passes in an memory address of the object (shallow copy) , to the subprogram
- if the object is modified within the subprogram, the callers object will persist those changes.


9.7 Overloaded Subprograms
An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. Every version of an  overloaded subprogram must have a unique protocol, that is, it must be different from the others in the number, order, or types of parameters or in return type. 


9.8 Generic Subprograms (Generic Method)

-Software reuse can be an important contribute to software productivity.

-One way to re-use, is to lessen the need to create different subprograms to do the same thing for different data-types


- A polymorphic subprogram takes parameters of different type on different activations.

- Parametric polymorphism - is provided by a subprogram hat takes generic parameters that are used in type expressions that describe the types of the parameters of the subprogram.

i.e of a generic method
. public static T doit<t> (t pl)


11.0 - OOP Principles

*Inheritance

 the ability to create class/interface which inherits certain aspects from inherited parent

*Polymorphism


The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.
Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

Static Polymorphism

The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism. They are:
  • Function overloading
  • Operator overloading

Dynamic Polymorphism

  • C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.
    Here are the rules about abstract classes:
  • You cannot create an instance of an abstract class
  • You cannot declare an abstract method outside an abstract class
  • When a class is declared sealed, it cannot be inherited, 
  • Abstract classes cannot be declared sealed.

* Encapsulation

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.

The following are the benefits of encapsulation:
  • Protection of data from accidental corruption
  • Specification of the accessibility of each of the members of a class to the code outside the class
  • Flexibility and extensibility of the code and reduction in complexity
  • Lower coupling between objects and hence improvement in code maintainability
Encapsulation is used to restrict access to the members of a class so as to prevent the user of a given class from manipulating objects in ways that are not intended by the designer. While encapsulation hides the internal implementation of the functionalities of class without affecting the overall functioning of the system, it allows the class to service a request for functionality and add or modify its internal structure (data or methods) to suit changing requirements.

Encapsulation is also known as information hiding.


Uses access modifiers
Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.

* Abstraction

Abstraction from Object Oriented Programming perspective is extracting the core features of an object, without being specific about the implementation details.

helps reduce the complexity of the software

Sunday 5 April 2015

8.0 Control Statements & structures.

Intro 

Control statements 
 -Satements that allow for alternative control flow paths  and others  causing the repeated execution of sequences of statements.
- allow programs to be flexible and powerful

Control Stucture
- Is a control statement and the  collection of statements whose execution it controls.


8.2 Selection Statements
-  A selection statement provides the means of choice between two or more execution paths in a program.

  • two-way/n-way category   i.e (2 -way) if then else,   (n-way) if then if then, else
  • multiple-way selectors (multiple selection) C++, java not so much C#

8.2.1  Two Way Selection Statements

If  control_expression
then clause
else clause


*note the expression immediately after the if keyword is called a control expression 

Nested selectors  - are just selection statements in selection statements. an if statement within an if statement

8.2.2  Multiple- Selection Statements

- Allow the selection of one or any number of statements.
- C+=  java not C# - allow execution of more then one segment

var a = 9
switch (a)
    case 9
        a  = a + 3     ' now eqauls 12
   case 11
     break
   case 12  <---while hit here too
    break

C# with statements different from c-base processors as they disallows implicit execution of more than one segment. each segment has to end with a break statement.



8.2 Iterative Statements
An iterative statement is one that causes a statement or collection of statement to be executed zero, one or more times. often called a loop.

ie. for statement, foreach statement, while statement

  

Saturday 4 April 2015

7.0 Expressions and Assignment Statements

Expressions are the fundamental means of specifying computations in a programming language.
The purpose of an assignment statement is to change the value of a variable.
In programming languages, arithmetic expressions consist of operators operands, parentheses and function calls.

An operator can be
Unary - meaning it has a single operand.      ie:   ! or ++ ,         ex:    int c  = x++
Binary - meaning it has two operands.          ie:   ==                  ex:    bool b  =     (c == d)
Ternary = Meaning it has three operands.     ie:   ?:                    ex:    (input > 0) ? "yes" : "no"

Unaray addition is called the identity operator because it usually has no associated operation and thus has no effect on its operand.

var a = 10
(-a).Dump();
 output = -10
yet variable a value still equals 10

7.2 Arithmetic Expressions

The purpose of an arithmetic expression is to specificity and arithmetic computation.
An implementation of such a computation must cause two actions . fetching the operands, usually from memory and executing the arithmetic operations on those operands.

7.2.1  Operator Evaluation Order

7.2.1.1 Precedence
- The order of evaluation of operators in the expression
- i.e.   a + b * c

- The operator precedence rules for expression evaluation define the order in which  the operator of difference precedence levels are evaluated. These are based on the hierarchy of operators priorities, as seen by the language designer.

7.2.1.2 Associativity 
When an expression contains two adjacent occurrences of operators with the same level of precedence, the question of which operator is evaluated first. This is answered by the associativity rules of the language.

i.e. a - b + c

An operator can have either  left to right associativity or right to left associativity

7.2.1.3 Parentheses
Programmers can alter the precedence and associativity rules by placing parentheses in expressions. A parenthesized part of an expression has precedence over its adjacent un-parenthesized parts. for example, although multiplication has precedence over addition, in the expression below, addition will be evaluated first.

i.e (A + B) * C


7.2.1.5 Conditional Expressions
We now look at the ternary operator ?: This operator is used to form conditional expressions.
Sometimes if-then-else statements are used to perform conditional expression assignment.


If (count == 0) Then {average = 0} Else {average = sum/ count)

average = (count == 0) ? 0 : sum/count


7.3 Overloaded Operators
The multiple use of an operator is called operator overloading and is generally thought to be acceptable, as long as readability and/or reliability don't suffer. 

Using the same symbol for two completely unrelated operations is detrimental to readability.

Risks are that a programmer could overload * to be an  addition operator. 

7.4 Type Conversions
-Type conversions are either narrowing or widening.

- A narrowing conversion converts a value to a type that cannot store even approximations of all of the values of the original type.  double to an int, Narrowing conversion are not always safe, sometimes the magnitude of the converted value is changed in the process.


 - A widening conversion converts a value to a type that can include at least approximations of all of the value of the original type. ie. int to a double, Widening conversion are nearly always safe, meaning that the magnitude of the converted value is maintained.


7.4.1 Coercion in  Expressions
- Implicit type conversion that is initiated by the compiler when an operator has two operands of a different type. With in the arithmetic expression  example below the compiler would simply insert code to coerce the value of the int operand to a double


int a = 10
double b = 5.5


var c = a * b

 Languages like java that don't allow such mixed mode expressions would just throw up a type error, and the programmer would have to do the conversion explicitly.


7.4.2 Explicit Type Conversion

Most languages provide some capability for doing explicit conversions, with widening and narrowing. In some cases warning messages are produced when an explicit narrowing conversion results in a significant change to the value of the object being converted.

In c-based languages, explicit type casts are called casts.

(double) sum


7.5 Relational and Boolean Expressions

7.5.1 Relational Expressions
- A relational operator is an operator that compares the values of it operands. ie. ==, !=
- A relational expression has two operands and one relational operator.
-  The result value of a relational expression is Boolean.
-checks the a type of relationship between operands

below are example of relational operators
==, !=,  >, <,  >=, <=

7.5.2  Boolean Expressions
- Boolean expressions consist of Boolean variables, Boolean constants, relational expressions and Boolean operator. The operator usual includes those for AND OR and NOT operations.

var a = (a.value == 10  &&   b.value == 10 )


7.6 Short-Circuit Evaluation
 A short-circuit evaluation of an expression is one in which the result is determined with evaluating all of the operands and/or operators. in the expression below

If (a >= 0) && (b<110)

If  the variable a evaluation fails to find that variable a is greater then or equal to  0 then we already know that we don't need to evaluate the second relational expression to know that this whole Boolean expression results is false.



7.7 Assignment Statement
-Provides the mechanism by which the user can dynamically change the binding of values to variables.
- <target_variable><assignment_operator><expression>

7.7.2 Conditional targets
 In C++

flag ? count 1 : count 2 = 0

equal to

if (flag) count1 = 0 else count 2 = 0

7.7.3 Compound Assignment Operators
 - A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment.

ie.  sum +=  value is equivalent to     sum = sum + value 


7.7.4 Unary Assignment Operators
- operators with a single operand
- abbreviated assignments

example:      sum = count++;



 7.7.5 Assignment as an Expression
- an assignment expression within a relational expressions

IF ((c = a * b) ==100)