Visual Programming Language: Classes, Objects, Inheritance, Arrays
Visual Programming Language: Classes, Objects, Inheritance, Arrays
Programming
Language
Classes, Objects, Inheritance, Arrays
Classes and Objects
2
In order to create class, the
class keyword is used.
Syntax:
class print
{
}
Example:
class print
{
public void printname()
{
Console.WriteLine(“Iqra University");
}
}
3
After creating class, you can
use it by creating its object.
An object is created using new keyword.
After creating the object of print class, you can use its
members using the object name as follow:
print pr = new print();
pr.printname();
4
Class - Example Code
class PersonalInfo
{
#region Fields Initializations
string stFirstName;
string stMidName;
string stLastName;
#endregion
#region Properties
public string FirstName
{
get { return stFirstName; }
5
set { stFirstName = value; }
}
PROGRAMMING EXAMPLE
using System; class Program //Creating 3rd class
using System.Collections.Generic; {
using System.Linq; static void Main(string[] args)
using System.Text; {
print p = new print();
namespace Creating_Class p.printdetails();
{ Console.ReadLine();
class accept //Creating 1st. class }
{ }
public string name; }
public void acceptdetails()
{
Console.Write("Enter your name:\t");
name = Console.ReadLine();
}
}
class print // Creating 2nd class
{
public void printdetails()
{
//Creating object of 1st. class
accept a = new accept();
//executing method of 1st class.
a.acceptdetails();
//Printing value of name variable
Console.WriteLine("Your name is " + a.name); 6
}
}
GUIDELINE WHILE CREATING
CLASS
The class name should be noun and meaningful.
Use either pascal case first letter is capital. Ex.
PascalCase.
Or camel case, the first letter is small. Ex. camelCase.
It is strictly recommended you to use pascal case for
class name and camel case for variable name.
Your class name should not contain any special
character except underscore (_) or digit. Must start your
class name with character.
Don’t use reserved keyword for class name.
7
INHERITANCE
8
EXAMPLE:
using System; //Creating Child Class
using System.Collections.Generic; class Scooter : Tyre
using System.Linq; {
using System.Text; public void ScooterType()
using System.Threading.Tasks; {
Console.WriteLine("Scooter Color is Red");
namespace Basic_Example TyreType();
{ }
class Program }
{ //Creating Child Class
static void Main(string[] args) class Car : Tyre
{ {
Scooter sc = new Scooter(); public void CarType()
sc.ScooterType(); {
Console.WriteLine("Car Type : Ferrari");
Car c = new Car(); TyreType();
c.CarType(); }
}
Console.ReadKey(); }
}
}
//Creating Base Class
class Tyre
{
protected void TyreType()
{
Console.WriteLine("This is Tubeless Tyre");
9
}
}
Arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Declare_Array
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[6]; //Declaring Array
//Initializing array
for (int a = 0; a <= 5; a++)
{
Console.Write(“\nEnter the value of array #" + a + ":\t");
num[a] = int.Parse(Console.ReadLine());
}
/*num[1] = 23;
num[2] = 12;
num[3] = 9;
num[4] = 14;
num[5] = 52;*/
int f = 0;
//Showing value of Array
foreach(int j in num)
{f++;
Console.WriteLine(f+" value:\t{0}", j);
}
/*Console.WriteLine("2nd value:\t{0}", num[1]);
Console.WriteLine("3rd value:\t{0}", num[2]);
Console.WriteLine("4th value:\t{0}", num[3]);
Console.WriteLine("5th value:\t{0}", num[4]);
Console.WriteLine("6th value:\t{0}", num[5]);*/
Console.ReadKey();
} 10
}
}
Storing data in two arrays
using System; //Accepting name and find their correspondence age in array.
using System.Collections.Generic;
using System.Linq; Console.Write("\n\nEnter your name to find age:\t");
using System.Text; find = Console.ReadLine();
namespace accessing_array_value for (i = 0; i < 6; i++)
{ {
class Program if (name[i] == find)
{ {
static void Main(string[] args) Console.WriteLine("\n\nName\t:{0}", name[i]);
{ Console.WriteLine("Age\t:{0}", age[i]);
int[] age = new int[6]; j++;
string[] name = new string[6]; }
int i, j = 0; }
string find; if (j == 0)
{
// Storing users name and age in two different array. Console.WriteLine("Not Found!!!");
for (i = 0; i < 6; i++) }
{ Console.ReadLine();
Console.Write("\n\nEnter your name:\t"); }
name[i] = Console.ReadLine(); }
Console.Write("Enter your age:\t\t"); }
age[i] = Convert.ToInt32(Console.ReadLine());
}
11
Class Activity
Write a program to print Array {22,50,11, 2, 49} in
Reverse Order.
Write a program to store and print the three different
values (Book Name, Author and Publisher Name) in
different arrays.
12