You are on page 1of 9

Visual programing (CS-692/CS-783)

Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093


Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
Note:

It is intimated that following Lectures will not be repeated and would be part
of mid-term & final exam as well.
(Week 3) Lecture # 5:

Objective:

• Inheritance

• Foreach loop

• Generics Functions and Classes

Inheritance:
One of the most important concepts in object-oriented programming is that of inheritance.
Inheritance allows us to define a class in terms of another class, which makes it easier to create
and maintain an application. This also provides an opportunity to reuse the code functionality and
fast implementation time. When there is “is a” relation between two classes that is inheritance.

When creating a class, instead of writing completely new data members and member functions,
the programmer can designate that the new class should inherit the members of an existing class.
This existing class is called the base class, and the new class is referred to as the derived class.

Base Class/Parent Class:


namespace Inheritance
{
class SuperClass
{
public virtual void doSomeThing()
{
Console.WriteLine("In super class");
}
}
}
Derived Class/Child Class:
namespace Inheritance
{
class SubClass:SuperClass
{
public override void doSomeThing()
{
base.doSomeThing();
Console.WriteLine("In sub class");
}
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
}
}
Main Class:
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
SuperClass objsc = new SubClass();
objsc.doSomeThing();
}
}
}
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
student s1 = new student();
s1.setData();
s1.showData();
}
}

public class person


{
string name;
int id;

public virtual void setData()


Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
{
Console.WriteLine("Enter Name:");
name = Console.ReadLine();
Console.WriteLine("ID:");
id=int.Parse(Console.ReadLine());
}

public virtual void showData()


{
Console.WriteLine("Name is: "+ name);
Console.WriteLine("ID is: "+id);

}
}

public class student : person


{
string grade;
int marks;

public override void setData()


{
base.setData();
Console.WriteLine("Enter marks:");
marks=int.Parse(Console.ReadLine());
Console.WriteLine("Enter Grade:");
grade = Console.ReadLine();
}

public override void showData()


{
base.showData();
Console.WriteLine("Marks are: " + marks);
Console.WriteLine("Grade is: "+grade);

}
}
}

FOREACH LOOP:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
static void Main(string[] args)
{
int[] myArray = new int[10];

for (int i = 0; i < 10; i++)


{
myArray[i] = int.Parse(Console.ReadLine());
}

foreach (int a in myArray)


{
Console.WriteLine(a);
}

}
}
}

GENERICS:
It helps us to maximize code reuse, performance and type safety.
Generics are strongly type collections.
Generics helps to decouple the data-type from the logical code snippet which makes the code to
reuse again and again

When to Use
Generics helps to separate the data-type from the logical code i.e. from methods and classes. So
if you want to maximize your code reusability and maintain its type safety, and performance then
use generics.
Generics acts as a bridge between dot.net data-types and your custom logical code and provides
flexibility to maximize the code reusability and reduces code.

EXAMPLE:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
compare<string>("21", "21");

}
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819

public static void compare<T>(T a, T b)


{
if (a.Equals(b))
{
Console.WriteLine("Equals");
}
else
Console.WriteLine("Not Equals");
}
}
}

Example 2:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)//static function call static function only
{
int a = 10, b = 20;
swap<int>(ref a, ref b);
Console.WriteLine("A is: " + a);
Console.WriteLine("B is: " + b);

string s1 = "aaa", s2 = "qqq";


swap<string>(ref s1, ref s2);
Console.WriteLine("s1 is: " + s1);
Console.WriteLine("s2 is: " + s2);
Console.Readkey();
public static void swap<T>(ref T lhs, ref T rhs)//This is kept static. main static
{
T temp;
Temp = lhs;
lhs = rhs;
rhs = temp;
}
}
}
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
(Week 3) Lecture # 6:
Objective:

 Stack
 Queue
 List
 Array

Arrays:

An array in Programming Language can be defined as number of memory locations, each of


which can store the same data type and which can be references through the same variable name.

An array is a collective name given to a group of similar quantities. These similar quantities
could be percentage marks of 100 students, number of chairs in home, or salaries of 300
employees or ages of 25 students.

Syntax:

DATATYPE[ ] ARRAYNAME= new DATATYPE[SIZE] ;

Example:

int[] arrayName=new int[10];

Saving Data in an Array:

for (int i = 0; i < 10; i++)


{
arrayName[i] = int.Parse(Console.ReadLine());
}

Reterving Data from an Array:

for (int i = 0; i < 10; i++)


{
Console.WriteLine(arrayName[i]);
}
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
Fuctions and Properties Associated with Arrays:

int noOfElement=arrayName.Length;
int maxValue=arrayName.Max<int>();
int minValue = arrayName.Min<int>();
bool flag=arrayName.Contains(10);

STACK:
Working principle is FIRST IN LAST OUT.
Required namespace is : System.Collections.Generic;
To add value in stack push() method is used. To remove from stack pop() method is used. To
show top element peek() method is used.
C# Code:

Stack<string> myStack = new Stack<string>();


myStack.Push("Element 1");
myStack.Push("Element 2");
string top=myStack.Peek();
Console.WriteLine(top);
Console.WriteLine(myStack.Pop());
Console.WriteLine(myStack.Pop());
myStack.Count();
top = myStack.Peek();
Console.WriteLine(top);

QUEUE:

Working principle is FIRST IN FIRST OUT.


Required namespace is: System.Collections.Generic;
Too add value in queue enqueue() method is used. To remove from queue dequeue() method is
used. To show front element peek() method is used.

C# Code:
Queue<string> myQueue = new Queue<string>();
myQueue.Enqueue("E1");
myQueue.Peek();
myQueue.Dequeue();
myQueue.Count();
myQueue.Contains("e1");

LIST:
Required namespace is: System.Collections.Generic;
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819

List<string> newList = new List<string>();


newList.Add("This");
newList.Add(" is ");
newList.Add("my first ");
newList.Add("application ");
newList.Add("for Generics");

foreach (string item in newList)


{
Console.WriteLine(item);
}

//newList.Clear();
bool find=newList.Contains(" is ");
Console.WriteLine(find);

int noOfItem=newList.Count();
Console.WriteLine(noOfItem);

string element=newList.ElementAt(4);
Console.WriteLine(element);

newList.Remove("This");

newList.RemoveAt(0);

newList.Reverse();

You might also like