Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Lecture 06 - programming fundamentals

Uploaded by

anjalee himalki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 06 - programming fundamentals

Uploaded by

anjalee himalki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Language Fundamentals V

Object oriented with visual programming


M. Y Abdur Rahman
Classes and Objects
• Modifiers define the
declaration of a class.  The Baselist is the inherited
class.
◦ By default, classes inherit from
Modifier Description the System.Object type.
Sealed Class can't be inherited by a derived ◦ A class can inherit and
class. implement multiple interfaces
Static Class contains only static members.
but doesn't support multiple
inheritances.
Unsafe The class that has some unsafe
construct likes pointers.

Abstract The instance of the class is not created if


the Class is abstract.
The practical use of the abstract Class

• They are commonly used when you want to define a template for a
group of subclasses that share some common implementation
code, but you also want to guarantee that the objects of the
superclass cannot be created.

• For instance, let's say you need to create Dog, Cat and Fish objects.
They possess similar properties like color, size, and number of legs
as well as behavior so you create an Animal superclass. However,
what color is an Animal? How many legs does an Animal object
have? In this case, it doesn't make much sense to instantiate an
object of type Animal but rather only its subclasses.
sealed Class
• Use to restrict the inheritance feature of OOP.

• Once a class is defined as a sealed class, the class cannot be


inherited.

• If a class is derived from a sealed class then the compiler throws an


error.
The practical use of the seal Class

• Sometimes classes are too precious and not designed to be


inherited.

• Runtime/Reflection can make inheritance assumptions about


sealed classes when looking for types. A great example of this is -
Attributes are recommended to be sealed for lookup runtime
speed.

• The sealed keyword tells the CLR that there is no class further
down to look for methods, and that speeds things up.
• namespace consoleprogram18
• {
• public sealed class Employee
• {
• public double empid;
• public string name;
• public int age;


• }
• public class salesrep:Employee
• {
• public int salary { get; }= 100000;

• public void display()


• {
• Console.WriteLine($"Salary is: {salary}");
• Console.WriteLine($"Employee number :{empid}");
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• }
• }
• }
Interface cont.,
The practical use of interface
• So in this example, the PowerSocket doesn't know anything else
about the other objects. The objects all depend on Power provided
by the PowerSocket, so they implement IPowerPlug, and in so
doing they can connect to it.

• Interfaces are useful because they provide contracts that objects


can use to work together without needing to know anything else
about each other.

• Interfaces are more flexible than base classes because you can
define a single implementation that can implement multiple
interfaces.

• Interfaces are better in situations in which you do not have to


inherit implementation from a base class.
Arrays
➢Stores a fixed-size sequential collection of elements of the same type.

➢Used to store a collection of data

➢An array is a collection of variables of the same type.

➢All arrays consist of contiguous memory locations.

➢The lowest address corresponds to the first element and the highest address to
the last element.
➢ To declare an array in C# :
• datatype[] arrayName;
• Eg: int [] balance;

➢Array is a reference type, so need to use the new keyword to create


an instance of the array.

➢To initialize an array :


• int [] balance = new int [10];

➢Declaring an array does not initialize the array in the memory.


Assigning Values to an Array
• Assign values to individual array elements, using the index number :
• double[] balance = new double[10];
• balance[0] = 4500.0;

• Assign values to the array at the time of declaration :


• double[] balance = { 2340.0, 4523.69, 3421.0};

• Creating and initializing an array :


• int [] marks = new int[5] { 99, 98, 92, 97, 95};

• Creating and initializing an array - omit the size


• int [] marks = new int[] { 99, 98, 92, 97, 95};

• Copy an array variable into another target array variable:


• int [] marks = new int[] { 99, 98, 92, 97, 95};
• int[] score = marks;
namespace ConsoleApplication4
{
class Array
{
public static void Main(string[] args)
{
int[] num = new int[6]; // Declaring the Array

//Initializing Array
num[0] = 1;
num[1] = 5;
num[2] = 75;
num[3] = 57;
num[4] = 88;
num[5] = 45;

for (int k = 0; k < num.Length; k++)


{
Console.WriteLine($"Elements [{k}] = {num[k]}");
}
Console.WriteLine("***************");

int[] n = new int[5];

//Initializing elements of Array n


for (int i=0; i<n.Length; i++)
{
Console.WriteLine("\n Enter Value {0}" , i);
n[i] = Convert.ToInt32(Console.ReadLine());
}
//output each elements value

for (int j = 0; j < n.Length; j++)


{
Console.WriteLine("Element [{0}]= {1}", j, n[j]);
}
Console.ReadKey();
}
}
}
Sample Program

Create an array to enter


5 subject marks and find
the total, average of
values in the array.
namespace ConsoleApplication4
{
class Array
{
public static void Main(string[] args)
{
double[] marks = new double[5]; // Declaring the Array
double total=0, average=0;

for (int i = 0; i < marks.Length; i++)


{
Console.WriteLine($"Enter Marks for Subject {i+1}");
marks[i]= Convert.ToDouble(Console.ReadLine());
total = total + marks[i];
}
average = total / 5;

Console.WriteLine($"Total is: {total}");


Console.WriteLine($"Average is: {average}");
Console.ReadKey();
}
}
}
Sample Program

• Create two arrays to add name and


age. (Values accepting by the user)

• Compare two arrays to find the age


of a particular person in the array.

• If the parson could not find; display


a message as “Not Found!!! ”
namespace ConsoleApplication4
{
class Array
{
public static void Main(string[] args)
{
string[] name = new string[4]; // Declaring the Array
int[] age = new int[4];
int x=0;
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine("Enter Your Name: ");
name[i]= Console.ReadLine();
Console.WriteLine("Enter your Age: ");
age[i]= Convert.ToInt32(Console.ReadLine());
}

Console.WriteLine("Enter your name to find the Age: ");


string fname = Console.ReadLine();
for (int c = 0; c < name.Length; c++)
{
if (name[c] == fname)
{
Console.WriteLine($"Name is: {name[c]}");
Console.WriteLine($"Age is: {age[c]}");
x++;
}
}
if (x == 0)
{
Console.WriteLine("No Name Found !!!");
}
Console.ReadKey();
}
}
}
Multidimensional Array
• The multidimensional array is also known as rectangular arrays in C#. It can
be two dimensional or three dimensional. The data is stored in tabular form
(row * column), which is also known as a matrix.
• To create a multidimensional array, we need to use commas inside the
square brackets.

1. int[,] arr=new int[3,3];//declaration of 2D array


2. int[,,] arr=new int[3,3,3];//declaration of 3D array
Console program 26 – multidimensional array
• namespace Consoleprogram26
• {
• internal class Program
• {
• static void Main(string[] args)
• {
• int[,] arr = new int[3, 3];//declaration of 2D array
• arr[0, 1] = 10;//initialization
• arr[1, 2] = 20;
• arr[2, 0] = 30;

• //traversal
• for (int i = 0; i < 3; i++)
• {
• for (int j = 0; j < 3; j++)
• {
• Console.Write(arr[i, j] + " ");
• }
• Console.WriteLine();//new line at each row
• Console.ReadKey();
• }}}}
Console program 26.1 – multidimensional array- without array size and operator

• using System;
• public class MultiArrayExample
• {
• public static void Main(string[] args)
• {
• int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//declaration and initialization

• //traversal
• for(int i=0;i<3;i++){
• for(int j=0;j<3;j++){
• Console.Write(arr[i,j]+" ");
• }
• Console.WriteLine();//new line at each row
• Console.ReadKey();
• } } }


Jagged Arrays

• In C#, jagged array is also known as "array of arrays" because its elements
are arrays. The element size of jagged array can be different.
• Declaration of Jagged array – With two elements
int[][] arr = new int[2][];

• Initialization of Jagged array - The size of elements can be different.


arr[0] = new int[4];
arr[1] = new int[6];
Console program 27 – jagged array
• using System;

• namespace Consoleprogram27
• {
• internal class Program
• {
• static void Main(string[] args)
• {
• int[][] arr = new int[2][];// Declare the array

• arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
• arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };

• // Traverse array elements


• for (int i = 0; i < arr.Length; i++)
• {
• for (int j = 0; j < arr[i].Length; j++)
• {
• Console.Write(arr[i][j] + " ");
• }
• Console.WriteLine();

• }
• Console.ReadKey();
• }}}

You might also like