Object Oriented Programming Session # 03
Abstraction: Process of forming of general and relevant information from a complex scenarios. Encapsulation: Localization of information within an object. This also leads to Information Hiding Inheritance: Is a process by which one object acquires the properties of another object Polymorphism: allows the programmer to use “one interface” for “many functions”. Object Oriented Concepts
Basics of C# [01] C# code normally uses the file extension of “.cs”. If a namespace is left out, your code is placed into the default, global, namespace. The “using” directive tells C# what methods you would like to use from that namespace. If we left out the “using System” statement, then we would have had to write “System.Console.WriteLine” instead of just “Console.WriteLine”.
System.Console.WriteLine() will output a string to the console. You can use this just like Java’s System.out.println(): System.Console.WriteLine(“hello world “ + 10/2); will output: hello world 5 We can also use {0}, {1}, {2}, … etc. to indicate arguments in the WriteLine statement to print. For example: Console.WriteLine(“hi {0} you are {0} and your age is {1}”, “Kenrick”, 23); will output: hi Kenrick you are Kenrick and your age is 23 Basics of C# [02]
There are also options to control things such as the number of columns to use for each variable, the number of decimals places to print, etc. For example, we could use :C to specify the value should be displayed as currency: Console.WriteLine(“you have {0:C} dollars.”, 1.3); outputs as: you have $1.30 dollars. Basics of C# [03]
Basics of C# [04] - Data Types C# supports value types and reference types. Value types are essentially the primitive types found in most languages, and are stored directly on the stack. Reference types are objects and are created on the heap. Built-In Types Ref type
Control Structures are statements which changes the execution sequence of the program C++ supports all the control structures supported in C If, else if, else switch case for loop while loop do while Control Structures
C# extends the features of C++ Structures C# structures can have functions as its members. Default access specifier for Structure members is “public”. Structures in C# struct _employee { int m_iEmpId; float m_fBasic; float m_fSal; void CalculateSal() { /* Code for sal calculation */ } struct { private: /* private data member and methods */ protected: /* protected data member and methods */ public: /* public data member and methods */ };
Are the heart of object oriented programming. It is a collection of objects. A class is simply a representation of a type of object. Application structure mirrors real world objects Related methods and data encapsulated in object Objects with the same structure are of same type A class is a blueprint for all things of that type Instance (request) of a class is a thing, an object Classes have three main types of members: Methods (functions in other languages) Fields (the data, sometimes called member variables) Properties (accessed like fields, but actually methods) Class [01]
Basic unit of abstraction in C# Extension of the concept of structures Provides a technique way to bind data and functions together (Encapsulation) Data Members – Variables declared inside the class Member Functions – Functions declared/defined inside the class Member Functions are also called as Methods Class [02] class { private: /* private data member and methods */ protected: /* protected data member and methods */ public: /* public data member and methods */ }; class Employee { int m_iEmpId; float m_fBasic; public: void CalculateSal() { /* Code for sal calculation */ }
Declaration of class and object
An object is basically a block of memory that has been allocated and configured according to the class. A program may create many objects of the same class. Objects are also called instances of a class. Instance of a class in memory Unique, identifiable, self – contained entity that contains attributes and behaviors If class is viewed as a data type then object can be considered as a variable of class data type. Objects
Objects-Example [01]
In the above C# code, we have a Person class with one member field. We declare a name member field. The public keyword specifies that the member field will be accessible outside the class block. We use the dot operator to access the attributes of objects. Objects-Example [02]