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

Lecture 05 - 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 05 - 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/ 32

Language Fundamentals

IV Object oriented with


visual programming
M. Y Abdur Rahman
• Allows multiple implementations of the same
function in a class.
• Overloaded method share the same name but
have a unique signature.
Overloading • The number of parameters, types of parameters
or both must be different.
• A function can’t be overloaded on the basis of a
different return type alone.
Console program 12 - overloading
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram12
• {
• class overloading
• {
• public int x;
• public void calculate(int a, int b) => x = a + b;
• public void calculate(int a, int b, int c) => x = a + b + c;

• public void displayFunc() => Console.WriteLine($"Answer is: {x}");


• }
• public class Execoverloading
• {
• static void Main(string[] args)
• {
• var a = new overloading();
• a.calculate(13, 22);
• a.displayFunc();

• var a1 = new overloading();


• a1.calculate(12, 13, 20);
• a1.displayFunc();
• Console.ReadKey();
• }
• }
• }
• Create overloaded functions to find the area and
volume of a rectangle. Create the function called
“Calculate”.
Sample • Area = length*width

program • Volume = length* width * height


• Get the user input for length, width and height.
• Pass the input through parameter and calculate the
area and volume and display the answers.
Console program 13 - overloading
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram13
• {
• class Rectangle
• {
• public double area, volume;
• public void calculate(double a, double b)
• {
• area = a * b;
• }
• public void calculate(double x, double y, double z)
• {
• volume = x * y * z;
• }
• public void display()
• {
• Console.WriteLine($" Area = {area}");
• Console.WriteLine($" Volume = {volume}");
• }
• }
• class Execute
• {
• static double length, width, height;
• static void Main(string[] args)
• {
• Console.WriteLine("Enter Length: ");
• length = Convert.ToDouble(Console.ReadLine());
• Console.WriteLine("Enter width: ");
• width = Convert.ToDouble(Console.ReadLine());
• Console.WriteLine("Enter height: ");
• height = Convert.ToDouble(Console.ReadLine());

• var r = new Rectangle();


• r.calculate(length, width);
• r.calculate(length, width, height);
• r.display();
• Console.ReadKey();

• }
• }
• }
Inheritance
• Allows classes to inherit commonly used state and behaviour from other
classes.
• Ability to create a new class from an exiting class by extending it.
Bicycle

Mountain Road Tandem


Bicycle Bicycle Bicycle
• Is the process of one object can acquire the
properties of another object.
• Allows to reuse existing code
Inheritance • The derived class inherits from the base class
con., • Can override inherited members as well as add new
members to extend the base class.
• Classes can inherit from a single class and one or
more interfaces.
Class derives class: base class, Interface1, Interface 2
{
Inheritance body
con., }
• Cross – language and multiple inheritance is not
support by .NET
Console program 14 – inheritance
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram14
• {
• public class Father
• {
• public string surname { get; } = "Perera";
• protected string lapmodel { get; } = "HP";
• internal string house { get; } = "Beach house";
• string designation { get; } = "General Manager";
• }
Console program 14 – inheritance
• public class child:Father
• {
• public string name { get; } = "Yasiru";
• public string degree { get; } = "Computer Science";
• public void display()
• {
• Console.WriteLine($"Laptop Model: {lapmodel}");
• // Console.WriteLine(designation); cannot access because it
is a private variable
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• Console.WriteLine("Chile class propertise");
• var a = new child();
• Console.WriteLine($"Name: {a.name}");
• Console.WriteLine($"Degree: {a.degree}");
• Console.WriteLine($"Surname: {a.surname}");
• Console.WriteLine($"House : {a.house}");
• a.display();
• Console.WriteLine("Parent class propertise");
• var b = new Father();
• Console.WriteLine($"Surname: {b.surname}");
• Console.WriteLine($"House: {b.house}");

• //Console.WriteLine($"Designation: {b.designation}"); can


not access private variable
• //Console.WriteLine($"Car model: {b.lapmodel}"); Can not
access protedted variable
• //Console.WriteLine($"Degree: {b.degree}"); cann not access
child variable

• Console.ReadKey();

• }
• }
• }
• Encapsulation is a process of binding the data
members and member functions into a single unit.
In c#, the class is the real-time example for
encapsulation because it will combine various types
of data members and member functions into a
single unit.
• Data hiding example, there is a class with a variable
and two functions. Here, the variable "num" is
private so, it can be accessed only by the members
of the same class, and it can't be accessed anywhere
else. Hence, it is unable to access this variable
outside the class, which is called data hiding.
• Child class override parent class method.
• Cannot change the number of parameters and type
of the parameters.
Overriding • Must need the same signature.
• Virtual and override key words are used for the
methods.
• Allow the function to be overridden in any derived
class
Virtual & • Virtual function has to implement both in the base
class and derived class.
override • If a method is virtual in the base class then provide
keyword the override keyword in the derived class
• Neither members fields nor static functions can
be declared as virtual.
Console program 15 – override
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram15
• {
• public class Emp
• {
• public virtual void join()
• {
• Console.WriteLine("New employee joined.");
• }
• }
• public class Managers:Emp
• {
• public override void join()
• {
• Console.WriteLine("New employee joined as manager.");
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {

• var b = new Managers();
• b.join();

• Console.ReadKey();

• }
• }
• }
• Example: A child class Boy and a parent class
Human. The Boy class extends Human class. Both
the classes have a common method void eat() . Boy
class is giving its own implementation to the eat()
method or in other words it is overriding the eat()
method.

• The ability of a subclass to override a method


enables a class to inherit from a superclass whose
behaviour is “close enough" and then to adjust
behaviour as necessary. The name, number, and
type of parameters as well as the return type of the
overriding method are all the same as the original.
• Both classes and functions can be declared abstract using the
abstract keyword.
• Can’t create an instance of an abstract class.
• An abstract members has a signature
• But no function body
• They must be overridden in any non-abstract derived
Abstract class class.
• Abstract classes exist primarily for inheritance.
• Member functions, properties and indexes can be
abstract.
• A class with one of more abstract members must be
abstract as well.
• Static members can’t be abstract.
Console program 16 – abstract

• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram16
• {
• public abstract class Abstractclass
• {
• public abstract void display();//abstract method with no implementation
• }
• public class Abssubclass:Abstractclass
• {
• public override void display() => Console.WriteLine("Printing");
• //overriding abstract method to implement
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• //cannot create instance for abstract class
• /*var a = new Abssubclass();
• a.display();*/

• var b = new Abssubclass();


• b.display();
• Console.ReadKey();
• }
• }
• }
• Objects in an OOP language provide an abstraction
that hides the internal implementation details. Similar
to the coffee machine in your kitchen, you just need to
know which methods of the object are available to call
and which input parameters are needed to trigger a
specific operation.
• You can create functionality in an abstract class that
subclasses can implement or override. You cannot
actually implement functionality using an interface;
just declare it. Additionally, an interface can be used by
more than one class, but a class can extend only one
abstract class.
• Example: Abstraction, a Vehicle parent class with
Truck and Motorbike inheriting from it is an abstraction
that easily allows more vehicles to be added
Data type
Reference type Value type
• A reference type doesn't store its value • A data type is a value type if it holds a data
directly. Instead, it stores the address where value within its own memory space. It means
the value is being stored. It contains a pointer the variables of these data types directly
to another memory location that holds the contain values.
data
• Use stack only
• Use Heap and Stack both
• Variables of value types directly contain their
• With reference type it is possible two variables data
to reference the same object
• Simple data type, Enum, struct, nullable are
• Possible for operations on one variable to value types.
affect the object referenced by the other
variable
• https://www.tutorialsteacher.com/csharp/csha
rp-value-type-and-reference-type
• Classes, interface, Array, Delegate are
• https://www.tutorialsteacher.com/csharp/csha
reference types
rp-data-types - Enum and other data types
• It is a contract for a behaviour Different and diverse classes
can implement the same interface. Much like abstract class.
• Share the fact-no instances of them can be created.
• No fields.
• Can define methods, properties and events but no
implementation.
Interface • No method bodies are allowed.
• Interface is kind of abstract class with nothing but
abstract methods, and since there are no methods with
actual code.
• C# doesn’t allow multiple inheritance, where classes
inherit more that a single base class but allow for
implementation of multiple.
overview
• interface IdataBound
• {
• void bind(IdataBound binder); // cannot use access modifier
• }
• class control
• {

• }
• class EditBox : control, IdataBound // inherit a class as well as interface
• {
• void IdataBound.bind(IdataBound binder)
• {
• //implementation
• }
• }
Console program 17 – interface
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram17
• {
• interface Ijump
• {
• void jump();
• }
• class Car:Ijump
• {
• public void jump()
• {
• Console.WriteLine("Jump");
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• var x = new Car();
• x.jump(); // class object calls the method
• Console.ReadKey();
• }
• }
• }
Difference between interface and abstract

Interface Abstract
• Interface support multiple implementation • Abstract class does not support multiple
implementation
• Interface does not contain data member
• Interface does not contain constructors
• Abstract class contain data member
• Abstract class contain constructors
Thank you

You might also like